-
Notifications
You must be signed in to change notification settings - Fork 82
/
test-runner.js
98 lines (76 loc) · 2.88 KB
/
test-runner.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
'use strict';
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const { PROJECT_ROOT, getSfdxProjectJson } = require('./project');
const { error, info } = require('../log');
const { jestConfig, expectedApiVersion } = require('../config');
// List of CLI options that should be passthrough to jest.
const JEST_PASSTHROUGH_OPTIONS = new Set(['coverage', 'updateSnapshot', 'verbose', 'watch']);
function validSourceApiVersion() {
const sfdxProjectJson = getSfdxProjectJson();
const apiVersion = sfdxProjectJson.sourceApiVersion;
if (apiVersion !== expectedApiVersion) {
error(
`Invalid sourceApiVersion found in sfdx-project.json. Expected ${expectedApiVersion}, found ${apiVersion}`,
);
}
}
function getJestPath() {
const packageJsonPath = require.resolve('jest/package.json');
const { bin } = require(packageJsonPath);
// Account for difference in behavior between npm v6 and v7 (#221)
let normalizedBinPath = bin;
if (typeof bin === 'object') {
normalizedBinPath = bin.jest;
}
return path.resolve(path.dirname(packageJsonPath), normalizedBinPath);
}
function getJestArgs(argv) {
// Extract known options from parsed arguments
const knownOptions = Object.keys(argv)
.filter((key) => argv[key] && JEST_PASSTHROUGH_OPTIONS.has(key))
.map((key) => `--${key}`);
// Extract remaining options after `--`
const rest = argv._;
const jestArgs = [...knownOptions, ...rest];
// Force jest to run in band when debugging.
if (argv.debug) {
jestArgs.unshift('--runInBand');
}
// Provide default configuration when none is present at the project root.
const hasCustomConfig =
fs.existsSync(path.resolve(PROJECT_ROOT, 'jest.config.js')) ||
fs.existsSync(path.resolve(PROJECT_ROOT, 'jest.config.mjs')) ||
fs.existsSync(path.resolve(PROJECT_ROOT, 'jest.config.cjs'));
if (!hasCustomConfig) {
jestArgs.unshift(`--config=${JSON.stringify(jestConfig)}`);
}
return jestArgs;
}
async function testRunner(argv) {
if (!argv.skipApiVersionCheck) {
validSourceApiVersion();
}
const spawnCommand = 'node';
const spawnArgs = [getJestPath(), ...getJestArgs(argv)];
if (argv.debug) {
spawnArgs.unshift('--inspect-brk');
info('Running in debug mode...');
info(`${spawnCommand} ${spawnArgs.join(' ')}`);
}
return new Promise((resolve) => {
const jest = spawn(spawnCommand, spawnArgs, {
env: process.env,
stdio: 'inherit',
});
jest.on('close', (code) => resolve(code));
});
}
module.exports = testRunner;