Usage in Deno
import * as mod from "node:test";
The node:test module facilitates the creation of JavaScript tests.
To access it:
import test from 'node:test';
This module is only available under the node: scheme. The following will not
work:
import test from 'node:test';
Tests created via the test module consist of a single function that is
processed in one of three ways:
- A synchronous function that is considered failing if it throws an exception, and is considered passing otherwise.
- A function that returns a
Promisethat is considered failing if thePromiserejects, and is considered passing if thePromisefulfills. - A function that receives a callback function. If the callback receives any
truthy value as its first argument, the test is considered failing. If a
falsy value is passed as the first argument to the callback, the test is
considered passing. If the test function receives a callback function and
also returns a
Promise, the test will fail.
The following example illustrates how tests are written using the test module.
test('synchronous passing test', (t) => {
// This test passes because it does not throw an exception.
assert.strictEqual(1, 1);
});
test('synchronous failing test', (t) => {
// This test fails because it throws an exception.
assert.strictEqual(1, 2);
});
test('asynchronous passing test', async (t) => {
// This test passes because the Promise returned by the async
// function is settled and not rejected.
assert.strictEqual(1, 1);
});
test('asynchronous failing test', async (t) => {
// This test fails because the Promise returned by the async
// function is rejected.
assert.strictEqual(1, 2);
});
test('failing test using Promises', (t) => {
// Promises can be used directly as well.
return new Promise((resolve, reject) => {
setImmediate(() => {
reject(new Error('this will cause the test to fail'));
});
});
});
test('callback passing test', (t, done) => {
// done() is the callback function. When the setImmediate() runs, it invokes
// done() with no arguments.
setImmediate(done);
});
test('callback failing test', (t, done) => {
// When the setImmediate() runs, done() is invoked with an Error object and
// the test fails.
setImmediate(() => {
done(new Error('callback failure'));
});
});
If any tests fail, the process exit code is set to 1.
Classes #
The MockFunctionContext class is used to inspect or manipulate the behavior of
mocks created via the MockTracker APIs.
The MockTracker class is used to manage mocking functionality. The test runner
module provides a top level mock export which is a MockTracker instance.
Each test also provides its own MockTracker instance via the test context's mock property.
An instance of SuiteContext is passed to each suite function in order to
interact with the test runner. However, the SuiteContext constructor is not
exposed as part of the API.
A successful call to run() will return a new TestsStream object, streaming a series of events representing the execution of the tests.
Functions #
This function creates a hook that runs after each test in the current suite.
The afterEach() hook is run even if the test fails.
Defines a new assertion function with the provided name and function. If an assertion already exists with the same name, it is overwritten.
The test() function is the value imported from the test module. Each
invocation of this function results in reporting the test to the TestsStream.
This function creates a hook that runs after each test in the current suite.
The afterEach() hook is run even if the test fails.
Defines a new assertion function with the provided name and function. If an assertion already exists with the same name, it is overwritten.
Shorthand for marking a suite as only. This is the same as calling describe with options.only set to true.
Shorthand for skipping a suite. This is the same as calling describe with options.skip set to true.
Shorthand for marking a suite as TODO. This is the same as calling describe with options.todo set to true.
Shorthand for marking a test as only. This is the same as calling it with options.only set to true.
Shorthand for skipping a test. This is the same as calling it with options.skip set to true.
Shorthand for marking a test as TODO. This is the same as calling it with options.todo set to true.
Shorthand for marking a test as only. This is the same as calling test with options.only set to true.
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.
Shorthand for skipping a test. This is the same as calling test with options.skip set to true.
This function is used to customize the default serialization mechanism used by the test runner.
This function is used to set a custom resolver for the location of the snapshot file used for snapshot testing.
By default, the snapshot filename is the same as the entry point filename with .snapshot appended.
Shorthand for marking a suite as only. This is the same as calling suite with options.only set to true.
Shorthand for skipping a suite. This is the same as calling suite with options.skip set to true.
Shorthand for marking a suite as TODO. This is the same as calling suite with options.todo set to true.
Shorthand for marking a test as TODO. This is the same as calling test with options.todo set to true.
Shorthand for marking a suite as only. This is the same as calling describe with options.only set to true.
Shorthand for skipping a suite. This is the same as calling describe with options.skip set to true.
Shorthand for marking a suite as TODO. This is the same as calling describe with options.todo set to true.
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.
This function is used to customize the default serialization mechanism used by the test runner.
This function is used to set a custom resolver for the location of the snapshot file used for snapshot testing.
By default, the snapshot filename is the same as the entry point filename with .snapshot appended.
Shorthand for marking a suite as only. This is the same as calling suite with options.only set to true.
Shorthand for skipping a suite. This is the same as calling suite with options.skip set to true.
Shorthand for marking a suite as TODO. This is the same as calling suite with options.todo set to true.
The test() function is the value imported from the test module. Each
invocation of this function results in reporting the test to the TestsStream.
This function creates a hook that runs after each test in the current suite.
The afterEach() hook is run even if the test fails.
Defines a new assertion function with the provided name and function. If an assertion already exists with the same name, it is overwritten.
Shorthand for marking a suite as only. This is the same as calling describe with options.only set to true.
Shorthand for skipping a suite. This is the same as calling describe with options.skip set to true.
Shorthand for marking a suite as TODO. This is the same as calling describe with options.todo set to true.
Shorthand for marking a test as only. This is the same as calling it with options.only set to true.
Shorthand for skipping a test. This is the same as calling it with options.skip set to true.
Shorthand for marking a test as TODO. This is the same as calling it with options.todo set to true.
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.
This function is used to customize the default serialization mechanism used by the test runner.
This function is used to set a custom resolver for the location of the snapshot file used for snapshot testing.
By default, the snapshot filename is the same as the entry point filename with .snapshot appended.
Shorthand for marking a suite as only. This is the same as calling suite with options.only set to true.
Shorthand for skipping a suite. This is the same as calling suite with options.skip set to true.
Shorthand for marking a suite as TODO. This is the same as calling suite with options.todo set to true.
Interfaces #
Namespaces #
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.
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.
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.
Type Aliases #
The hook function. The first argument is the context in which the hook is called. If the hook uses callbacks, the callback function is passed as the second argument.
The hook function. The first argument is a TestContext object.
If the hook uses callbacks, the callback function is passed as the second argument.
The type of a function passed to test. The first argument to this function is a TestContext object. If the test uses callbacks, the callback function is passed as the second argument.