diff options
author | 2024-01-12 15:53:00 +0800 | |
---|---|---|
committer | 2024-01-12 15:53:00 +0800 | |
commit | 71db79e62a237ead543fab3bbe69a1c49645c98a (patch) | |
tree | 22aeb97ce1237c3ee40499496b40ea0efe68da85 /scripts | |
parent | d02a3c48a3ce204649d22e17b1e26fb5a6a60bcf (diff) | |
download | astro-71db79e62a237ead543fab3bbe69a1c49645c98a.tar.gz astro-71db79e62a237ead543fab3bbe69a1c49645c98a.tar.zst astro-71db79e62a237ead543fab3bbe69a1c49645c98a.zip |
Use node:test and node:assert/strict (#9649)
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/cmd/test.js | 51 | ||||
-rwxr-xr-x | scripts/index.js | 5 |
2 files changed, 56 insertions, 0 deletions
diff --git a/scripts/cmd/test.js b/scripts/cmd/test.js index e69de29bb..84f6d9742 100644 --- a/scripts/cmd/test.js +++ b/scripts/cmd/test.js @@ -0,0 +1,51 @@ +import { run } from 'node:test'; +import { spec } from 'node:test/reporters'; +import arg from 'arg'; +import glob from 'tiny-glob'; + +const isCI = !!process.env.CI; +const defaultTimeout = isCI ? 30000 : 20000; + +export default async function test() { + const args = arg({ + '--match': String, // aka --test-name-pattern: https://nodejs.org/api/test.html#filtering-tests-by-name + '--only': Boolean, // aka --test-only: https://nodejs.org/api/test.html#only-tests + '--parallel': Boolean, // aka --test-concurrency: https://nodejs.org/api/test.html#test-runner-execution-model + '--watch': Boolean, // experimental: https://nodejs.org/api/test.html#watch-mode + '--timeout': Number, // Test timeout in milliseconds (default: 30000ms) + '--setup': String, // Test setup file + // Aliases + '-m': '--match', + '-o': '--only', + '-p': '--parallel', + '-w': '--watch', + '-t': '--timeout', + '-s': '--setup', + }); + + const pattern = args._[1]; + if (!pattern) throw new Error('Missing test glob pattern'); + + const files = await glob(pattern, { filesOnly: true, absolute: true }); + + // For some reason, the `only` option does not work and we need to explicitly set the CLI flag instead. + // Node.js requires opt-in to run .only tests :( + // https://nodejs.org/api/test.html#only-tests + if (args['--only']) { + process.env.NODE_OPTIONS ??= ''; + process.env.NODE_OPTIONS += ' --test-only'; + } + + // https://nodejs.org/api/test.html#runoptions + run({ + files, + testNamePatterns: args['--match'], + concurrency: args['--parallel'], + only: args['--only'], + setup: args['--setup'], + watch: args['--watch'], + timeout: args['--timeout'] ?? defaultTimeout, // Node.js defaults to Infinity, so set better fallback + }) + .pipe(new spec()) + .pipe(process.stdout); +} diff --git a/scripts/index.js b/scripts/index.js index 249eac53d..381500ac4 100755 --- a/scripts/index.js +++ b/scripts/index.js @@ -18,6 +18,11 @@ export default async function run() { await prebuild(...args); break; } + case 'test': { + const { default: test } = await import('./cmd/test.js'); + await test(...args); + break; + } } } |