Skip to content

TestContext

[<Interface>]
type TestContext =
    abstract member assert: TestContextAssert
    abstract member filePath: string option
    abstract member fullName: string
    abstract member mock: MockTracker
    abstract member name: string
    abstract member signal: AbortSignal
    abstract member test: Test with get, set
    static member Create<'T> (assert: TestContextAssert, before: Action<TestContextHookFn option,HookOptions option>, beforeEach: Action<TestContextHookFn option,HookOptions option>, after: Action<TestContextHookFn option,HookOptions option>, afterEach: Action<TestContextHookFn option,HookOptions option>, diagnostic: string -> unit, fullName: string, name: string, plan: Action<float,TestContextPlanOptions option>, runOnly: bool -> unit, signal: AbortSignal, skip: string option -> unit, todo: string option -> unit, test: Test, waitFor: Func<(unit -> 'T),TestContextWaitForOptions option,Promise<obj>>, mock: MockTracker, ?filePath: string) : TestContext
    abstract member after (?fn: TestContextHookFn, ?options: HookOptions) : unit
    abstract member afterEach (?fn: TestContextHookFn, ?options: HookOptions) : unit
    abstract member before (?fn: TestContextHookFn, ?options: HookOptions) : unit
    abstract member beforeEach (?fn: TestContextHookFn, ?options: HookOptions) : unit
    abstract member diagnostic (message: string) : unit
    abstract member plan (count: float, ?options: TestContextPlanOptions) : unit
    abstract member runOnly (shouldRunOnlyTests: bool) : unit
    abstract member skip (?message: string) : unit
    abstract member todo (?message: string) : unit
    abstract member waitFor<'T> (condition: unit -> 'T, ?options: TestContextWaitForOptions) : Promise<obj>
AssemblyXantham.Fable.Node
Attributes[<Interface>]

An instance of TestContext is passed to each test function in order to interact with the test runner. However, the TestContext constructor is not exposed as part of the API.

@since v18.0.0, v16.17.0

Properties

abstract member assert: TestContextAssert

An object containing assertion methods bound to the test context. The top-level functions from the node:assert module are exposed here for the purpose of creating test plans.

Note: Some of the functions from node:assert contain type assertions. If these are called via the TestContext assert object, then the context parameter in the test's function signature must be explicitly typed (ie. the parameter must have a type annotation), otherwise an error will be raised by the TypeScript compiler:

import { test, type TestContext } from 'node:test';

// The test function's context parameter must have a type annotation.
test('example', (t: TestContext) => {
t.assert.deepStrictEqual(actual, expected);
});

// Omitting the type annotation will result in a compilation error.
test('example', t => {
t.assert.deepStrictEqual(actual, expected); // Error: 't' needs an explicit type annotation.
});

@since v22.2.0, v20.15.0

abstract member filePath: string option

The absolute path of the test file that created the current test. If a test file imports additional modules that generate tests, the imported tests will return the path of the root test file.

@since v22.6.0

abstract member fullName: string

The name of the test and each of its ancestors, separated by >.

@since v22.3.0

abstract member mock: MockTracker

Each test provides its own MockTracker instance.

abstract member name: string

The name of the test.

@since v18.8.0, v16.18.0

abstract member signal: AbortSignal
test('top level test', async (t) => {
await fetch('some/uri', { signal: t.signal });
});

@since v18.7.0, v16.17.0

abstract member test: Test with get, set

This function is used to create subtests under the current test. This function behaves in the same fashion as the top level test function.

@since v18.0.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. This 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. A {@link Promise} resolved with undefined once the test completes.

Methods

static member Create<'T> (assert: TestContextAssert, before: Action<TestContextHookFn option,HookOptions option>, beforeEach: Action<TestContextHookFn option,HookOptions option>, after: Action<TestContextHookFn option,HookOptions option>, afterEach: Action<TestContextHookFn option,HookOptions option>, diagnostic: string -> unit, fullName: string, name: string, plan: Action<float,TestContextPlanOptions option>, runOnly: bool -> unit, signal: AbortSignal, skip: string option -> unit, todo: string option -> unit, test: Test, waitFor: Func<(unit -> 'T),TestContextWaitForOptions option,Promise<obj>>, mock: MockTracker, ?filePath: string) : TestContext
abstract member after (?fn: TestContextHookFn, ?options: HookOptions) : unit

This function is used to create a hook that runs after the current test finishes.

@since v18.13.0

Parameter
fnThe hook function. The first argument to this function is a TestContext object. If the hook uses callbacks, the callback function is passed as the second argument.
optionsConfiguration options for the hook.
abstract member afterEach (?fn: TestContextHookFn, ?options: HookOptions) : unit

This function is used to create a hook running after each subtest of the current test.

@since v18.8.0

Parameter
fnThe hook function. The first argument to this function is a TestContext object. If the hook uses callbacks, the callback function is passed as the second argument.
optionsConfiguration options for the hook.
abstract member before (?fn: TestContextHookFn, ?options: HookOptions) : unit

This function is used to create a hook running before subtest of the current test.

@since v20.1.0, v18.17.0

Parameter
fnThe hook function. The first argument to this function is a TestContext object. If the hook uses callbacks, the callback function is passed as the second argument.
optionsConfiguration options for the hook.
abstract member beforeEach (?fn: TestContextHookFn, ?options: HookOptions) : unit

This function is used to create a hook running before each subtest of the current test.

@since v18.8.0

Parameter
fnThe hook function. The first argument to this function is a TestContext object. If the hook uses callbacks, the callback function is passed as the second argument.
optionsConfiguration options for the hook.
abstract member diagnostic (message: string) : unit

This function is used to write diagnostics to the output. Any diagnostic information is included at the end of the test's results. This function does not return a value.

test('top level test', (t) => {
t.diagnostic('A diagnostic message');
});

@since v18.0.0, v16.17.0

Parameter
messageMessage to be reported.
abstract member plan (count: float, ?options: TestContextPlanOptions) : unit

This function is used to set the number of assertions and subtests that are expected to run within the test. If the number of assertions and subtests that run does not match the expected count, the test will fail.

Note: To make sure assertions are tracked, t.assert must be used instead of assert directly.

test('top level test', (t) => {
t.plan(2);
t.assert.ok('some relevant assertion here');
t.test('subtest', () => {});
});

When working with asynchronous code, the plan function can be used to ensure that the correct number of assertions are run:

test('planning with streams', (t, done) => {
function* generate() {
yield 'a';
yield 'b';
yield 'c';
}
const expected = ['a', 'b', 'c'];
t.plan(expected.length);
const stream = Readable.from(generate());
stream.on('data', (chunk) => {
t.assert.strictEqual(chunk, expected.shift());
});

stream.on('end', () => {
done();
});
});

When using the wait option, you can control how long the test will wait for the expected assertions. For example, setting a maximum wait time ensures that the test will wait for asynchronous assertions to complete within the specified timeframe:

test('plan with wait: 2000 waits for async assertions', (t) => {
t.plan(1, { wait: 2000 }); // Waits for up to 2 seconds for the assertion to complete.

const asyncActivity = () => {
setTimeout(() => {
*       t.assert.ok(true, 'Async assertion completed within the wait time');
}, 1000); // Completes after 1 second, within the 2-second wait time.
};

asyncActivity(); // The test will pass because the assertion is completed in time.
});

Note: If a wait timeout is specified, it begins counting down only after the test function finishes executing.

@since v22.2.0

abstract member runOnly (shouldRunOnlyTests: bool) : unit

If shouldRunOnlyTests is truthy, the test context will only run tests that have the only option set. Otherwise, all tests are run. If Node.js was not started with the --test-only command-line option, this function is a no-op.

test('top level test', (t) => {
// The test context can be set to run subtests with the 'only' option.
t.runOnly(true);
return Promise.all([
t.test('this subtest is now skipped'),
t.test('this subtest is run', { only: true }),
]);
});

@since v18.0.0, v16.17.0

Parameter
shouldRunOnlyTestsWhether or not to run only tests.
abstract member todo (?message: string) : unit

This function adds a TODO directive to the test's output. If message is provided, it is included in the output. Calling todo() does not terminate execution of the test function. This function does not return a value.

test('top level test', (t) => {
// This test is marked as `TODO`
t.todo('this is a todo');
});

@since v18.0.0, v16.17.0

Parameter
messageOptional TODO message.
abstract member waitFor<'T> (condition: unit -> 'T, ?options: TestContextWaitForOptions) : Promise<obj>

This method polls a condition function until that function either returns successfully or the operation times out.

@since v22.14.0

Parameter
conditionAn assertion function that is invoked periodically until it completes successfully or the defined polling timeout elapses. Successful completion is defined as not throwing or rejecting. This function does not accept any arguments, and is allowed to return any value.
optionsAn optional configuration object for the polling operation.

Returns. Fulfilled with the value returned by condition.