Skip to content

Commit 0a38f08

Browse files
OrKoNwolfib
authored andcommitted
test: speed up .only (#1846)
the node test framework runs not only tests respecting `--test-concurrency=1` but also discovery of the tests (import of the files). This makes the `npm run test:only` slow. This PR checks ahead of times which files have .only and only tells the node test runner to import those.
1 parent f09de54 commit 0a38f08

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

scripts/test.mjs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// Node 20 does not support --experimental-strip-types flag.
99

1010
import {spawn, execSync} from 'node:child_process';
11+
import {readFile} from 'node:fs/promises';
1112
import path from 'node:path';
1213
import process from 'node:process';
1314

@@ -38,10 +39,27 @@ if (userArgs.length > 0) {
3839
}
3940
} else {
4041
const isNode20 = process.version.startsWith('v20.');
41-
if (isNode20) {
42-
files.push('build/tests');
43-
} else {
44-
files.push('build/tests/**/*.test.js');
42+
if (flags.includes('--test-only')) {
43+
if (isNode20) {
44+
throw new Error(`--test-only is not supported for Node 20`);
45+
}
46+
const {glob} = await import('node:fs/promises');
47+
for await (const tsFile of glob('tests/**/*.test.ts')) {
48+
const content = await readFile(tsFile, 'utf8');
49+
if (content.includes('.only(')) {
50+
files.push(path.join('build', tsFile.replace(/\.ts$/, '.js')));
51+
}
52+
}
53+
if (files.length === 0) {
54+
console.warn('no files contain .only');
55+
process.exit(0);
56+
}
57+
} else if (files.length === 0) {
58+
if (isNode20) {
59+
files.push('build/tests');
60+
} else {
61+
files.push('build/tests/**/*.test.js');
62+
}
4563
}
4664
}
4765

0 commit comments

Comments
 (0)