-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use node:test and node:assert/strict (#9649)
- Loading branch information
Showing
9 changed files
with
119 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'node:test'; | ||
import * as assert from 'node:assert/strict'; | ||
import { getContext } from '../dist/index.js'; | ||
|
||
describe('context', () => { | ||
it('no arguments', async () => { | ||
const ctx = await getContext([]); | ||
expect(ctx.version).to.eq('latest'); | ||
expect(ctx.dryRun).to.be.undefined; | ||
assert.equal(ctx.version, 'latest'); | ||
assert.equal(ctx.dryRun, undefined); | ||
}); | ||
it('tag', async () => { | ||
const ctx = await getContext(['beta']); | ||
expect(ctx.version).to.eq('beta'); | ||
expect(ctx.dryRun).to.be.undefined; | ||
assert.equal(ctx.version, 'beta'); | ||
assert.equal(ctx.dryRun, undefined); | ||
}); | ||
it('dry run', async () => { | ||
const ctx = await getContext(['--dry-run']); | ||
expect(ctx.dryRun).to.eq(true); | ||
assert.equal(ctx.dryRun, true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters