TestContext
type TestContext = TestContext| Assembly | Xantham.Fable.Node |
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
abstract member assert: TestContextAssertAn 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
abstract member filePath: string optionThe 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
abstract member fullName: stringThe name of the test and each of its ancestors, separated by >.
@since v22.3.0
abstract member signal: AbortSignal
abstract member signal: AbortSignaltest('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
abstract member test: Test with get, setThis 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 | |
|---|---|
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. 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.