Load node module with SystemJS and Karma in Angular 2
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)