Skip to content

Commit 082c407

Browse files
authored
refactor: Simplify test-runner logic (#196)
1 parent 2c906c7 commit 082c407

File tree

13 files changed

+129
-234
lines changed

13 files changed

+129
-234
lines changed

bin/sfdx-lwc-jest

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ const { error } = require('../src/log');
1313

1414
const args = getArgs();
1515
const runJest = require('../src/utils/test-runner');
16-
runJest(args);
1716

18-
process.on('unhandledRejection', reason => {
17+
runJest(args)
18+
.catch((err) => {
19+
error(err);
20+
return 1;
21+
})
22+
.then((code) => {
23+
process.exit(code);
24+
});
25+
26+
process.on('unhandledRejection', (reason) => {
1927
error(reason);
2028
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"scripts": {
1818
"check-license-headers": "node ./scripts/checkLicenseHeaders.js",
19-
"lint": "eslint src/",
19+
"lint": "eslint src/ tests/",
2020
"format": "prettier --write '**/*.{js,json,md,html,css}'",
2121
"format:check": "prettier --check '**/*.{js,json,md,html,css}'",
2222
"release": "npm publish --access public",

src/config.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,4 @@ const jestConfig = {
3737

3838
const expectedApiVersion = '50.0';
3939

40-
const jestPath = (() => {
41-
const packageJsonPath = require.resolve('jest/package.json');
42-
const { bin } = require(packageJsonPath);
43-
44-
return path.resolve(path.dirname(packageJsonPath), bin);
45-
})();
46-
47-
module.exports = { jestConfig, jestPath, expectedApiVersion };
40+
module.exports = { jestConfig, expectedApiVersion };

src/log.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ function info(message) {
1414
console.log(`${chalk.blue('info')} ${message}`);
1515
}
1616

17-
function error(message, code = 1) {
17+
function error(message) {
1818
console.error(`${chalk.red('error')} ${message}`);
19-
process.exit(code);
2019
}
2120

2221
module.exports = {

src/resolver.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ const fs = require('fs');
1010
const path = require('path');
1111
const lwcResolver = require('@lwc/jest-resolver');
1212

13-
const { PROJECT_ROOT, getModulePaths, DEFAULT_NAMESPACE } = require('./utils/project.js');
13+
const { PROJECT_ROOT, getModulePaths } = require('./utils/project.js');
1414

1515
const { getInfoFromId } = require('./utils/module.js');
1616

17+
const DEFAULT_NAMESPACE = 'c';
18+
1719
function isFile(file) {
1820
let result;
1921

src/utils/jest.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/utils/project.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const path = require('path');
1111
const fg = require('fast-glob');
1212

1313
const PROJECT_ROOT = fs.realpathSync(process.cwd());
14-
const DEFAULT_NAMESPACE = 'c';
1514

1615
let PATHS = [];
1716

@@ -40,6 +39,5 @@ function getModulePaths() {
4039
module.exports = {
4140
PROJECT_ROOT,
4241
getSfdxProjectJson,
43-
DEFAULT_NAMESPACE,
4442
getModulePaths,
4543
};

src/utils/shell.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/utils/test-runner.js

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,15 @@
88

99
const fs = require('fs');
1010
const path = require('path');
11-
const { jestRunner } = require('./jest');
12-
const shell = require('./shell');
13-
14-
const { error, info } = require('../log');
11+
const { spawn } = require('child_process');
1512

1613
const { PROJECT_ROOT, getSfdxProjectJson } = require('./project');
1714

18-
const { jestConfig, expectedApiVersion, jestPath } = require('../config');
19-
20-
// CLI options we do not want to pass along to Jest
21-
// prettier-ignore
22-
const OPTIONS_DISALLOW_LIST = [
23-
'_',
24-
'$0',
25-
'debug', 'd',
26-
'skipApiVersionCheck', 'skip-api-version-check'
27-
];
28-
29-
function getOptions(argv) {
30-
let options = [];
15+
const { error, info } = require('../log');
16+
const { jestConfig, expectedApiVersion } = require('../config');
3117

32-
Object.keys(argv).forEach((arg) => {
33-
if (argv[arg] && !OPTIONS_DISALLOW_LIST.includes(arg)) {
34-
options.push(`--${arg}`);
35-
}
36-
});
37-
return options.concat(argv._);
38-
}
18+
// List of CLI options that should be passthrough to jest.
19+
const JEST_PASSTHROUGH_OPTIONS = new Set(['coverage', 'updateSnapshot', 'verbose', 'watch']);
3920

4021
function validSourceApiVersion() {
4122
const sfdxProjectJson = getSfdxProjectJson();
@@ -47,33 +28,61 @@ function validSourceApiVersion() {
4728
}
4829
}
4930

31+
function getJestPath() {
32+
const packageJsonPath = require.resolve('jest/package.json');
33+
34+
const { bin } = require(packageJsonPath);
35+
return path.resolve(path.dirname(packageJsonPath), bin);
36+
}
37+
38+
function getJestArgs(argv) {
39+
// Extract known options from parsed arguments
40+
const knownOptions = Object.keys(argv)
41+
.filter((key) => argv[key] && JEST_PASSTHROUGH_OPTIONS.has(key))
42+
.map((key) => `--${key}`);
43+
44+
// Extract remaining options after `--`
45+
const rest = argv._;
46+
47+
const jestArgs = [...knownOptions, ...rest];
48+
49+
// Force jest to run in band when debugging.
50+
if (argv.debug) {
51+
jestArgs.unshift('--runInBand');
52+
}
53+
54+
// Provide default configuration when none is present at the project root.
55+
const hasCustomConfig = fs.existsSync(path.resolve(PROJECT_ROOT, 'jest.config.js'));
56+
if (!hasCustomConfig) {
57+
jestArgs.unshift(`--config=${JSON.stringify(jestConfig)}`);
58+
}
59+
60+
return jestArgs;
61+
}
62+
5063
async function testRunner(argv) {
5164
if (!argv.skipApiVersionCheck) {
5265
validSourceApiVersion();
5366
}
5467

55-
const hasCustomConfig = fs.existsSync(path.resolve(PROJECT_ROOT, 'jest.config.js'));
56-
const config = hasCustomConfig ? [] : ['--config', JSON.stringify(jestConfig)];
68+
const spawnCommand = 'node';
69+
const spawnArgs = [getJestPath(), ...getJestArgs(argv)];
5770

58-
const options = getOptions(argv);
5971
if (argv.debug) {
72+
spawnArgs.unshift('--inspect-brk');
73+
6074
info('Running in debug mode...');
61-
let commandArgs = ['--inspect-brk', jestPath, '--runInBand'];
62-
commandArgs = commandArgs.concat(options);
63-
if (!hasCustomConfig) {
64-
commandArgs.push('--config=' + JSON.stringify(jestConfig));
65-
}
66-
const command = 'node';
67-
info(command + ' ' + commandArgs.join(' '));
68-
69-
shell.runCommand(command, commandArgs);
70-
} else {
71-
// Jest will not set the env if not run from the bin executable
72-
if (process.env.NODE_ENV == null) {
73-
process.env.NODE_ENV = 'test';
74-
}
75-
jestRunner.run([...config, ...options]);
75+
info(`${spawnCommand} ${spawnArgs.join(' ')}`);
7676
}
77+
78+
return new Promise((resolve) => {
79+
const jest = spawn(spawnCommand, spawnArgs, {
80+
env: process.env,
81+
stdio: 'inherit',
82+
});
83+
84+
jest.on('close', (code) => resolve(code));
85+
});
7786
}
7887

7988
module.exports = testRunner;

src/utils/yargs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
*/
77
'use strict';
88

9-
const options = require('../options/options');
109
const yargs = require('yargs');
1110

1211
const { error, info } = require('../log');
12+
const options = require('../options/options');
1313

1414
const argError = (msg, err, yargs) => {
1515
if (err) {

0 commit comments

Comments
 (0)