forked from mrsone40/vets-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-unit-test.js
More file actions
77 lines (66 loc) · 2.63 KB
/
run-unit-test.js
File metadata and controls
77 lines (66 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* eslint-disable no-console */
const commandLineArgs = require('command-line-args');
const glob = require('glob');
const printUnitTestHelp = require('./run-unit-test-help');
const { runCommand } = require('./utils');
// For usage instructions see https://github.com/department-of-veterans-affairs/vets-website#unit-tests
const specDirs = '{src,script}';
const defaultPath = `./${specDirs}/**/*.unit.spec.js?(x)`;
const COMMAND_LINE_OPTIONS_DEFINITIONS = [
{ name: 'log-level', type: String, defaultValue: 'log' },
{ name: 'app-folder', type: String, defaultValue: null },
{ name: 'coverage', type: Boolean, defaultValue: false },
{ name: 'coverage-html', type: Boolean, defaultValue: false },
{ name: 'reporter', type: String, defaultValue: null },
{ name: 'help', alias: 'h', type: Boolean, defaultValue: false },
{ name: 'config', type: String, defaultValue: null },
{
name: 'path',
type: String,
defaultOption: true,
multiple: true,
defaultValue: [defaultPath],
},
];
const options = commandLineArgs(COMMAND_LINE_OPTIONS_DEFINITIONS);
let coverageInclude = '';
if (
options['app-folder'] &&
options.path[0] === defaultPath &&
options.path.length === 1
) {
options.path[0] = options.path[0].replace(
`/${specDirs}/`,
`/src/applications/${options['app-folder']}/`,
);
const unitTestList = glob.sync(options.path[0]);
if (!unitTestList.length) {
console.log('There are no unit tests in the app folder.');
process.exit(0);
}
coverageInclude = `--include 'src/applications/${options['app-folder']}/**'`;
}
const reporterOption = options.reporter ? `--reporter ${options.reporter}` : '';
if (options.help) {
printUnitTestHelp();
process.exit(0);
}
const mochaPath = `BABEL_ENV=test NODE_ENV=test mocha ${reporterOption}`;
const coverageReporter = options['coverage-html']
? '--reporter=html mocha --retries 5'
: '--reporter=json-summary mocha --reporter mocha-multi-reporters --reporter-options configFile=config/mocha-multi-reporter.js --no-color --retries 5';
const coveragePath = `NODE_ENV=test nyc --all ${coverageInclude} ${coverageReporter}`;
const testRunner = options.coverage ? coveragePath : mochaPath;
const configFile = options.config ? options.config : 'config/mocha.json';
let testsToVerify = null;
if (
process.env.TESTS_TO_VERIFY &&
process.env.GITHUB_REF !== 'refs/heads/main'
) {
testsToVerify = JSON.parse(process.env.TESTS_TO_VERIFY).join(' ');
}
const command = `LOG_LEVEL=${options[
'log-level'
].toLowerCase()} ${testRunner} --max-old-space-size=4096 --config ${configFile} ${testsToVerify ||
`--recursive ${options.path.map(p => `'${p}'`).join(' ')}`}`;
runCommand(command);