我正在为Angular 2应用设置Karma。我的karma-test-shim.js中的System.import抛出了一个错误,我相信这是因为我输错的是我放入karma.conf.js的内容。
我的karma.conf.js(裁剪):
'use strict';
var argv = require('yargs').argv;
module.exports = function (config) {
config.set({
basePath: './',
frameworks: ['jasmine'],
files: [
'node_modules/core-js/client/shim.min.js',
'node_modules/traceur/bin/traceur.js',
// System.js for module loading
'node_modules/systemjs/dist/system.src.js',
// Zone.js dependencies
'node_modules/zone.js/dist/zone.js',
'node_modules/zone.js/dist/long-stack-trace-zone.js',
'node_modules/zone.js/dist/async-test.js',
'node_modules/zone.js/dist/fake-async-test.js',
'node_modules/zone.js/dist/sync-test.js',
'node_modules/zone.js/dist/proxy.js',
'node_modules/zone.js/dist/jasmine-patch.js',
// Paths loaded via module imports
{ pattern: 'node_modules/@angular/**/*.js', included: false, watched: true },
{ pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false },
{ pattern: 'app/**/*.js', included: false, watched: true },
{ pattern: 'app/**/*.html', included: false, watched: true, served: true },
{ pattern: 'app/**/*.css', included: false, watched: true, served: true },
{ pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: false, watched: false },
{ pattern: 'karma-test-shim.js', included: true, watched: true},
],
// Proxied base paths
proxies: {},
exclude: [
'node_modules/**/*spec.js'
],
reporters: ['mocha'],
port: 9876,
colors: true,
logLevel: config.LOG_DEBUG,
autoWatch: false,
// TODO: Remove sandbox. This is just work around for Chrome issue
// https://github.com/karma-runner/karma-chrome-launcher/issues/73#issuecomment-236597429
browsers: ['ChromeNoSandbox'],
customLaunchers: {
ChromeNoSandbox: {
base: 'Chrome',
flags: ['--no-sandbox']
}
},
singleRun: true,
// Command line args for tests
client: {
files: argv.files
}
});
};
我的业力测试-shim.js:
// Turn 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/app': {
defaultExtension: false,
format: 'register',
map: Object.keys(window.__karma__.files).
filter(onlyAppFiles).
reduce(function createPathRecords(pathsMapping, appPath) {
var moduleName = appPath.replace(/^\/base\/app\//, './').replace(/\.js$/, '');
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]
return pathsMapping;
}, {})
}
}
});
Promise.all([
System.import('@angular/core/testing'),
System.import('@angular/platform-browser-dynamic/testing')
])
// First, initialize the Angular testing environment.
.then(([testing, testingBrowser]) => {
// testing.getTestBed().initTestEnvironment(
// testingBrowser.BrowserDynamicTestingModule,
// testingBrowser.platformBrowserDynamicTesting()
// );
testing.setBaseTestProviders(testingBrowser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
testingBrowser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
})
// Then we find all the tests.
.then(() => require.context('./', true, /\.spec\.ts/))
// And load the modules.
.then(context => context.keys().map(context))
// Finally, start Karma to run the tests.
.then(__karma__.start, __karma__.error);
function filePath2moduleName(filePath) {
return filePath.
replace(/^\//, ''). // remove / prefix
replace(/\.\w+$/, ''); // remove suffix
}
function onlyAppFiles(filePath) {
return /\/base\/app\/(?!.*\.spec\.js$).*\.js$/.test(filePath);
}
function onlySpecFiles(path) {
return /spec\.js$/.test(path);
}
我得到的错误:
24 11 2016 18:19:31.954:DEBUG [middleware:source-files]: Requesting /@angular/core/testing /
24 11 2016 18:19:31.954:DEBUG [middleware:source-files]: Fetching /@angular/core/testing
24 11 2016 18:19:31.956:WARN [web-server]: 404: /@angular/core/testing
24 11 2016 18:19:31.958:DEBUG [middleware:source-files]: Requesting /@angular/platform-browser-dynamic/testing /
24 11 2016 18:19:31.959:DEBUG [middleware:source-files]: Fetching /@angular/platform-browser-dynamic/testing
24 11 2016 18:19:31.962:WARN [web-server]: 404: /@angular/platform-browser-dynamic/testing
Chrome 54.0.2840 (Windows 7 0.0.0) ERROR
{
"originalErr": {}
}
答案 0 :(得分:0)
你的karma-test-shim.js错误,你需要改变地图
System.config({
baseURL: '/base',
// Extend usual application package list with test folder
packages: {'testing': {main: 'index.js', defaultExtension: 'js'}},
// Assume npm: is set in `paths` in systemjs.config
// Map the angular testing umd bundles
map: {
'@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
'@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
'@angular/forms/testing': 'npm:@angular/forms/bundles/forms-testing.umd.js',
},
});
强文