vendredi 3 juin 2016

How to dynamically mock ES6 modules with SystemJS?

I have a single-page application written in ES6. The code in transpiled server-side into classic javascript by babelJs, then loaded by SystemJs.

Javascript present in my html file:

System.config({
  baseURL: '/js',
  meta: {
    '/js/*': { format: 'cjs' }
  }});
System.defaultJSExtensions = true;
System.import("index.js")
    .catch(function (error) {
        console.error(error)
    });

index.js:

import f1 from 'file1';
import f2 from 'file2';

// code here ...

Everything works fine. index.js is loaded, and all import statements are correctly executed.

Now, I want to create some pages with mocked ES6 modules, for testing purpose. My goal is to display pages by replacing model classes (contained in ES6 modules) with other static test classes.

Let's say I have 3 files: real_model.js, fake_model.js and component.js. component.js import the real model (import Model from 'real_model';).

How can I replace the real model by the fake one (in the component) dynamically ?

Aucun commentaire:

Enregistrer un commentaire