Skip to content

Commit

Permalink
Reorganize and fix/add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bearfriend committed Jul 19, 2023
1 parent 4a4e4ed commit 0829a96
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 228 deletions.
152 changes: 4 additions & 148 deletions bin/test.js
Original file line number Diff line number Diff line change
@@ -1,151 +1,7 @@
#!/usr/bin/env node
import { ConfigLoaderError, readConfig } from '@web/config-loader';
import commandLineArgs from 'command-line-args';
import commandLineUsage from 'command-line-usage';
import process from 'node:process';
import { startTestRunner } from '@web/test-runner';
import { WTRConfig } from '../src/server/wtr-config.js';
import { argv } from 'node:process';
import { runner } from '../src/server/cli/test.js';

const DISALLOWED_OPTIONS = ['--browsers', '--playwright', '--puppeteer', '--groups'];
const options = await runner.getOptions(argv);

const optionDefinitions = [
// @web/test-runner options
{
name: 'files',
type: String,
multiple: true,
description: 'Test files to run. Path or glob.\n[Default: ./test/**/*.<group>.js]',
order: 8
},
{
name: 'group',
type: String,
required: true,
defaultOption: true,
description: 'Name of the group to run tests for\n[Default: test]',
order: 1
},
{
name: 'manual',
type: Boolean,
description: 'Starts test runner in manual testing mode. Ignores browser options and prints manual testing URL.\n{underline Not compatible with automated browser interactions}\nConsider using --watch to debug in the browser instead',
order: 11
},
{
name: 'watch',
type: Boolean,
description: 'Reload tests on file changes. Allows debugging in all browsers.',
order: 9
},

// d2l-test options
{
name: 'chrome',
type: Boolean,
description: 'Run tests in Chromium',
order: 2
},
{
name: 'config',
alias: 'c',
type: String,
description: 'Location to read config file from\n[Default: ./d2l-test.config.js]',
order: 9
},
{
name: 'filter',
alias: 'f',
type: String,
multiple: true,
description: 'Filter test files by replacing wildcards with this glob',
order: 6
},
{
name: 'firefox',
type: Boolean,
description: 'Run tests in Firefox',
order: 3
},
{
name: 'golden',
type: Boolean,
description: 'Generate new golden screenshots',
order: 10
},
{
name: 'grep',
alias: 'g',
type: String,
description: 'Only run tests matching this string or regexp',
order: 7
},
{
name: 'help',
type: Boolean,
description: 'Print usage information and exit',
order: 12
},
{
name: 'safari',
type: Boolean,
description: 'Run tests in Webkit',
order: 4
},
{
name: 'timeout',
alias: 't',
type: Number,
description: 'Test timeout threshold in ms\n[Default: 2000]',
order: 5
},
];

const cliArgs = commandLineArgs(optionDefinitions, { partial: true });

if (cliArgs.help) {
const help = commandLineUsage([
{
header: 'D2L Test',
content: 'Test runner for D2L components and applications'
},
{
header: 'Usage',
content: 'd2l-test [options]',
},
{
header: 'Options',
optionList: optionDefinitions
.map(o => (o.description += '\n') && o)
.sort((a, b) => (a.order > b.order ? 1 : -1))
}
]);
process.stdout.write(help);
process.exit();
}

cliArgs._unknown = cliArgs._unknown?.filter(o => !DISALLOWED_OPTIONS.includes(o));

const testConfig = await readConfig('d2l-test.config', cliArgs.config).catch(err => {
if (err instanceof ConfigLoaderError) {
throw new Error(err.message);
} else {
throw err;
}
}) || {};

const wtrConfig = new WTRConfig(cliArgs);
const config = wtrConfig.create(testConfig);

const argv = [
'--group', cliArgs.group,
...(cliArgs._unknown || [])
];
// copy cli-only wtr options back to argv to be processed
cliArgs.watch && argv.push('--watch');
cliArgs.manual && argv.push('--manual');

await startTestRunner({
argv,
config,
readFileConfig: false
});
await runner.start(options);
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"scripts": {
"lint": "eslint . --ext .js",
"start": "web-dev-server --root-dir ./.vdiff --open ./.report/",
"test": "npm run lint && npm run test:server && npm run test:browser",
"test:browser": "web-test-runner --files \"./test/browser/**/*.test.js\" --node-resolve --playwright",
"test:server": "mocha ./test/server/**/*.test.js",
"test": "npm run lint && npm run test:bin && npm run test:server && npm run test:browser",
"test:bin": "mocha './test/bin/**/*.test.js'",
"test:browser": "web-test-runner --files './test/browser/**/*.test.js' --node-resolve --playwright",
"test:server": "mocha './test/server/**/*.test.js'",
"test:vdiff": "d2l-test --config ./test/browser/d2l-test.config.js --group vdiff",
"test:vdiff:golden": "npm run test:vdiff -- --golden"
},
Expand Down
159 changes: 159 additions & 0 deletions src/server/cli/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { ConfigLoaderError, readConfig } from '@web/config-loader';
import commandLineArgs from 'command-line-args';
import commandLineUsage from 'command-line-usage';
import process from 'node:process';
import { startTestRunner } from '@web/test-runner';
import { WTRConfig } from '../wtr-config.js';

async function getTestRunnerOptions(argv = []) {

const DISALLOWED_OPTIONS = ['--browsers', '--playwright', '--puppeteer', '--groups'];

const optionDefinitions = [
// @web/test-runner options
{
name: 'files',
type: String,
multiple: true,
description: 'Test files to run. Path or glob.\n[Default: ./test/**/*.<group>.js]',
order: 8
},
{
name: 'group',
type: String,
required: true,
defaultOption: true,
description: 'Name of the group to run tests for\n[Default: test]',
order: 1
},
{
name: 'manual',
type: Boolean,
description: 'Starts test runner in manual testing mode. Ignores browser options and prints manual testing URL.\n{underline Not compatible with automated browser interactions}\nConsider using --watch to debug in the browser instead',
order: 11
},
{
name: 'watch',
type: Boolean,
description: 'Reload tests on file changes. Allows debugging in all browsers.',
order: 9
},

// d2l-test options
{
name: 'chrome',
type: Boolean,
description: 'Run tests in Chromium',
order: 2
},
{
name: 'config',
alias: 'c',
type: String,
description: 'Location to read config file from\n[Default: ./d2l-test.config.js]',
order: 9
},
{
name: 'filter',
alias: 'f',
type: String,
multiple: true,
description: 'Filter test files by replacing wildcards with this glob',
order: 6
},
{
name: 'firefox',
type: Boolean,
description: 'Run tests in Firefox',
order: 3
},
{
name: 'golden',
type: Boolean,
description: 'Generate new golden screenshots',
order: 10
},
{
name: 'grep',
alias: 'g',
type: String,
description: 'Only run tests matching this string or regexp',
order: 7
},
{
name: 'help',
type: Boolean,
description: 'Print usage information and exit',
order: 12
},
{
name: 'safari',
type: Boolean,
description: 'Run tests in Webkit',
order: 4
},
{
name: 'timeout',
alias: 't',
type: Number,
description: 'Test timeout threshold in ms\n[Default: 2000]',
order: 5
},
];

const cliArgs = commandLineArgs(optionDefinitions, { partial: true, argv });

if (cliArgs.help) {
const help = commandLineUsage([
{
header: 'D2L Test',
content: 'Test runner for D2L components and applications'
},
{
header: 'Usage',
content: 'd2l-test [options]',
},
{
header: 'Options',
optionList: optionDefinitions
.map(o => (o.description += '\n') && o)
.filter(o => 'order' in o)
.sort((a, b) => (a.order > b.order ? 1 : -1))
}
]);
process.stdout.write(help);
process.exit();
}

cliArgs._unknown = cliArgs._unknown?.filter(o => !DISALLOWED_OPTIONS.includes(o));

const testConfig = await readConfig('d2l-test.config', cliArgs.config).catch(err => {
if (err instanceof ConfigLoaderError) {
throw new Error(err.message);
} else {
throw err;
}
}) || {};

const wtrConfig = new WTRConfig(cliArgs);
const config = wtrConfig.create(testConfig);

argv = [
'--group', cliArgs.group,
...(cliArgs._unknown || [])
];
// copy cli-only wtr options back to argv to be processed
cliArgs.watch && argv.push('--watch');
cliArgs.manual && argv.push('--manual');

return {
argv,
config,
readFileConfig: false
};
}

export const runner = {
getOptions: getTestRunnerOptions,
start: startTestRunner
};
4 changes: 3 additions & 1 deletion src/server/wtr-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ export class WTRConfig {
timeout,
...passthroughConfig
} = {}) {

const { files, filter, golden, grep, group, manual, watch } = this.#cliArgs;
const passthroughGroupNames = passthroughConfig.groups?.map(g => g.name) ?? [];

if (!['test', 'vdiff', ...passthroughConfig.groups.map(g => g.name)].includes(group)) {
if (!['test', 'vdiff', ...passthroughGroupNames].includes(group)) {
return {}; // allow wtr to error
}

Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions test/bin/test.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { assert, restore, stub } from 'sinon';
import { argv } from 'node:process';
import { runner } from '../../src/server/cli/test.js';

describe('d2l-test', () => {

it('starts test runner with options', async() => {
const opts = { my: 'options' };
const optionsStub = stub(runner, 'getOptions').returns(opts);
const startStub = stub(runner, 'start');
await import('../../bin/test.js');

assert.calledOnceWithExactly(optionsStub, argv);
assert.calledOnceWithExactly(startStub, opts);

restore();
});
});
Loading

0 comments on commit 0829a96

Please sign in to comment.