Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef10c96

Browse files
committed
improve karma.conf.js argument handling with minimist
- better handle test suite arguments (normalize file name) - add --bundleTest=<> argument to make testing bundle test easier to run - add --watch, --verbose, --width, --height and --info flags
1 parent 34a4611 commit ef10c96

File tree

1 file changed

+147
-54
lines changed

1 file changed

+147
-54
lines changed

test/jasmine/karma.conf.js

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

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

29101

30102
function func(config) {
@@ -71,13 +143,13 @@ func.defaultConfig = {
71143
preprocessors: {},
72144

73145
// test results reporter to use
74-
// possible values: 'dots', 'progress'
146+
// possible values: 'dots', 'progress', 'spec' and 'verbose'
75147
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
76148
//
77149
// See note in CONTRIBUTING.md about more verbose reporting via karma-verbose-reporter:
78150
// https://www.npmjs.com/package/karma-verbose-reporter ('verbose')
79151
//
80-
reporters: isSingleSuiteRun ? ['progress'] : ['dots', 'spec'],
152+
reporters: (isFullSuite && !argv.tags) ? ['dots', 'spec'] : ['progress'],
81153

82154
// web server port
83155
port: 9876,
@@ -86,38 +158,43 @@ func.defaultConfig = {
86158
colors: true,
87159

88160
// enable / disable watching file and executing tests whenever any file changes
89-
autoWatch: !isCI,
161+
autoWatch: argv.watch,
90162

91163
// if true, Karma captures browsers, runs the tests and exits
92-
singleRun: isCI,
164+
singleRun: !argv.watch,
93165

94166
// how long will Karma wait for a message from a browser before disconnecting (30 ms)
95167
browserNoActivityTimeout: 30000,
96168

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

101175
// custom browser options
102176
//
103177
// window-size values came from observing default size
104178
//
105179
// '--ignore-gpu-blacklist' allow to test WebGL on CI (!!!)
106180
customLaunchers: {
107-
Chrome_WindowSized: {
181+
_Chrome: {
108182
base: 'Chrome',
109-
flags: ['--window-size=1035,617', '--ignore-gpu-blacklist']
183+
flags: [
184+
'--window-size=' + argv.width + ',' + argv.height,
185+
isCI ? '--ignore-gpu-blacklist' : ''
186+
]
110187
},
111-
Firefox_WindowSized: {
188+
_Firefox: {
112189
base: 'Firefox',
113-
flags: ['--width=1035', '--height=617']
190+
flags: ['--width=' + argv.width, '--height=' + argv.height]
114191
}
115192
},
116193

117194
browserify: {
118-
transform: ['../../tasks/util/shortcut_paths.js'],
195+
transform: [pathToShortcutPath],
119196
extensions: ['.js'],
120-
watch: !isCI,
197+
watch: argv.watch,
121198
debug: true
122199
},
123200

@@ -140,10 +217,33 @@ func.defaultConfig = {
140217
}
141218
};
142219

143-
// Add lib/index.js to single-suite runs,
144-
// to avoid import conflicts due to plotly.js
145-
// circular dependencies.
146-
if(isSingleSuiteRun) {
220+
if(isFullSuite) {
221+
func.defaultConfig.files.push(pathToJQuery);
222+
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
223+
} else if(isBundleTest) {
224+
switch(basename(testFileGlob)) {
225+
case 'requirejs':
226+
func.defaultConfig.files = [
227+
constants.pathToRequireJS,
228+
constants.pathToRequireJSFixture
229+
];
230+
break;
231+
case 'ie9':
232+
// load ie9_mock.js before plotly.js+test bundle
233+
// to catch reference errors that could occur
234+
// when plotly.js is first loaded.
235+
func.defaultConfig.files.push(pathToIE9mock);
236+
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
237+
break;
238+
default:
239+
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
240+
break;
241+
}
242+
} else {
243+
// Add lib/index.js to non-full-suite runs,
244+
// to avoid import conflicts due to plotly.js
245+
// circular dependencies.
246+
147247
func.defaultConfig.files.push(
148248
pathToJQuery,
149249
pathToMain
@@ -152,26 +252,19 @@ if(isSingleSuiteRun) {
152252
func.defaultConfig.preprocessors[pathToMain] = ['browserify'];
153253
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
154254
}
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-
}
173255

174256
// lastly, load test file glob
175257
func.defaultConfig.files.push(testFileGlob);
176258

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

0 commit comments

Comments
 (0)