将应用程序升级到rc-7并运行我的单元测试以确保没有任何损坏后,我在大约一半的单元测试中收到此错误消息。
"错误:Zone.js检测到ZoneAwarePromise'(窗口|全局).Promise'已被覆盖。 最有可能的原因是在Zone.js之后加载了Promise polyfill(在加载zone.js时不需要Polyfilling Promise api。如果必须在node_modules / zone.js /中加载一个,请在加载zone.js之前加载)。 dist / zone.js(第21行)"
我能想到的最接近的事情是我在单位测试中通过手动返回observable来模拟http调用的响应。
这是一个使用Jasmine和Karma进行单元测试的Angular2应用程序。
更新 因此,根据错误消息,这似乎可能是一个加载订单问题,但是我没有在我可以确定的任何地方加载polyfill,除非我不小心抓住它们。我已经附加了我的karma.conf.js和systemjs.conf.js以帮助查找错误。
Karma.conf.js
module.exports = function(config) {
var gulpConfig = require('../gulp/config')();
/**
* List of npm packages that imported via `import` syntax
*/
var dependencies = [
'@angular',
'lodash',
'rxjs'
];
var configuration = {
basePath: '../../',
browserNoActivityTimeout: 20000,
frameworks: ['jasmine'],
browsers: ['PhantomJS'],
reporters: ['progress', 'coverage'],
preprocessors: {},
// Generate json used for remap-istanbul
coverageReporter: {
includeAllSources: true,
dir: 'coverage/appCoverage/remap/',
reporters: [
{ type: 'json', subdir: 'report-json' }
]
},
files: [
'node_modules/reflect-metadata/Reflect.js',
'node_modules/zone.js/dist/zone.js',
'node_modules/zone.js/dist/long-stack-trace-zone.js',
'node_modules/zone.js/dist/proxy.js',
'node_modules/zone.js/dist/sync-test.js',
'node_modules/zone.js/dist/jasmine-patch.js',
'node_modules/zone.js/dist/async-test.js',
'node_modules/zone.js/dist/fake-async-test.js',
'node_modules/angular/angular.js',
'node_modules/core-js/client/shim.min.js',
'node_modules/systemjs/dist/system.src.js'
],
// proxied base paths
proxies: {
// required for component assests fetched by Angular's compiler
"/src/": "/base/src/",
"/app/": "/base/src/app/",
"/node_modules/": "/base/node_modules/"
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
//browserNoActivityTimeout: 100000
};
configuration.preprocessors[gulpConfig.tmpApp + '**/!(*.spec)+(.js)'] = ['coverage'];
configuration.preprocessors[gulpConfig.tmpApp + '**/*.js'] = ['sourcemap'];
configuration.preprocessors[gulpConfig.tmpTest + '**/*.js'] = ['sourcemap'];
var files = [
gulpConfig.tmpTest + 'test-helpers/global/**/*.js',
gulpConfig.src + 'systemjs.conf.js',
'config/test/karma-test-shim.js',
createFilePattern(gulpConfig.tmpApp + '**/*.js', { included: false }),
createFilePattern(gulpConfig.tmpTest + 'test-helpers/*.js', { included: false }),
createFilePattern(gulpConfig.app + '**/*.html', { included: false }),
createFilePattern(gulpConfig.app + '**/*.css', { included: false }),
createFilePattern(gulpConfig.app + '**/*.ts', { included: false, watched: false }),
createFilePattern(gulpConfig.tmpApp + '**/*.js.map', { included: false, watched: false })
];
configuration.files = configuration.files.concat(files);
dependencies.forEach(function(key) {
configuration.files.push({
pattern: 'node_modules/' + key + '/**/*.js',
included: false,
watched: false
});
});
if (process.env.APPVEYOR) {
configuration.browsers = ['IE'];
configuration.singleRun = true;
configuration.browserNoActivityTimeout = 90000; // Note: default value (10000) is not enough
}
config.set(configuration);
// Helpers
function createFilePattern(path, config) {
config.pattern = path;
return config;
}
}
更新 我的解决方案涉及确保我的所有zone.js文件最后都在karma.conf.js中加载。我猜测我包含的其他东西也包括polyfill,但是在zone.js之后加载,所以引发了错误。将zone.js移动到最后,一切正常。
答案 0 :(得分:3)
我遇到了同样的问题。 (Angular 2 RC.7 Jasmine Karma)这是由于我在 karma.conf.js 中加载测试文件的顺序。我在 /systemjs/dist/system-polyfills.js 之前加载 /zone.js/dist/zone.js 。更改文件的顺序以加载zone.js在systemjs polyfills解决此问题之后。
这是我在karma.conf.js中的当前设置,它为我解决了错误。
files: [
{pattern: 'dist/vendor/es6-shim/es6-shim.js', included: true, watched: false},
{pattern: 'dist/vendor/reflect-metadata/Reflect.js', included: true, watched: false},
{pattern: 'dist/vendor/systemjs/dist/system-polyfills.js', included: true, watched: false},
{pattern: 'dist/vendor/systemjs/dist/system.src.js', included: true, watched: false},
{pattern: 'dist/vendor/zone.js/dist/zone.js', included: true, watched: false},
{pattern: 'dist/vendor/zone.js/dist/proxy.js', included: true, watched: false},
{pattern: 'dist/vendor/zone.js/dist/sync-test.js', included: true, watched: false},
{pattern: 'dist/vendor/zone.js/dist/long-stack-trace-zone.js', included: true, watched: false},
{pattern: 'dist/vendor/zone.js/dist/async-test.js', included: true, watched: false},
{pattern: 'dist/vendor/zone.js/dist/fake-async-test.js', included: true, watched: false},
{pattern: 'dist/vendor/zone.js/dist/jasmine-patch.js', included: true, watched: false},
{pattern: 'config/karma-test-shim.js', included: true, watched: true},
// Distribution folder.
{pattern: 'dist/**/*', included: false, watched: true}
],
答案 1 :(得分:0)
我还必须在zone.js之前加入es6-promise。 HTH。
答案 2 :(得分:0)
如果有人犯了与我一样的愚蠢错误,只是为了增加我的经验。使用ng
我在scripts
的{{1}}部分中有以下内容:
.angular-cli.json
我甚至不记得,为什么我把...
"scripts": [
"../node_modules/bootstrap/dist/js/bootstrap.min.js",
"../node_modules/core-js/client/shim.min.js"
],
...
置于首位。但无论如何,在删除之后,我的测试用例现在运行得很完美:
shim.min.js