我已经将angular-cli 1.0.0生成的基本项目从使用jasmine转换为使用mocha。 为什么当我完成转换为mocha时,所有测试都会运行两次?有趣的是,当我们使用wallabyjs runner运行相同的测试/代码或使用降级的angular2时,测试只运行一次。
要从头开始重现,我选择了第3步,选择了第4步。不幸的是,涉及到很多代码,所以我提供了一个存储库来演示和遵循每个步骤的命令,可以在步骤细节中找到。
ng new someBlankProject
cd someBlankProject
ng test
预期结果3次测试git clone git@github.com:arranbartish/angular-cli-seed.git 01-fresh-project; cd 01-fresh-project; git checkout 2bf12b577173085344a86e37726d9f7ece930c77;npm install;npm run test -- --single-run=true
npm install
解决依赖关系ng test
预期结果3次测试No failing tests, 3 passing
git clone git@github.com:arranbartish/angular-cli-seed.git 02-add-wallaby; cd 02-add-wallaby; git checkout 0bb96884d0e71f286d0b4fedc0dcafd20dc9d2b1;npm install;npm run test -- --single-run=true
No failing tests, 3 passing
npm prune;npm install
来清理和解决依赖关系ng test
预期结果 3次测试实际结果 6次测试
Chromeium 53.0.2785 (Ubuntu 0.0.0): Executed 6 of 3 SUCCESS (0.347 secs / 0.042 secs)
No failing tests, 3 passing
git clone git@github.com:arranbartish/angular-cli-seed.git 03-converted-mocha; cd 03-converted-mocha; git checkout 0a9ed8804e15c451ff0d67ebd2d38980d54f9763;npm install;npm run test -- --single-run=true
No failing tests, 3 passing
我观察到的一个最奇怪的事情是使用角度2不具有相同的行为!
npm prune;npm install
来清理和解决依赖关系ng test
预期结果3次测试No failing tests, 3 passing
git clone git@github.com:arranbartish/angular-cli-seed.git 04-downgrade-angular; cd 04-downgrade-angular; git checkout 0e6c2811b9aab722fcc382e4676d97c089ad1f91;npm install;npm run test -- --single-run=true
No failing tests, 3 passing
答案 0 :(得分:0)
因为wallabyjs按预期工作,我花了很多时间探索业力和业力摩卡,重点关注karma.conf.js
// Karma configuration file, see link for more information
// https://karma-runner.github.io/0.13/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['mocha', 'chai', 'sinon-chai', '@angular/cli'],
plugins: [
require('karma-mocha'),
require('karma-chai'),
require('karma-sinon'),
require('karma-sinon-chai'),
require('karma-chrome-launcher'),
require('karma-phantomjs-launcher'),
require('karma-mocha-reporter'),
require('karma-coverage-istanbul-reporter'),
require('karma-istanbul-threshold'),
require('@angular/cli/plugins/karma')
],
client:{
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
files: [
{ pattern: 'node_modules/sinon/pkg/sinon.js', instrument: false },
{ pattern: 'node_modules/chai/chai.js', instrument: false },
{ pattern: 'node_modules/sinon-chai/lib/sinon-chai.js', instrument: false },
{ pattern: './src/test.ts', watched: false }
],
preprocessors: {
'./src/test.ts': ['@angular/cli']
},
mime: {
'text/x-typescript': ['ts','tsx']
},
coverageIstanbulReporter: {
reports: [ 'html', 'lcovonly', 'json' ],
fixWebpackSourcePaths: true
},
istanbulThresholdReporter: {
src: 'coverage/coverage-final.json',
reporters: ['text'],
thresholds: {
global: {
statements: 95.01,
branches: 75.59,
lines: 91.89,
functions: 89.23
},
each: {
statements: 75.76,
branches: 33.33,
lines: 75,
functions: 41.67
}
}
},
angularCli: {
environment: 'dev'
},
reporters: config.angularCli && config.angularCli.codeCoverage
? ['progress', 'coverage-istanbul', 'istanbul-threshold']
: ['progress', 'mocha'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
我采用的方法是“尝试直到它工作”的最佳实践,这转化为我逐行删除部分配置以查看它是否重要并查看它是否改变了行为。
具体来说,修复方法是从sinon-chai
数组中删除chai
和可选frameworks
。
frameworks: ['mocha', 'chai', 'sinon-chai', '@angular/cli'],
变为
frameworks: ['mocha', '@angular/cli'],
使用angular-cli / karma / mocha / angular4
可以使用此this commit在diff找到工作代码Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 0 of 3 SUCCESS (0 secs / 0 secs)
Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 1 of 3 SUCCESS (0 secs / 0.062 secs)
Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 2 of 3 SUCCESS (0 secs / 0.079 secs)
Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 3 of 3 SUCCESS (0 secs / 0.086 secs)
Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 3 of 3 SUCCESS (0.32 secs / 0.086 secs)