Reputation: 144
I've converted a basic project generated by angular-cli 1.0.0 from using jasmine to using mocha. Why as I complete the conversion to mocha, do all the tests run twice? Interestingly when we run the same tests/code with wallabyjs runner, or with a downgraded angular2, then the tests only run once.
To reproduce from scratch I took 3 steps with an optional 4th. Unfortunately there is a lot of code involved, so I've provided a repository to demonstrate and a command to follow along each step which can be found in the step detail.
ng new someBlankProject
cd someBlankProject
ng test
expected result 3 tests rungit clone [email protected]: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
to resolve dependenciesng test
expected result 3 tests runNo failing tests, 3 passing
git clone [email protected]: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
to clean and resolve dependenciesng test
expected result 3 tests run actual result 6 tests run
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 [email protected]: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
One of the strangest things that I've observed is that using angular 2 does not have the same behavior!
npm prune;npm install
to clean and resolve dependenciesng test
expected result 3 tests runNo failing tests, 3 passing
git clone [email protected]: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
Upvotes: 0
Views: 1905
Reputation: 144
Because wallabyjs is working as expected, I've spent much of my time exploring karma and karma-mocha with a focus on the 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
});
};
The approach I took was the best practice of just "try stuff until it works", which translates to me removing parts of the configuration line by line to see if it was important and to see if it changed the behavior.
Specifically the fix was removing sinon-chai
and optionally chai
from the frameworks
array.
frameworks: ['mocha', 'chai', 'sinon-chai', '@angular/cli'],
becomes
frameworks: ['mocha', '@angular/cli'],
The working code can be found at this commit with this diff using angular-cli / karma / mocha / angular4
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)
Upvotes: 1