Test
[<Interface>]
type Test =
abstract member MockPropertyContext: MockPropertyContextConstructor with get, set
abstract member assert: Assert with get, set
abstract member describe: Suite with get, set
abstract member it: Test with get, set
abstract member mock: MockTracker
abstract member snapshot: Snapshot with get, set
abstract member suite: Suite with get, set
abstract member test: Test with get, set
abstract member Invoke (?fn: TestFn) : Promise<unit>
abstract member Invoke (?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member Invoke (?name: string, ?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member Invoke (?name: string, ?fn: TestFn) : Promise<unit>
abstract member after (?fn: HookFn, ?options: HookOptions) : unit
abstract member afterEach (?fn: HookFn, ?options: HookOptions) : unit
abstract member before (?fn: HookFn, ?options: HookOptions) : unit
abstract member beforeEach (?fn: HookFn, ?options: HookOptions) : unit
abstract member only (?fn: TestFn) : Promise<unit>
abstract member only (?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member only (?name: string, ?fn: TestFn) : Promise<unit>
abstract member only (?name: string, ?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member run (?options: RunOptions) : TestsStream
abstract member skip (?fn: TestFn) : Promise<unit>
abstract member skip (?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member skip (?name: string, ?fn: TestFn) : Promise<unit>
abstract member skip (?name: string, ?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member todo (?fn: TestFn) : Promise<unit>
abstract member todo (?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member todo (?name: string, ?fn: TestFn) : Promise<unit>
abstract member todo (?name: string, ?options: TestOptions, ?fn: TestFn) : Promise<unit>| Assembly | Xantham.Fable.Node |
node:test
Declared inside
Assert | |
AssertSnapshotOptions | |
EventData | |
Exports | The package's value exports, each bound to its import |
HookFn | |
HookOptions | Configuration options for hooks |
It | |
Mock | |
MockFunctionCall | |
MockFunctionContext | The MockFunctionContext class is used to inspect or manipulate the behavior of mocks created via the MockTracker APIs |
MockFunctionOptions | |
MockMethodOptions | |
MockModuleContext | |
MockModuleOptions | |
MockPropertyContext | |
MockPropertyContextConstructor | |
MockTimers | Mocking timers is a technique commonly used in software testing to simulate and control the behavior of timers, such as setInterval and setTimeout, without actually waiting for the specified time intervals |
MockTimersOptions | |
MockTracker | The MockTracker class is used to manage mocking functionality |
Reporters | node:test/reporters |
RunOptions | |
Snapshot | |
Suite | |
SuiteContext | An instance of SuiteContext is passed to each suite function in order to interact with the test runner |
SuiteFn | |
Test | |
TestContext | An instance of TestContext is passed to each test function in order to interact with the test runner |
TestContextAssert | |
TestContextHookFn | |
TestContextPlanOptions | |
TestContextWaitForOptions | |
TestFn | |
TestOptions | |
TestShard | |
TestsStream | A successful call to run() will return a new TestsStream object, streaming a series of events representing the execution of the tests |
Properties
abstract member MockPropertyContext: MockPropertyContextConstructor with get, set
abstract member MockPropertyContext: MockPropertyContextConstructor with get, set@since v22.20.0
abstract member assert: Assert with get, set
abstract member assert: Assert with get, setAn object whose methods are used to configure available assertions on the TestContext objects in the current process. The methods from node:assert and snapshot testing functions are available by default.
It is possible to apply the same configuration to all files by placing common configuration code in a module preloaded with --require or --import.
@since v22.14.0
abstract member describe: Suite with get, setabstract member suite: Suite with get, set
abstract member suite: Suite with get, setThe suite() function is imported from the node:test module.
@since v20.13.0
| Parameter | |
|---|---|
name | The name of the suite, which is displayed when reporting test results. Defaults to the name property of fn, or '<anonymous>' if fn does not have a name. |
options | Configuration options for the suite. This supports the same options as {@link test}. |
fn | The suite function declaring nested tests and suites. The first argument to this function is a {@link SuiteContext} object. |
Returns. Immediately fulfilled with undefined.
Methods
abstract member Invoke (?fn: TestFn) : Promise<unit>
abstract member Invoke (?fn: TestFn) : Promise<unit>The test() function is the value imported from the test module. Each invocation of this function results in reporting the test to the TestsStream.
The TestContext object passed to the fn argument can be used to perform actions related to the current test. Examples include skipping the test, adding additional diagnostic information, or creating subtests.
test() returns a Promise that fulfills once the test completes. if test() is called within a suite, it fulfills immediately. The return value can usually be discarded for top level tests. However, the return value from subtests should be used to prevent the parent test from finishing first and cancelling the subtest as shown in the following example.
test('top level test', async (t) => {
// The setTimeout() in the following subtest would cause it to outlive its
// parent test if 'await' is removed on the next line. Once the parent test
// completes, it will cancel any outstanding subtests.
await t.test('longer running subtest', async (t) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000);
});
});
});
The timeout option can be used to fail the test if it takes longer than timeout milliseconds to complete. However, it is not a reliable mechanism for canceling tests because a running test might block the application thread and thus prevent the scheduled cancellation.
@since v18.0.0, v16.17.0
| Parameter | |
|---|---|
name | The name of the test, which is displayed when reporting test results. Defaults to the name property of fn, or '<anonymous>' if fn does not have a name. |
options | Configuration options for the test. |
fn | The function under test. The first argument to this function is a {@link TestContext} object. If the test uses callbacks, the callback function is passed as the second argument. |
Returns. Fulfilled with undefined once the test completes, or immediately if the test runs within a suite.
abstract member Invoke (?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member Invoke (?options: TestOptions, ?fn: TestFn) : Promise<unit>The test() function is the value imported from the test module. Each invocation of this function results in reporting the test to the TestsStream.
The TestContext object passed to the fn argument can be used to perform actions related to the current test. Examples include skipping the test, adding additional diagnostic information, or creating subtests.
test() returns a Promise that fulfills once the test completes. if test() is called within a suite, it fulfills immediately. The return value can usually be discarded for top level tests. However, the return value from subtests should be used to prevent the parent test from finishing first and cancelling the subtest as shown in the following example.
test('top level test', async (t) => {
// The setTimeout() in the following subtest would cause it to outlive its
// parent test if 'await' is removed on the next line. Once the parent test
// completes, it will cancel any outstanding subtests.
await t.test('longer running subtest', async (t) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000);
});
});
});
The timeout option can be used to fail the test if it takes longer than timeout milliseconds to complete. However, it is not a reliable mechanism for canceling tests because a running test might block the application thread and thus prevent the scheduled cancellation.
@since v18.0.0, v16.17.0
| Parameter | |
|---|---|
name | The name of the test, which is displayed when reporting test results. Defaults to the name property of fn, or '<anonymous>' if fn does not have a name. |
options | Configuration options for the test. |
fn | The function under test. The first argument to this function is a {@link TestContext} object. If the test uses callbacks, the callback function is passed as the second argument. |
Returns. Fulfilled with undefined once the test completes, or immediately if the test runs within a suite.
abstract member Invoke (?name: string, ?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member Invoke (?name: string, ?options: TestOptions, ?fn: TestFn) : Promise<unit>The test() function is the value imported from the test module. Each invocation of this function results in reporting the test to the TestsStream.
The TestContext object passed to the fn argument can be used to perform actions related to the current test. Examples include skipping the test, adding additional diagnostic information, or creating subtests.
test() returns a Promise that fulfills once the test completes. if test() is called within a suite, it fulfills immediately. The return value can usually be discarded for top level tests. However, the return value from subtests should be used to prevent the parent test from finishing first and cancelling the subtest as shown in the following example.
test('top level test', async (t) => {
// The setTimeout() in the following subtest would cause it to outlive its
// parent test if 'await' is removed on the next line. Once the parent test
// completes, it will cancel any outstanding subtests.
await t.test('longer running subtest', async (t) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000);
});
});
});
The timeout option can be used to fail the test if it takes longer than timeout milliseconds to complete. However, it is not a reliable mechanism for canceling tests because a running test might block the application thread and thus prevent the scheduled cancellation.
@since v18.0.0, v16.17.0
| Parameter | |
|---|---|
name | The name of the test, which is displayed when reporting test results. Defaults to the name property of fn, or '<anonymous>' if fn does not have a name. |
options | Configuration options for the test. |
fn | The function under test. The first argument to this function is a {@link TestContext} object. If the test uses callbacks, the callback function is passed as the second argument. |
Returns. Fulfilled with undefined once the test completes, or immediately if the test runs within a suite.
abstract member Invoke (?name: string, ?fn: TestFn) : Promise<unit>
abstract member Invoke (?name: string, ?fn: TestFn) : Promise<unit>The test() function is the value imported from the test module. Each invocation of this function results in reporting the test to the TestsStream.
The TestContext object passed to the fn argument can be used to perform actions related to the current test. Examples include skipping the test, adding additional diagnostic information, or creating subtests.
test() returns a Promise that fulfills once the test completes. if test() is called within a suite, it fulfills immediately. The return value can usually be discarded for top level tests. However, the return value from subtests should be used to prevent the parent test from finishing first and cancelling the subtest as shown in the following example.
test('top level test', async (t) => {
// The setTimeout() in the following subtest would cause it to outlive its
// parent test if 'await' is removed on the next line. Once the parent test
// completes, it will cancel any outstanding subtests.
await t.test('longer running subtest', async (t) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000);
});
});
});
The timeout option can be used to fail the test if it takes longer than timeout milliseconds to complete. However, it is not a reliable mechanism for canceling tests because a running test might block the application thread and thus prevent the scheduled cancellation.
@since v18.0.0, v16.17.0
| Parameter | |
|---|---|
name | The name of the test, which is displayed when reporting test results. Defaults to the name property of fn, or '<anonymous>' if fn does not have a name. |
options | Configuration options for the test. |
fn | The function under test. The first argument to this function is a {@link TestContext} object. If the test uses callbacks, the callback function is passed as the second argument. |
Returns. Fulfilled with undefined once the test completes, or immediately if the test runs within a suite.
abstract member after (?fn: HookFn, ?options: HookOptions) : unit
abstract member after (?fn: HookFn, ?options: HookOptions) : unitThis function creates a hook that runs after executing a suite.
describe('tests', async () => {
after(() => console.log('finished running tests'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
});
});
@since v18.8.0, v16.18.0
| Parameter | |
|---|---|
fn | The hook function. If the hook uses callbacks, the callback function is passed as the second argument. |
options | Configuration options for the hook. |
abstract member afterEach (?fn: HookFn, ?options: HookOptions) : unit
abstract member afterEach (?fn: HookFn, ?options: HookOptions) : unitThis function creates a hook that runs after each test in the current suite. The afterEach() hook is run even if the test fails.
describe('tests', async () => {
afterEach(() => console.log('finished running a test'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
});
});
@since v18.8.0, v16.18.0
| Parameter | |
|---|---|
fn | The hook function. If the hook uses callbacks, the callback function is passed as the second argument. |
options | Configuration options for the hook. |
abstract member before (?fn: HookFn, ?options: HookOptions) : unit
abstract member before (?fn: HookFn, ?options: HookOptions) : unitThis function creates a hook that runs before executing a suite.
describe('tests', async () => {
before(() => console.log('about to run some test'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
});
});
@since v18.8.0, v16.18.0
| Parameter | |
|---|---|
fn | The hook function. If the hook uses callbacks, the callback function is passed as the second argument. |
options | Configuration options for the hook. |
abstract member beforeEach (?fn: HookFn, ?options: HookOptions) : unit
abstract member beforeEach (?fn: HookFn, ?options: HookOptions) : unitThis function creates a hook that runs before each test in the current suite.
describe('tests', async () => {
beforeEach(() => console.log('about to run a test'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
});
});
@since v18.8.0, v16.18.0
| Parameter | |
|---|---|
fn | The hook function. If the hook uses callbacks, the callback function is passed as the second argument. |
options | Configuration options for the hook. |
abstract member only (?fn: TestFn) : Promise<unit>
abstract member only (?fn: TestFn) : Promise<unit>Shorthand for marking a test as only. This is the same as calling test with options.only set to true.
@since v20.2.0
abstract member only (?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member only (?options: TestOptions, ?fn: TestFn) : Promise<unit>Shorthand for marking a test as only. This is the same as calling test with options.only set to true.
@since v20.2.0
abstract member only (?name: string, ?fn: TestFn) : Promise<unit>
abstract member only (?name: string, ?fn: TestFn) : Promise<unit>Shorthand for marking a test as only. This is the same as calling test with options.only set to true.
@since v20.2.0
abstract member only (?name: string, ?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member only (?name: string, ?options: TestOptions, ?fn: TestFn) : Promise<unit>Shorthand for marking a test as only. This is the same as calling test with options.only set to true.
@since v20.2.0
abstract member run (?options: RunOptions) : TestsStream
abstract member run (?options: RunOptions) : TestsStreamNote:shard is used to horizontally parallelize test running across machines or processes, ideal for large-scale executions across varied environments. It's incompatible with watch mode, tailored for rapid code iteration by automatically rerunning tests on file changes.
import { tap } from 'node:test/reporters';
import { run } from 'node:test';
import process from 'node:process';
import path from 'node:path';
run({ files: [path.resolve('./tests/test.js')] })
.compose(tap)
.pipe(process.stdout);
@since v18.9.0, v16.19.0
| Parameter | |
|---|---|
options | Configuration options for running tests. |
abstract member skip (?fn: TestFn) : Promise<unit>
abstract member skip (?fn: TestFn) : Promise<unit>Shorthand for skipping a test. This is the same as calling test with options.skip set to true.
@since v20.2.0
abstract member skip (?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member skip (?options: TestOptions, ?fn: TestFn) : Promise<unit>Shorthand for skipping a test. This is the same as calling test with options.skip set to true.
@since v20.2.0
abstract member skip (?name: string, ?fn: TestFn) : Promise<unit>
abstract member skip (?name: string, ?fn: TestFn) : Promise<unit>Shorthand for skipping a test. This is the same as calling test with options.skip set to true.
@since v20.2.0
abstract member skip (?name: string, ?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member skip (?name: string, ?options: TestOptions, ?fn: TestFn) : Promise<unit>Shorthand for skipping a test. This is the same as calling test with options.skip set to true.
@since v20.2.0
abstract member todo (?fn: TestFn) : Promise<unit>
abstract member todo (?fn: TestFn) : Promise<unit>Shorthand for marking a test as TODO. This is the same as calling test with options.todo set to true.
@since v20.2.0
abstract member todo (?options: TestOptions, ?fn: TestFn) : Promise<unit>
abstract member todo (?options: TestOptions, ?fn: TestFn) : Promise<unit>Shorthand for marking a test as TODO. This is the same as calling test with options.todo set to true.
@since v20.2.0