问题:
尝试使用karma-systemjs将 SystemJS 配置与 Karma 集成,但是 Karma 抱怨无法为每个导入找到 package.json 文件:
...
10 07 2017 13:38:15.107:WARN [web-server]: 404: /node_modules/angular-mocks/package.json
10 07 2017 13:38:15.380:WARN [web-server]: 404: /node_modules/moment/package.json
10 07 2017 13:38:15.382:WARN [web-server]: 404: /node_modules/angular/package.json
PhantomJS 2.1.1 (Windows 7 0.0.0) ERROR
Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:9876/node_modules/angular-mocks/package.json
Error loading http://localhost:9876/node_modules/angular-mocks/package.json
Error loading build/js/app.spec.js
PhantomJS 2.1.1 (Windows 7 0.0.0): Executed 0 of 0 ERROR (0.945 secs / 0 secs)
以上已被截断,但输出上方还有几个缺少 package.json 警告。此外,这些错误仅与 Karma 有关,实际上应用程序本身在以下设置下运行良好。
依赖版本:
"karma": "1.7.0"
"karma-jasmine": "1.1.0"
"karma-systemjs": "0.16.0"
"systemjs": "0.19.47"
SystemJS 配置:
(引用自:https ://github.com/systemjs/systemjs/issues/767#issuecomment-139515090 )
System.config({
defaultJSExtensions: true,
transpiler: "babel",
babelOptions: {
"optional": [
"runtime",
"optimisation.modules.system"
]
},
paths: {
'*': './node_modules/*',
},
packageConfigPaths: ['./node_modules/*/package.json'],
map: {
'systemjs': './node_modules/systemjs/dist/system.js',
'system-polyfills': './node_modules/systemjs/dist/system-polyfills.js',
'babel': './node_modules/babel-core/browser.js',
'angular-feature-flags': './node_modules/angular-feature-flags/dist/featureFlags.min.js',
crypto: '@empty',
fs: '@empty',
stream: '@empty'
}
});
业力配置:
module.exports = function (config) {
"use strict";
config.set({
autoWatch: true,
singleRun: false,
port: 9876,
frameworks: ["systemjs", "jasmine"],
babelPreprocessor: {
options: {
compact: true
}
},
files: [
"node_modules/phantomjs-polyfill-object-assign/object-assign-polyfill.js",
{pattern: "app/assets/images/**/*", watched: false, included: false, served: true},
"build/**/*spec*.js"
],
systemjs: {
configFile: "./config.js",
serveFiles: [
'./build/**/*.js'
]
},
browsers: ["PhantomJS"],
reporters: ["spec"]
});
};