Skip to content

Commit 7f07d51

Browse files
authored
Merge pull request #1524 from etpinard/karma-conf-minimist
Better test-jasmine karma.conf.js with minimist
2 parents 34a4611 + 7cf9943 commit 7f07d51

File tree

5 files changed

+185
-62
lines changed

5 files changed

+185
-62
lines changed

CONTRIBUTING.md

+33-4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ Both jasmine and image tests are run on
104104
[CircleCI](https://circleci.com/gh/plotly/plotly.js) on every push to this
105105
repo.
106106

107+
### Jasmine tests
108+
107109
Jasmine tests are run in a browser using
108110
[karma](https://github.com/karma-runner/karma). To run them locally:
109111

@@ -114,17 +116,44 @@ npm run test-jasmine
114116
To run a specific suite, use:
115117

116118
```
117-
npm run test-jasmine -- tests/<suite>.js
119+
npm run test-jasmine -- <suite>
120+
```
121+
122+
where the `<suite>` corresponds to the suite's file name as found in
123+
[`test/jasmine/tests/`](https://github.com/plotly/plotly.js/tree/master/test/jasmine/tests).
124+
125+
You can also test multiple suites at a time, for example:
126+
127+
```
128+
npm run test-jasmine -- bar axes scatter
129+
```
130+
131+
which will run tests in the `bar_test.js`, `axes_test.js` and `scatter_test.js`
132+
suites.
133+
134+
To turn off the `autoWatch` / auto-bundle / multiple run mode:
135+
136+
```
137+
npm run test-jasmine -- <suite> --nowatch
118138
```
119139

120-
where the `<suite>` corresponds to the suite's file name as found in [`test/jasmine/tests/`](https://github.com/plotly/plotly.js/tree/master/test/jasmine/tests). In certain situations, you may find that the default reporting is not verbose enough to pin down the source of the failing test. In this situation, you may wish to use [karma-verbose-reporter](https://www.npmjs.com/package/karma-verbose-reporter). You can use it without adding as a dev dependency by running:
140+
In certain situations, you may find that the default reporting is not verbose
141+
enough to pin down the source of the failing test. In this situation, you may
142+
wish to use
143+
[karma-verbose-reporter](https://www.npmjs.com/package/karma-verbose-reporter):
121144

122145
```
123-
npm install karma-verbose-reporter
146+
npm run test-jasmine -- <suite> --verbose
124147
```
125148

126-
and adding `reporters: ['verbose']` to the corresponding karma configuration file. (You should disable the `progress` reporter when using `verbose`.)
149+
For more info on the karma / jasmine CLI:
150+
151+
```
152+
npm run test-jasmine -- --help
153+
npm run test-jasmine -- --info
154+
```
127155

156+
### Image pixel comparison tests
128157

129158
Image pixel comparison tests are run in a docker container. For more
130159
information on how to run them locally, please refer to [image test

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@
3333
"docker": "node tasks/docker.js",
3434
"pretest": "node tasks/pretest.js",
3535
"test-jasmine": "karma start test/jasmine/karma.conf.js",
36-
"citest-jasmine": "CIRCLECI=1 karma start test/jasmine/karma.conf.js",
3736
"test-image": "node tasks/test_image.js",
3837
"test-image-gl2d": "node tasks/test_image.js gl2d_* --queue",
3938
"test-export": "node tasks/test_export.js",
4039
"test-syntax": "node tasks/test_syntax.js",
4140
"test-bundle": "node tasks/test_bundle.js",
42-
"test": "npm run citest-jasmine && npm run test-image && npm run test-image-gl2d && npm run test-syntax && npm run test-bundle",
41+
"test": "npm run test-jasmine && npm run test-bundle && npm run test-image && npm run test-image-gl2d && npm run test-syntax && npm run lint",
4342
"start-test_dashboard": "node devtools/test_dashboard/server.js",
4443
"start-image_viewer": "node devtools/image_viewer/server.js",
4544
"start": "npm run start-test_dashboard",
@@ -124,6 +123,7 @@
124123
"karma-spec-reporter": "0.0.30",
125124
"karma-verbose-reporter": "0.0.6",
126125
"madge": "^1.6.0",
126+
"minimist": "^1.2.0",
127127
"node-sass": "^4.5.0",
128128
"npm-link-check": "^1.2.0",
129129
"open": "0.0.5",

tasks/noci_test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ EXIT_STATE=0
55
# tests that aren't run on CI
66

77
# jasmine specs with @noCI tag
8-
npm run citest-jasmine -- tests/*_test.js --tags noCI || EXIT_STATE=$?
8+
npm run test-jasmine -- --tags=noCI --nowatch || EXIT_STATE=$?
99

1010
# mapbox image tests take too much resources on CI
1111
npm run test-image -- mapbox_* || EXIT_STATE=$?

tasks/test_bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var pathToJasmineBundleTests = path.join(constants.pathToJasmineBundleTests);
99
glob(pathToJasmineBundleTests + '/*.js', function(err, files) {
1010
files.forEach(function(file) {
1111
var baseName = path.basename(file);
12-
var cmd = 'npm run citest-jasmine -- bundle_tests/' + baseName;
12+
var cmd = 'npm run test-jasmine -- --bundleTest=' + baseName;
1313

1414
common.execCmd(cmd);
1515
});

test/jasmine/karma.conf.js

+148-54
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,103 @@
11
/* eslint-env node*/
22

3-
// Karma configuration
4-
5-
/*
6-
* Test file globs can be passed with an argument.
7-
*
8-
* Example:
9-
*
10-
* $ npm run test-jasmine -- tests/axes_test.js
11-
*
12-
* will only run the tests in axes_test.js
13-
*
14-
*/
15-
3+
var path = require('path');
4+
var minimist = require('minimist');
165
var constants = require('../../tasks/util/constants');
176

18-
var arg = process.argv[4];
19-
207
var isCI = !!process.env.CIRCLECI;
21-
var testFileGlob = arg ? arg : 'tests/*_test.js';
22-
var isSingleSuiteRun = (arg && arg.indexOf('bundle_tests/') === -1);
23-
var isRequireJSTest = (arg && arg.indexOf('bundle_tests/requirejs') !== -1);
24-
var isIE9Test = (arg && arg.indexOf('bundle_tests/ie9') !== -1);
8+
var argv = minimist(process.argv.slice(4), {
9+
string: ['bundleTest', 'width', 'height'],
10+
'boolean': ['info', 'nowatch', 'verbose', 'Chrome', 'Firefox'],
11+
alias: {
12+
'Chrome': 'chrome',
13+
'Firefox': ['firefox', 'FF'],
14+
'bundleTest': ['bundletest', 'bundle_test'],
15+
'nowatch': 'no-watch'
16+
},
17+
'default': {
18+
info: false,
19+
nowatch: isCI,
20+
verbose: false,
21+
width: '1035',
22+
height: '617'
23+
}
24+
});
25+
26+
if(argv.info) {
27+
console.log([
28+
'plotly.js karma runner for jasmine tests CLI info',
29+
'',
30+
'Examples:',
31+
'',
32+
'Run `axes_test.js`, `bar_test.js` and `scatter_test.js` suites w/o `autoWatch`:',
33+
' $ npm run test-jasmine -- axes bar_test.js scatter --nowatch',
34+
'',
35+
'Run all tests with the `noCI` tag on Firefox in a 1500px wide window:',
36+
' $ npm run test-jasmine -- --tags=noCI --FF --width=1500',
37+
'',
38+
'Run the `ie9_test.js` bundle test with the verbose reporter:',
39+
' $ npm run test-jasmine -- --bundleTest=ie9 --verbose',
40+
'',
41+
'Arguments:',
42+
' - All non-flagged arguments corresponds to the test suites in `test/jasmine/tests/` to be run.',
43+
' No need to add the `_test.js` suffix, we expand them correctly here.',
44+
' - `--bundleTest` set the bundle test suite `test/jasmine/bundle_tests/ to be run.',
45+
' Note that only one bundle test can be run at a time.',
46+
'',
47+
'Other options:',
48+
' - `--info`: show this info message',
49+
' - `--Chrome` (alias `--chrome`): run test in (our custom) Chrome browser',
50+
' - `--Firefox` (alias `--FF`, `--firefox`): run test in (our custom) Firefox browser',
51+
' - `--nowatch (dflt: `false`, `true` on CI)`: run karma w/o `autoWatch` / multiple run mode',
52+
' - `--verbose` (dflt: `false`): show test result using verbose reporter',
53+
' - `--tags`: run only test with given tags (using the `jasmine-spec-tags` framework)',
54+
' - `--width`(dflt: 1035): set width of the browser window',
55+
' - `--height` (dflt: 617): set height of the browser window',
56+
'',
57+
'For info on the karma CLI options run `npm run test-jasmine -- --help`'
58+
].join('\n'));
59+
process.exit(0);
60+
}
61+
62+
var SUFFIX = '_test.js';
63+
var basename = function(s) { return path.basename(s, SUFFIX); };
64+
var merge = function(_) {
65+
var list = [];
66+
67+
(Array.isArray(_) ? _ : [_]).forEach(function(p) {
68+
list = list.concat(p.split(','));
69+
});
70+
71+
return list;
72+
};
73+
var glob = function(_) {
74+
return _.length === 1 ?
75+
_[0] + SUFFIX :
76+
'{' + _.join(',') + '}' + SUFFIX;
77+
};
78+
79+
var isBundleTest = !!argv.bundleTest;
80+
var isFullSuite = !isBundleTest && argv._.length === 0;
81+
var testFileGlob;
82+
83+
if(isFullSuite) {
84+
testFileGlob = path.join('tests', '*' + SUFFIX);
85+
} else if(isBundleTest) {
86+
var _ = merge(argv.bundleTest);
87+
88+
if(_.length > 1) {
89+
console.warn('Can only run one bundle test suite at a time, ignoring ', _.slice(1));
90+
}
91+
92+
testFileGlob = path.join('bundle_tests', glob([basename(_[0])]));
93+
} else {
94+
testFileGlob = path.join('tests', glob(merge(argv._).map(basename)));
95+
}
2596

26-
var pathToMain = '../../lib/index.js';
27-
var pathToJQuery = 'assets/jquery-1.8.3.min.js';
97+
var pathToShortcutPath = path.join(__dirname, '..', '..', 'tasks', 'util', 'shortcut_paths.js');
98+
var pathToMain = path.join(__dirname, '..', '..', 'lib', 'index.js');
99+
var pathToJQuery = path.join(__dirname, 'assets', 'jquery-1.8.3.min.js');
100+
var pathToIE9mock = path.join(__dirname, 'assets', 'ie9_mock.js');
28101

29102

30103
function func(config) {
@@ -71,13 +144,13 @@ func.defaultConfig = {
71144
preprocessors: {},
72145

73146
// test results reporter to use
74-
// possible values: 'dots', 'progress'
147+
// possible values: 'dots', 'progress', 'spec' and 'verbose'
75148
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
76149
//
77150
// See note in CONTRIBUTING.md about more verbose reporting via karma-verbose-reporter:
78151
// https://www.npmjs.com/package/karma-verbose-reporter ('verbose')
79152
//
80-
reporters: isSingleSuiteRun ? ['progress'] : ['dots', 'spec'],
153+
reporters: (isFullSuite && !argv.tags) ? ['dots', 'spec'] : ['progress'],
81154

82155
// web server port
83156
port: 9876,
@@ -86,38 +159,43 @@ func.defaultConfig = {
86159
colors: true,
87160

88161
// enable / disable watching file and executing tests whenever any file changes
89-
autoWatch: !isCI,
162+
autoWatch: !argv.nowatch,
90163

91164
// if true, Karma captures browsers, runs the tests and exits
92-
singleRun: isCI,
165+
singleRun: argv.nowatch,
93166

94167
// how long will Karma wait for a message from a browser before disconnecting (30 ms)
95168
browserNoActivityTimeout: 30000,
96169

97170
// start these browsers
98171
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
99-
browsers: ['Chrome_WindowSized'],
172+
//
173+
// N.B. this field is filled below
174+
browsers: [],
100175

101176
// custom browser options
102177
//
103178
// window-size values came from observing default size
104179
//
105180
// '--ignore-gpu-blacklist' allow to test WebGL on CI (!!!)
106181
customLaunchers: {
107-
Chrome_WindowSized: {
182+
_Chrome: {
108183
base: 'Chrome',
109-
flags: ['--window-size=1035,617', '--ignore-gpu-blacklist']
184+
flags: [
185+
'--window-size=' + argv.width + ',' + argv.height,
186+
isCI ? '--ignore-gpu-blacklist' : ''
187+
]
110188
},
111-
Firefox_WindowSized: {
189+
_Firefox: {
112190
base: 'Firefox',
113-
flags: ['--width=1035', '--height=617']
191+
flags: ['--width=' + argv.width, '--height=' + argv.height]
114192
}
115193
},
116194

117195
browserify: {
118-
transform: ['../../tasks/util/shortcut_paths.js'],
196+
transform: [pathToShortcutPath],
119197
extensions: ['.js'],
120-
watch: !isCI,
198+
watch: !argv.nowatch,
121199
debug: true
122200
},
123201

@@ -140,10 +218,33 @@ func.defaultConfig = {
140218
}
141219
};
142220

143-
// Add lib/index.js to single-suite runs,
144-
// to avoid import conflicts due to plotly.js
145-
// circular dependencies.
146-
if(isSingleSuiteRun) {
221+
if(isFullSuite) {
222+
func.defaultConfig.files.push(pathToJQuery);
223+
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
224+
} else if(isBundleTest) {
225+
switch(basename(testFileGlob)) {
226+
case 'requirejs':
227+
func.defaultConfig.files = [
228+
constants.pathToRequireJS,
229+
constants.pathToRequireJSFixture
230+
];
231+
break;
232+
case 'ie9':
233+
// load ie9_mock.js before plotly.js+test bundle
234+
// to catch reference errors that could occur
235+
// when plotly.js is first loaded.
236+
func.defaultConfig.files.push(pathToIE9mock);
237+
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
238+
break;
239+
default:
240+
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
241+
break;
242+
}
243+
} else {
244+
// Add lib/index.js to non-full-suite runs,
245+
// to avoid import conflicts due to plotly.js
246+
// circular dependencies.
247+
147248
func.defaultConfig.files.push(
148249
pathToJQuery,
149250
pathToMain
@@ -152,26 +253,19 @@ if(isSingleSuiteRun) {
152253
func.defaultConfig.preprocessors[pathToMain] = ['browserify'];
153254
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
154255
}
155-
else if(isRequireJSTest) {
156-
func.defaultConfig.files = [
157-
constants.pathToRequireJS,
158-
constants.pathToRequireJSFixture
159-
];
160-
}
161-
else if(isIE9Test) {
162-
// load ie9_mock.js before plotly.js+test bundle
163-
// to catch reference errors that could occur
164-
// when plotly.js is first loaded.
165-
166-
func.defaultConfig.files.push('./assets/ie9_mock.js');
167-
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
168-
}
169-
else {
170-
func.defaultConfig.files.push(pathToJQuery);
171-
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
172-
}
173256

174257
// lastly, load test file glob
175258
func.defaultConfig.files.push(testFileGlob);
176259

260+
// add browsers
261+
var browsers = func.defaultConfig.browsers;
262+
if(argv.Chrome) browsers.push('_Chrome');
263+
if(argv.Firefox) browsers.push('_Firefox');
264+
if(browsers.length === 0) browsers.push('_Chrome');
265+
266+
// add verbose reporter if specified
267+
if(argv.verbose) {
268+
func.defaultConfig.reporters.push('verbose');
269+
}
270+
177271
module.exports = func;

0 commit comments

Comments
 (0)