Load node module with SystemJS and Karma in Angula

2020-08-15 04:05发布

问题:

I'm building a web app using Angular 2, SystemJS, and Karma for tests.

I'm trying to load the node module ngrx/store in a test:

import {
  it, describe, expect, beforeEach, inject
} from 'angular2/testing';

import { Store } from '@ngrx/store';

describe('Graphs store', () => {
  let graphs;

  beforeEach(inject([Store], (store: Store<any>) => {
    graphs = store.select('graphs');
  }));

  it('works', () => {
    // expect graphs to do something...
  });
});

However, my tests fail with the following message:

404: /@ngrx/store
Chrome 48.0.2564 (Mac OS X 10.11.3) ERROR
  Error: XHR error (404 Not Found) loading http://localhost:9876/@ngrx/store

I actually had the same problem in dev as well, and it turns out SystemJS did not know where to find @ngrx/store. To solve this, I did this:

System.config({
  packages: {
    src: {
      format: 'register',
      defaultExtension: 'js'
    }
  },
  map: { '@ngrx/store' : 'node_modules/@ngrx/store/dist/store.js' } // <-- this
});

I modified my Karma shim file to do the same. Upon the running the tests a second time, I now get a different error:

404: /node_modules/@ngrx/store/dist/store.js
Chrome 48.0.2564 (Mac OS X 10.11.3) ERROR
  Error: XHR error (404 Not Found) loading http://localhost:9876/node_modules/@ngrx/store/dist/store.js

This means it must be taking the explicit path I gave it into account, but it still cannot find the module. It is the correct path to the module, however, and works when loaded in a browser.

I'm pretty lost on what to do next. Can someone point me in the right direction?

A few things to note:

  • Adding the node module to Karma's files array is not an option, since its dependencies need to be resolved with SystemJS
  • This only happens with node modules for which SystemJS needs custom instructions on where they are located. I can load other modules just fine in my tests without providing a specific location as long SystemJS is able to find it

Here is my Karma configuration:

// Set up with the help of
// http://twofuckingdevelopers.com/2016/01/testing-angular-2-with-karma-and-jasmine/

module.exports = function(config) {
  config.set({

    basePath: '.',

    frameworks: ['jasmine'],

    files: [
      // paths loaded by Karma
      {pattern: 'node_modules/angular2/bundles/angular2-polyfills.js', included: true, watched: true},
      {pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: true},
      {pattern: 'node_modules/rxjs/bundles/Rx.js', included: true, watched: true},
      {pattern: 'node_modules/angular2/bundles/angular2.dev.js', included: true, watched: true},
      {pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: true},
      {pattern: 'karma-test-shim.js', included: true, watched: true},

      // paths loaded via module imports
      {pattern: 'src/**/*.js', included: false, watched: true},

      // paths to support debugging with source maps in dev tools
      {pattern: 'src/**/*.ts', included: false, watched: false},
      {pattern: 'src/**/*.js.map', included: false, watched: false}
    ],

    // proxied base paths
    proxies: {
      // required for component assets fetched by Angular's compiler
      '/src/': '/base/src/'
    },

    port: 9876,

    logLevel: config.LOG_INFO,

    colors: true,

    autoWatch: true,

    browsers: ['Chrome'],

    // Karma plugins loaded
    plugins: [
      'karma-jasmine',
      'karma-coverage',
      'karma-chrome-launcher'
    ],

    // // Coverage reporter generates the coverage
    // reporters: ['progress', 'dots', 'coverage'],
    //
    // // Source files that you wanna generate coverage for.
    // // Do not include tests or libraries (these files will be instrumented by Istanbul)
    // preprocessors: {
    //   'src/**/!(*spec).js': ['coverage']
    // },

    // coverageReporter: {
    //   reporters:[
    //     {type: 'json', subdir: '.', file: 'coverage-final.json'}
    //   ]
    // },

    singleRun: true
  })
};

And here is my Karma shim:

// Tun on full stack traces in errors to help debugging
Error.stackTraceLimit = Infinity;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;

// // Cancel Karma's synchronous start,
// // we will call `__karma__.start()` later, once all the specs are loaded.
__karma__.loaded = function() {};

System.config({
  packages: {
    'base/src': {
      defaultExtension: 'js',
      format: 'register',
      map: Object.keys(window.__karma__.files).filter(onlyAppFiles).reduce(createPathRecords, {})
    }
  },
  // This makes it work in the browser, but not in my tests!
  paths: { '@ngrx/store' : 'node_modules/@ngrx/store/dist/store.js' }
});

System.import('angular2/src/platform/browser/browser_adapter')
  .then(function(browser_adapter) { browser_adapter.BrowserDomAdapter.makeCurrent(); })
  .then(function() { return Promise.all(resolveTestFiles()); })
  .then(function() { __karma__.start(); }, function(error) { __karma__.error(error.stack || error); });

function createPathRecords(pathsMapping, appPath) {
  // creates local module name mapping to global path with karma's fingerprint in path, e.g.:
  // './vg-player/vg-player':
  // '/base/src/vg-player/vg-player.js?f4523daf879cfb7310ef6242682ccf10b2041b3e'
  var pathParts = appPath.split('/');
  var moduleName = './' + pathParts.slice(Math.max(pathParts.length - 2, 1)).join('/');
  moduleName = moduleName.replace(/\.js$/, '');
  pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath];
  return pathsMapping;
}

function onlyAppFiles(filePath) {
  return /\/base\/src\/(?!.*\.spec\.js$).*\.js$/.test(filePath);
}

function onlySpecFiles(path) {
  return /\.spec\.js$/.test(path);
}

function resolveTestFiles() {
  return Object.keys(window.__karma__.files)  // All files served by Karma.
    .filter(onlySpecFiles)
    .map(function(moduleName) {
      // loads all spec files via their global module names (e.g.
      // 'base/src/vg-player/vg-player.spec')
      return System.import(moduleName);
    });
}

Update

There is an example repository with the error here. You can see the specific changes that cause the error here. Run $ npm install and $ npm test to get the error.

回答1:

Including @ngrx/store with the rest of your bundles, would resolve 404 error

    // for testing in karma.conf.js
    files: [
        // paths loaded by Karma
        {pattern: 'node_modules/@ngrx/store/dist/store.js', included: true, watched: true},
    ],

But unlike the rest of the bundles that are compiled as System modules, @ngrx/store is compiled as commonjs module

// 'node_modules/angular2/bundles/angular2.dev.js'
"format register";
System.register("angular2/src/facade/lang", [], true, function(require, exports, module) {
....

// 'node_modules/rxjs/bundles/Rx.js'
"format register";
System.register("rxjs/util/root", [], true, function(require, exports, module) {
....

// 'node_modules/@ngrx/store/dist/store.js'
....
var Observable_1 = require('rxjs/Observable');
....

which causes an error:

Uncaught ReferenceError: require is not defined

// with {pattern: '~/store.js', indluded: true}
// context.html includes
<script type="text/javascript" src="/base/node_modules/@ngrx/store/dist/store.js?fb5e807149603c3c2f998c98faf6826c7e301d71"></script>

That's why you shouldn't include it:

{pattern: 'node_modules/@ngrx/store/dist/store.js', included: false, watched: true}

This will basically list it in window.__karma__.files object, but won't add it as a script in karma's context.html - browser won't load and run the code that causes the error. Loading should be handled by SystemJS...

If you run karma tests with singleRun: false you can inspect files in Chrome's Devtools > Network. You'll see there a list of loaded files and that's where the last piece of the puzzle is:

In your karma-test-shim.js change System.config.map to:

map: { '@ngrx/store' : '/base/node_modules/@ngrx/store/dist/store.js' }

Executed 4 of 4 SUCCESS (0.037 secs / 0.008 secs)



回答2:

Sent a PR with the fix. There might be a better way to do this but this works:

Copy the store.js file to the dist folder by updating the package.json's build script

... && cp node_modules/@ngrx/store/dist/store.js dist/store.js

Update the path in karma-test.shim.js

paths: { '@ngrx/store' : '/base/dist/store.js' }