Skip to content

Commit eea01ba

Browse files
committed
CLI: Fix --seed <file> arg confusion, Backport --seed=true support
* Fix `--seed <file>` arg confusion. This is now correctly interpreted as a file. * Fix presence of "Running tests with seed:" message on CLI when preconfig qunit_config_seed is set via environment variable. Previously the seed was applied, but not printed because the printing logic only checked the CLI options, not QUnit.config. * Add `--seed=true` to generate a new seed. This is backported from QUnit 3.0.0-alpha, and aligns the CLI with what was already supported via Preconfig, QUnit.config, and URL params. Cherry-picked from 390a5b7 (3.0.0-dev). > Ref #1691.
1 parent da0c59e commit eea01ba

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

bin/qunit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ program
3131
'reporters will be displayed')
3232
.option('--require <module>', 'specify a module or script to include before running ' +
3333
'any tests.', collect, [])
34-
.option('--seed [value]', 'specify a seed to re-order your tests; ' +
35-
'if specified without a value, a seed will be generated')
34+
.option('--seed <value>', 'specify a seed to re-order your tests; ' +
35+
'set to "true" to generate a new seed')
3636
.option('-w, --watch', 'watch files for changes and re-run the test suite')
3737
.parse(process.argv);
3838

src/cli/run.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ async function run (args, options) {
5555

5656
const seed = options.seed;
5757
if (seed) {
58-
if (seed === true) {
59-
QUnit.config.seed = Math.random().toString(36).slice(2);
58+
if (seed === 'true' || seed === true) {
59+
// NOTE: QUnit 3 will set preconfig qunit_config_seed from here.
60+
// NOTE: This duplicates logic from /src/core/config.js. Consolidated in QUnit 3.
61+
QUnit.config.seed = (Math.random().toString(36) + '0000000000').slice(2, 12);
6062
} else {
6163
QUnit.config.seed = seed;
6264
}
63-
65+
}
66+
if (QUnit.config.seed) {
6467
console.log(`Running tests with seed: ${QUnit.config.seed}`);
6568
}
6669

src/core/config.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ function readFlatPreconfigString (val, dest) {
156156
}
157157
}
158158

159+
function readFlatPreconfigStringOrBoolean (val, dest) {
160+
if (typeof val === 'boolean' || (typeof val === 'string' && val !== '')) {
161+
config[dest] = val;
162+
}
163+
}
164+
159165
function readFlatPreconfigStringArray (val, dest) {
160166
if (typeof val === 'string' && val !== '') {
161167
config[dest] = [val];
@@ -178,7 +184,7 @@ function readFlatPreconfig (obj) {
178184
readFlatPreconfigBoolean(obj.qunit_config_reorder, 'reorder');
179185
readFlatPreconfigBoolean(obj.qunit_config_requireexpects, 'requireExpects');
180186
readFlatPreconfigBoolean(obj.qunit_config_scrolltop, 'scrolltop');
181-
readFlatPreconfigString(obj.qunit_config_seed, 'seed');
187+
readFlatPreconfigStringOrBoolean(obj.qunit_config_seed, 'seed');
182188
readFlatPreconfigStringArray(obj.qunit_config_testid, 'testId');
183189
readFlatPreconfigNumber(obj.qunit_config_testtimeout, 'testTimeout');
184190
}
@@ -200,4 +206,10 @@ if (preConfig) {
200206
// Push a loose unnamed module to the modules collection
201207
config.modules.push(config.currentModule);
202208

209+
if (config.seed === 'true' || config.seed === true) {
210+
// Generate a random seed
211+
// Length of `Math.random()` fraction, in base 36, may vary from 6-14.
212+
// Pad and take slice to a consistent 10-digit value.
213+
config.seed = (Math.random().toString(36) + '0000000000').slice(2, 12);
214+
}
203215
export default config;

src/html-runner/urlparams.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ import { window } from '../globals';
2626
QUnit.config.testId = [].concat(urlParams.testId || []);
2727

2828
// Test order randomization
29-
if (urlParams.seed === true) {
30-
// Generate a random seed if the option is specified without a value
31-
QUnit.config.seed = Math.random().toString(36).slice(2);
29+
// Generate a random seed if `?seed` is specified without a value (boolean true),
30+
// or when set to the string "true".
31+
if (urlParams.seed === 'true' || urlParams.seed === true) {
32+
// NOTE: This duplicates logic from /src/core/config.js. Consolidated in QUnit 3.
33+
QUnit.config.seed = (Math.random().toString(36) + '0000000000').slice(2, 12);
3234
} else if (urlParams.seed) {
3335
QUnit.config.seed = urlParams.seed;
3436
}

test/cli/cli-main.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ QUnit.module('CLI Main', () => {
3131
qunit_config_testtimeout: '7'
3232
}
3333
});
34-
assert.equal(execution.snapshot, `TAP version 13
34+
assert.equal(execution.snapshot, `Running tests with seed: dummyfirstyes
35+
TAP version 13
3536
ok 1 dummy
3637
not ok 2 slow
3738
---
@@ -367,6 +368,38 @@ not ok 1 global failure
367368
# exit code: 1`);
368369
});
369370

371+
QUnit.test('--seed=true generates new random seed', async assert => {
372+
const command = ['qunit', '--seed', 'true', 'basic-one.js', 'test/'];
373+
const execution = await execute(command);
374+
375+
const actualHarness = execution.snapshot
376+
.replace(/^(Running tests with seed: )([a-z0-9]{10,20})/g, (_m, m1) => {
377+
return m1 + '0000000000';
378+
})
379+
.split('\n')
380+
.filter(line => !line.startsWith('ok '))
381+
.join('\n');
382+
383+
const actualResults = execution.snapshot
384+
.replace(/^ok \d/gm, 'ok 0')
385+
.split('\n')
386+
.filter(line => line.startsWith('ok '))
387+
.sort()
388+
.join('\n');
389+
390+
assert.equal(actualHarness, `Running tests with seed: 0000000000
391+
TAP version 13
392+
1..3
393+
# pass 3
394+
# skip 0
395+
# todo 0
396+
# fail 0`);
397+
398+
assert.equal(actualResults, `ok 0 First > 1
399+
ok 0 Second > 1
400+
ok 0 Single > has a test`);
401+
});
402+
370403
QUnit.test('--require loads unknown module', async assert => {
371404
const command = ['qunit', 'basic-one.js', '--require', 'does-not-exist-at-all'];
372405
const execution = await execute(command);

0 commit comments

Comments
 (0)