Skip to content

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>
AssemblyXantham.Fable.Node

node:test

Declared inside

Assert
AssertSnapshotOptions
EventData
ExportsThe package's value exports, each bound to its import
HookFn
HookOptionsConfiguration options for hooks
It
Mock
MockFunctionCall
MockFunctionContextThe MockFunctionContext class is used to inspect or manipulate the behavior of mocks created via the MockTracker APIs
MockFunctionOptions
MockMethodOptions
MockModuleContext
MockModuleOptions
MockPropertyContext
MockPropertyContextConstructor
MockTimersMocking 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
MockTrackerThe MockTracker class is used to manage mocking functionality
Reportersnode:test/reporters
RunOptions
Snapshot
Suite
SuiteContextAn instance of SuiteContext is passed to each suite function in order to interact with the test runner
SuiteFn
Test
TestContextAn instance of TestContext is passed to each test function in order to interact with the test runner
TestContextAssert
TestContextHookFn
TestContextPlanOptions
TestContextWaitForOptions
TestFn
TestOptions
TestShard
TestsStreamA 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

@since v22.20.0

abstract member assert: Assert with get, set

An 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, set
abstract member it: Test with get, set
abstract member mock: MockTracker
abstract member snapshot: Snapshot with get, set

@since v22.3.0

abstract member suite: Suite with get, set

The suite() function is imported from the node:test module.

@since v20.13.0

Parameter
nameThe 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.
optionsConfiguration options for the suite. This supports the same options as {@link test}.
fnThe suite function declaring nested tests and suites. The first argument to this function is a {@link SuiteContext} object.

Returns. Immediately fulfilled with undefined.

abstract member test: Test with get, set

Methods

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
nameThe 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.
optionsConfiguration options for the test.
fnThe 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>

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
nameThe 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.
optionsConfiguration options for the test.
fnThe 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>

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
nameThe 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.
optionsConfiguration options for the test.
fnThe 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>

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
nameThe 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.
optionsConfiguration options for the test.
fnThe 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

This 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
fnThe hook function. If the hook uses callbacks, the callback function is passed as the second argument.
optionsConfiguration options for the hook.
abstract member afterEach (?fn: HookFn, ?options: HookOptions) : unit

This 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
fnThe hook function. If the hook uses callbacks, the callback function is passed as the second argument.
optionsConfiguration options for the hook.
abstract member before (?fn: HookFn, ?options: HookOptions) : unit

This 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
fnThe hook function. If the hook uses callbacks, the callback function is passed as the second argument.
optionsConfiguration options for the hook.
abstract member beforeEach (?fn: HookFn, ?options: HookOptions) : unit

This 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
fnThe hook function. If the hook uses callbacks, the callback function is passed as the second argument.
optionsConfiguration options for the hook.
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>

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>

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>

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

Note: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
optionsConfiguration options for running tests.
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>

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>

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>

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>

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 (?name: string, ?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 (?name: string, ?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