Skip to content

Commit 7e3f0dc

Browse files
authored
chore(node-integration-tests): Improve local testing experience (#8714)
* Re-add support for `-t` arg to filter for specific tests * Re-add support for `--watch` flag * Reduce logging for test suites where all tests were skipped. * Add `yargs` to parse arguments (and provide help)
1 parent 050a636 commit 7e3f0dc

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

packages/node-integration-tests/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"mongodb-memory-server-global": "^7.6.3",
3737
"mysql": "^2.18.1",
3838
"nock": "^13.1.0",
39-
"pg": "^8.7.3"
39+
"pg": "^8.7.3",
40+
"yargs": "^16.2.0"
4041
},
4142
"config": {
4243
"mongodbMemoryServer": {

packages/node-integration-tests/utils/run-tests.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
/* eslint-disable no-console */
22
import childProcess from 'child_process';
33
import os from 'os';
4+
import yargs from 'yargs';
5+
6+
const args = yargs
7+
.option('t', {
8+
alias: 'testNamePattern',
9+
type: 'string',
10+
description: 'Filter for a specific test spec\nsee: https://jestjs.io/docs/cli#--testnamepatternregex',
11+
})
12+
.option('watch', {
13+
type: 'boolean',
14+
description: 'Run tests in watch mode\nsee: https://jestjs.io/docs/cli#--watch',
15+
}).argv;
416

517
// This variable will act as a job queue that is consumed by a number of worker threads. Each item represents a test to run.
618
const testPaths = childProcess.execSync('jest --listTests', { encoding: 'utf8' }).trim().split('\n');
@@ -14,7 +26,17 @@ const workers = os.cpus().map(async (_, i) => {
1426
const testPath = testPaths.pop();
1527
console.log(`(Worker ${i}) Running test "${testPath}"`);
1628
await new Promise<void>(resolve => {
17-
const jestProcess = childProcess.spawn('jest', ['--runTestsByPath', testPath as string, '--forceExit']);
29+
const jestArgs = ['--runTestsByPath', testPath as string, '--forceExit'];
30+
31+
if (args.t) {
32+
jestArgs.push('-t', args.t);
33+
}
34+
35+
if (args.watch) {
36+
jestArgs.push('--watch');
37+
}
38+
39+
const jestProcess = childProcess.spawn('jest', jestArgs);
1840

1941
// We're collecting the output and logging it all at once instead of inheriting stdout and stderr, so that
2042
// test outputs of the individual workers aren't interwoven, in case they print at the same time.
@@ -36,6 +58,7 @@ const workers = os.cpus().map(async (_, i) => {
3658
});
3759

3860
jestProcess.on('exit', exitcode => {
61+
output = checkSkippedAllTests(output, i, testPath);
3962
console.log(`(Worker ${i}) Finished test "${testPath}"`);
4063
console.log(output);
4164
if (exitcode !== 0) {
@@ -61,3 +84,20 @@ void Promise.all(workers).then(() => {
6184
process.exit(0);
6285
}
6386
});
87+
88+
/**
89+
* Suppress jest output for test suites where all tests were skipped.
90+
* This only clutters the logs and we can safely print a one-liner instead.
91+
*/
92+
function checkSkippedAllTests(output: string, workerNumber: number, testPath: string | undefined): string {
93+
const regex = /Tests:\s+(\d+) skipped, (\d+) total/gm;
94+
const matches = regex.exec(output);
95+
if (matches) {
96+
const skipped = Number(matches[1]);
97+
const total = Number(matches[2]);
98+
if (!isNaN(skipped) && !isNaN(total) && total === skipped) {
99+
return `(Worker ${workerNumber}) > Skipped all (${total} tests) in ${testPath}`;
100+
}
101+
}
102+
return output;
103+
}

0 commit comments

Comments
 (0)