Skip to content

Commit

Permalink
expose "slow" CLI parameter and pass-through to Mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
dlockhart committed Jul 26, 2023
1 parent 3e79e64 commit 74a09d4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/server/cli/test-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function getTestRunnerOptions(argv = []) {
name: 'golden',
type: Boolean,
description: 'Generate new golden screenshots. Ignored unless group is "vdiff".',
order: 13
order: 14
},
{
name: 'grep',
Expand All @@ -83,7 +83,7 @@ async function getTestRunnerOptions(argv = []) {
name: 'help',
type: Boolean,
description: 'Print usage information and exit',
order: 14
order: 15
},
{
name: 'open',
Expand All @@ -96,6 +96,13 @@ async function getTestRunnerOptions(argv = []) {
longAlias: 'webkit',
type: Boolean
},
{
name: 'slow',
alias: 's',
type: Number,
description: 'Tests whose duration in milliseconds are at most half of this threshold are "fast" and tests which exceed it are "slow"\n[Default: 75]',
order: 13
},
{
name: 'slowmo',
type: Number,
Expand Down
13 changes: 11 additions & 2 deletions src/server/wtr-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const BROWSER_MAP = {
safari: 'webkit',
webkit: 'webkit'
};
const DEFAULT_VDIFF_SLOW = 500;

export class WTRConfig {

Expand Down Expand Up @@ -119,27 +120,35 @@ export class WTRConfig {
}).flat();
}

#getMochaConfig(timeoutConfig) {
#getMochaConfig(group, slowConfig, timeoutConfig) {
const {
timeout = timeoutConfig,
grep,
open,
slow = slowConfig,
watch
} = this.#cliArgs;

if (typeof timeout !== 'undefined' && typeof timeout !== 'number') throw new TypeError('timeout must be a number');
if (typeof slow !== 'undefined' && typeof slow !== 'number') throw new TypeError('slow must be a number');

const config = {};

if (timeout) config.timeout = String(timeout);
if (open || watch) config.timeout = '0';
if (grep) config.grep = grep;
if (slow) {
config.slow = String(slow);
} else if (group === 'vdiff') {
config.slow = String(DEFAULT_VDIFF_SLOW);
}

return Object.keys(config).length && { testFramework: { config } };
}

create({
pattern = DEFAULT_PATTERN,
slow,
timeout,
...passthroughConfig
} = {}) {
Expand All @@ -158,7 +167,7 @@ export class WTRConfig {

const config = {
...this.#defaultConfig,
...this.#getMochaConfig(timeout),
...this.#getMochaConfig(group, slow, timeout),
...passthroughConfig
};

Expand Down
5 changes: 3 additions & 2 deletions test/server/cli/test-runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ describe('runner.getOptions()', () => {
'-c', './test/browser/vdiff.config.js',
'-f', 'abc',
'-g', 'ghi',
'-t', 123]
'-t', 123,
'-s', 456]
);
expect(opts.argv).to.deep.equal(['--group', 'test']);
expect(opts.config.groups[0]).to.deep.include({
name: 'test',
files: [ 'test/browser/**/+(abc.vdiff.js)' ]
});
expect(opts.config.testFramework).to.deep.include({
config: { timeout: '123', grep: 'ghi' }
config: { timeout: '123', grep: 'ghi', slow: '456' }
});
});

Expand Down
41 changes: 41 additions & 0 deletions test/server/wtr-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,47 @@ describe('WTRConfig', () => {
});
});

it('should throw for invalid timeout value', () => {
expect(() => wtrConfig.create({ timeout: 'never' }))
.to.throw(TypeError, 'timeout must be a number');
});

it('should configure testFramework when provided with a slow value', () => {
const config = wtrConfig.create({ slow: 1234 });
expect(config.testFramework).to.not.be.undefined;
expect(config.testFramework.config).to.not.be.undefined;
expect(config.testFramework.config.slow).to.equal('1234');
});

it('should set slow from CLI when provided', () => {
const wtrConfig = new WTRConfig({ slow: 1234 });
const config = wtrConfig.create({ slow: 5678 });
expect(config.testFramework.config.slow).to.equal('1234');
});

it('should default slow to a higher value for vdiff group', () => {
const wtrConfig = new WTRConfig({ group: 'vdiff' });
const config = wtrConfig.create();
expect(config.testFramework.config.slow).to.equal('500');
});

it('should set slow from CLI even for vdiff group', () => {
const wtrConfig = new WTRConfig({ group: 'vdiff', slow: 1234 });
const config = wtrConfig.create();
expect(config.testFramework.config.slow).to.equal('1234');
});

it('should set slow from config even for vdiff group', () => {
const wtrConfig = new WTRConfig({ group: 'vdiff' });
const config = wtrConfig.create({ slow: 5678 });
expect(config.testFramework.config.slow).to.equal('5678');
});

it('should throw for invalid slow value', () => {
expect(() => wtrConfig.create({ slow: 'fast' }))
.to.throw(TypeError, 'slow must be a number');
});

it('should set a common default files pattern', () => {
expect(config.groups[0].files).to.deep.equal(['./test/**/*.test.js']);
});
Expand Down

0 comments on commit 74a09d4

Please sign in to comment.