This is a user-land port of node:test,
the experimental test runner introduced in Node.js 18. This module makes it
available in Node.js 14 and later.
Minimal dependencies, with full test suite.
Differences from the core implementation:
Doesn’t hide its own stack frames.
Some features require the use of --experimental-abortcontroller CLI flag to
work on Node.js v14.x. It’s recommended to pass
NODE_OPTIONS='--experimental-abortcontroller --no-warnings' in your env if
you are testing on v14.x.
Docs
Test runner
Stability: 1 - Experimental
The node:test module facilitates the creation of JavaScript tests.
To access it:
import test from 'test'
const test = require('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 Promise that is considered failing if the
Promise rejects, and is considered passing if the Promise resolves.
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 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.
Subtests
The test context’s test() method allows subtests to be created. This method
behaves identically to the top level test() function. The following example
demonstrates the creation of a top level test with two subtests.
test('top level test', async t => {
await t.test('subtest 1', t => {
assert.strictEqual(1, 1)
})
await t.test('subtest 2', t => {
assert.strictEqual(2, 2)
})
})
In this example, await is used to ensure that both subtests have completed.
This is necessary because parent tests do not wait for their subtests to
complete. Any subtests that are still outstanding when their parent finishes
are cancelled and treated as failures. Any subtest failures cause the parent
test to fail.
Skipping tests
Individual tests can be skipped by passing the skip option to the test, or by
calling the test context’s skip() method as shown in the
following example.
// The skip option is used, but no message is provided.
test('skip option', { skip: true }, t => {
// This code is never executed.
})
// The skip option is used, and a message is provided.
test('skip option with message', { skip: 'this is skipped' }, t => {
// This code is never executed.
})
test('skip() method', t => {
// Make sure to return here as well if the test contains additional logic.
t.skip()
})
test('skip() method with message', t => {
// Make sure to return here as well if the test contains additional logic.
t.skip('this is skipped')
})
describe/it syntax
Running tests can also be done using describe to declare a suite
and it to declare a test.
A suite is used to organize and group related tests together.
it is an alias for test, except there is no test context passed,
since nesting is done using suites.
describe and it are imported from the test module.
import { describe, it } from 'test';
const { describe, it } = require('test');
only tests
If node--test is started with the --test-only command-line option, it is
possible to skip all top level tests except for a selected subset by passing
the only option to the tests that should be run. When a test with the only
option set is run, all subtests are also run. The test context’s runOnly()
method can be used to implement the same behavior at the subtest level.
// Assume node--test is run with the --test-only command-line option.
// The 'only' option is set, so this test is run.
test('this test is run', { only: true }, async t => {
// Within this test, all subtests are run by default.
await t.test('running subtest')
// The test context can be updated to run subtests with the 'only' option.
t.runOnly(true)
await t.test('this subtest is now skipped')
await t.test('this subtest is run', { only: true })
// Switch the context back to execute all tests.
t.runOnly(false)
await t.test('this subtest is now run')
// Explicitly do not run these tests.
await t.test('skipped subtest 3', { only: false })
await t.test('skipped subtest 4', { skip: true })
})
// The 'only' option is not set, so this test is skipped.
test('this test is not run', () => {
// This code is not run.
throw new Error('fail')
})
Filtering tests by name
The [--test-name-pattern][] command-line option can be used to only run tests
whose name matches the provided pattern. Test name patterns are interpreted as
JavaScript regular expressions. The --test-name-pattern option can be
specified multiple times in order to run nested tests. For each test that is
executed, any corresponding test hooks, such as beforeEach(), are also
run.
Given the following test file, starting Node.js with the
--test-name-pattern="test [1-3]" option would cause the test runner to execute
test 1, test 2, and test 3. If test 1 did not match the test name
pattern, then its subtests would not execute, despite matching the pattern. The
same set of tests could also be executed by passing --test-name-pattern
multiple times (e.g. --test-name-pattern="test 1",
--test-name-pattern="test 2", etc.).
Test name patterns can also be specified using regular expression literals. This
allows regular expression flags to be used. In the previous example, starting
Node.js with --test-name-pattern="/test [4-5]/i" would match Test 4 and
Test 5 because the pattern is case-insensitive.
Test name patterns do not change the set of files that the test runner executes.
Extraneous asynchronous activity
Once a test function finishes executing, the results are reported as quickly
as possible while maintaining the order of the tests. However, it is possible
for the test function to generate asynchronous activity that outlives the test
itself. The test runner handles this type of activity, but does not delay the
reporting of test results in order to accommodate it.
In the following example, a test completes with two setImmediate()
operations still outstanding. The first setImmediate() attempts to create a
new subtest. Because the parent test has already finished and output its
results, the new subtest is immediately marked as failed, and reported later
to the {TestsStream}.
The second setImmediate() creates an uncaughtException event.
uncaughtException and unhandledRejection events originating from a completed
test are marked as failed by the test module and reported as diagnostic
warnings at the top level by the {TestsStream}.
test('a test that creates asynchronous activity', t => {
setImmediate(() => {
t.test('subtest that is created too late', t => {
throw new Error('error1')
})
})
setImmediate(() => {
throw new Error('error2')
})
// The test finishes after this line.
})
Running tests from the command line
The Node.js test runner can be invoked from the command line:
node--test
By default, Node.js will recursively search the current directory for
JavaScript source files matching a specific naming convention. Matching files
are executed as test files. More information on the expected test file naming
convention and behavior can be found in the test runner execution model
section.
Alternatively, one or more paths can be provided as the final argument(s) to
the Node.js command, as shown below.
In this example, the test runner will execute the files test1.js and
test2.mjs. The test runner will also recursively search the
custom_test_dir/ directory for test files to execute.
Test runner execution model
When searching for test files to execute, the test runner behaves as follows:
Any files explicitly provided by the user are executed.
If the user did not explicitly specify any paths, the current working
directory is recursively searched for files as specified in the following
steps.
node_modules directories are skipped unless explicitly provided by the
user.
If a directory named test is encountered, the test runner will search it
recursively for all all .js, .cjs, and .mjs files. All of these files
are treated as test files, and do not need to match the specific naming
convention detailed below. This is to accommodate projects that place all of
their tests in a single test directory.
In all other directories, .js, .cjs, and .mjs files matching the
following patterns are treated as test files:
^test$ - Files whose basename is the string 'test'. Examples:
test.js, test.cjs, test.mjs.
^test-.+ - Files whose basename starts with the string 'test-'
followed by one or more characters. Examples: test-example.js,
test-another-example.mjs.
.+[\.\-\_]test$ - Files whose basename ends with .test, -test, or
_test, preceded by one or more characters. Examples: example.test.js,
example-test.cjs, example_test.mjs.
Other file types understood by Node.js such as .node and .json are not
automatically executed by the test runner, but are supported if explicitly
provided on the command line.
Each matching test file is executed in a separate child process. If the child
process finishes with an exit code of 0, the test is considered passing.
Otherwise, the test is considered to be a failure. Test files must be
executable by Node.js, but are not required to use the node:test module
internally.
Mocking
The node:test module supports mocking during testing via a top-level mock
object. The following example creates a spy on a function that adds two numbers
together. The spy is then used to assert that the function was called as
expected.
import assert from 'node:assert';
import { mock, test } from 'test';
test('spies on a function', () => {
const sum = mock.fn((a, b) => {
return a + b;
});
assert.strictEqual(sum.mock.calls.length, 0);
assert.strictEqual(sum(3, 4), 7);
assert.strictEqual(sum.mock.calls.length, 1);
const call = sum.mock.calls[0];
assert.deepStrictEqual(call.arguments, [3, 4]);
assert.strictEqual(call.result, 7);
assert.strictEqual(call.error, undefined);
// Reset the globally tracked mocks.
mock.reset();
});
'use strict';
const assert = require('node:assert');
const { mock, test } = require('test');
test('spies on a function', () => {
const sum = mock.fn((a, b) => {
return a + b;
});
assert.strictEqual(sum.mock.calls.length, 0);
assert.strictEqual(sum(3, 4), 7);
assert.strictEqual(sum.mock.calls.length, 1);
const call = sum.mock.calls[0];
assert.deepStrictEqual(call.arguments, [3, 4]);
assert.strictEqual(call.result, 7);
assert.strictEqual(call.error, undefined);
// Reset the globally tracked mocks.
mock.reset();
});
The same mocking functionality is also exposed on the TestContext object
of each test. The following example creates a spy on an object method using the
API exposed on the TestContext. The benefit of mocking via the test context is
that the test runner will automatically restore all mocked functionality once
the test finishes.
The node:test module supports passing [--test-reporter][]
flags for the test runner to use a specific reporter.
The following built-reporters are supported:
tap
The tap reporter is the default reporter used by the test runner. It outputs
the test results in the TAP format.
spec
The spec reporter outputs the test results in a human-readable format.
dot
The dot reporter outputs the test results in a compact format,
where each passing test is represented by a .,
and each failing test is represented by a X.
Custom reporters
[--test-reporter][] can be used to specify a path to custom reporter.
a custom reporter is a module that exports a value
accepted by [stream.compose][].
Reporters should transform events emitted by a {TestsStream}
Example of a custom reporter using {stream.Transform}:
import { Transform } from 'node:stream';
const customReporter = new Transform({
writableObjectMode: true,
transform(event, encoding, callback) {
switch (event.type) {
case 'test:start':
callback(null, `test ${event.data.name} started`);
break;
case 'test:pass':
callback(null, `test ${event.data.name} passed`);
break;
case 'test:fail':
callback(null, `test ${event.data.name} failed`);
break;
case 'test:plan':
callback(null, 'test plan');
break;
case 'test:diagnostic':
callback(null, event.data.message);
break;
}
},
});
export default customReporter;
const { Transform } = require('node:stream');
const customReporter = new Transform({
writableObjectMode: true,
transform(event, encoding, callback) {
switch (event.type) {
case 'test:start':
callback(null, `test ${event.data.name} started`);
break;
case 'test:pass':
callback(null, `test ${event.data.name} passed`);
break;
case 'test:fail':
callback(null, `test ${event.data.name} failed`);
break;
case 'test:plan':
callback(null, 'test plan');
break;
case 'test:diagnostic':
callback(null, event.data.message);
break;
}
},
});
module.exports = customReporter;
Example of a custom reporter using a generator function:
export default async function * customReporter(source) {
for await (const event of source) {
switch (event.type) {
case 'test:start':
yield `test ${event.data.name} started\n`;
break;
case 'test:pass':
yield `test ${event.data.name} passed\n`;
break;
case 'test:fail':
yield `test ${event.data.name} failed\n`;
break;
case 'test:plan':
yield 'test plan';
break;
case 'test:diagnostic':
yield `${event.data.message}\n`;
break;
}
}
}
module.exports = async function * customReporter(source) {
for await (const event of source) {
switch (event.type) {
case 'test:start':
yield `test ${event.data.name} started\n`;
break;
case 'test:pass':
yield `test ${event.data.name} passed\n`;
break;
case 'test:fail':
yield `test ${event.data.name} failed\n`;
break;
case 'test:plan':
yield 'test plan\n';
break;
case 'test:diagnostic':
yield `${event.data.message}\n`;
break;
}
}
};
The value provided to --test-reporter should be a string like one used in an
import() in JavaScript code, or a value provided for [--import][].
Multiple reporters
The [--test-reporter][] flag can be specified multiple times to report test
results in several formats. In this situation
it is required to specify a destination for each reporter
using [--test-reporter-destination][].
Destination can be stdout, stderr, or a file path.
Reporters and destinations are paired according
to the order they were specified.
In the following example, the spec reporter will output to stdout,
and the dot reporter will output to file.txt:
When a single reporter is specified, the destination will default to stdout,
unless a destination is explicitly provided.
run([options])
options {Object} Configuration options for running tests. The following
properties are supported:
concurrency {number|boolean} If a number is provided,
then that many files would run in parallel.
If true, it would run os.availableParallelism() - 1 test files in
parallel.
If false, it would only run one test file at a time.
Default:false.
files: {Array} An array containing the list of files to run.
Default matching files from test runner execution model.
signal {AbortSignal} Allows aborting an in-progress test execution.
timeout {number} A number of milliseconds the test execution will
fail after.
If unspecified, subtests inherit this value from their parent.
Default:Infinity.
inspectPort {number|Function} Sets inspector port of test child process.
This can be a number, or a function that takes no arguments and returns a
number. If a nullish value is provided, each process gets its own port,
incremented from the primary’s process.debugPort.
Default:undefined.
name {string} The name of the test, which is displayed when reporting test
results. Default: The name property of fn, or '<anonymous>' if fn
does not have a name.
options {Object} Configuration options for the test. The following
properties are supported:
concurrency {number|boolean} If a number is provided,
then that many tests would run in parallel.
If true, it would run os.availableParallelism() - 1 tests in parallel.
For subtests, it will be Infinity tests in parallel.
If false, it would only run one test at a time.
If unspecified, subtests inherit this value from their parent.
Default:false.
only {boolean} If truthy, and the test context is configured to run
only tests, then this test will be run. Otherwise, the test is skipped.
Default:false.
signal {AbortSignal} Allows aborting an in-progress test.
skip {boolean|string} If truthy, the test is skipped. If a string is
provided, that string is displayed in the test results as the reason for
skipping the test. Default:false.
todo {boolean|string} If truthy, the test marked as TODO. If a string
is provided, that string is displayed in the test results as the reason why
the test is TODO. Default:false.
timeout {number} A number of milliseconds the test will fail after.
If unspecified, subtests inherit this value from their parent.
Default:Infinity.
fn {Function|AsyncFunction} The function under 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. Default: A no-op
function.
Returns: {Promise} Resolved with undefined once the test completes.
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 resolves once the test completes. 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.
describe([name][, options][, fn])
name {string} The name of the suite, which is displayed when reporting test
results. Default: The name property of fn, or '<anonymous>' if fn
does not have a name.
options {Object} Configuration options for the suite.
supports the same options as test([name][, options][, fn]).
fn {Function|AsyncFunction} The function under suite
declaring all subtests and subsuites.
The first argument to this function is a SuiteContext object.
Default: A no-op function.
Returns: undefined.
The describe() function imported from the test module. Each
invocation of this function results in the creation of a Subtest.
After invocation of top level describe functions,
all top level tests and suites will execute.
name {string} The name of the test, which is displayed when reporting test
results. Default: The name property of fn, or '<anonymous>' if fn
does not have a name.
options {Object} Configuration options for the suite.
supports the same options as test([name][, options][, fn]).
fn {Function|AsyncFunction} The function under test.
If the test uses callbacks, the callback function is passed as an argument.
Default: A no-op function.
Returns: undefined.
The it() function is the value imported from the test module.
fn {Function|AsyncFunction} The hook function.
If the hook uses callbacks,
the callback function is passed as the second argument. Default: A no-op
function.
options {Object} Configuration options for the hook. The following
properties are supported:
signal {AbortSignal} Allows aborting an in-progress hook.
timeout {number} A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
Default:Infinity.
This function is used to create a hook running before running a suite.
describe('tests', async () => {
before(() => console.log('about to run some test'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
});
});
after([, fn][, options])
fn {Function|AsyncFunction} The hook function.
If the hook uses callbacks,
the callback function is passed as the second argument. Default: A no-op
function.
options {Object} Configuration options for the hook. The following
properties are supported:
signal {AbortSignal} Allows aborting an in-progress hook.
timeout {number} A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
Default:Infinity.
This function is used to create a hook running after running a suite.
fn {Function|AsyncFunction} The hook function.
If the hook uses callbacks,
the callback function is passed as the second argument. Default: A no-op
function.
options {Object} Configuration options for the hook. The following
properties are supported:
signal {AbortSignal} Allows aborting an in-progress hook.
timeout {number} A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
Default:Infinity.
This function is used to create a hook running
before each subtest of the current suite.
describe('tests', async () => {
beforeEach(() => t.diagnostics('about to run a test'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
});
});
afterEach([, fn][, options])
fn {Function|AsyncFunction} The hook function.
If the hook uses callbacks,
the callback function is passed as the second argument. Default: A no-op
function.
options {Object} Configuration options for the hook. The following
properties are supported:
signal {AbortSignal} Allows aborting an in-progress hook.
timeout {number} A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
Default:Infinity.
This function is used to create a hook running
after each subtest of the current test.
describe('tests', async () => {
afterEach(() => t.diagnostics('about to run a test'));
it('is a subtest', () => {
assert.ok('some relevant assertion here');
});
});
Class: MockFunctionContext
The MockFunctionContext class is used to inspect or manipulate the behavior of
mocks created via the MockTracker APIs.
ctx.calls
{Array}
A getter that returns a copy of the internal array used to track calls to the
mock. Each entry in the array is an object with the following properties.
arguments {Array} An array of the arguments passed to the mock function.
error {any} If the mocked function threw then this property contains the
thrown value. Default:undefined.
result {any} The value returned by the mocked function.
stack {Error} An Error object whose stack can be used to determine the
callsite of the mocked function invocation.
target {Function|undefined} If the mocked function is a constructor, this
field contains the class being constructed. Otherwise this will be
undefined.
this {any} The mocked function’s this value.
ctx.callCount()
Returns: {integer} The number of times that this mock has been invoked.
This function returns the number of times that this mock has been invoked. This
function is more efficient than checking ctx.calls.length because ctx.calls
is a getter that creates a copy of the internal call tracking array.
ctx.mockImplementation(implementation)
implementation {Function|AsyncFunction} The function to be used as the
mock’s new implementation.
This function is used to change the behavior of an existing mock.
The following example creates a mock function using t.mock.fn(), calls the
mock function, and then changes the mock implementation to a different function.
test('changes a mock behavior', (t) => {
let cnt = 0;
function addOne() {
cnt++;
return cnt;
}
function addTwo() {
cnt += 2;
return cnt;
}
const fn = t.mock.fn(addOne);
assert.strictEqual(fn(), 1);
fn.mock.mockImplementation(addTwo);
assert.strictEqual(fn(), 3);
assert.strictEqual(fn(), 5);
});
implementation {Function|AsyncFunction} The function to be used as the
mock’s implementation for the invocation number specified by onCall.
onCall {integer} The invocation number that will use implementation. If
the specified invocation has already occurred then an exception is thrown.
Default: The number of the next invocation.
This function is used to change the behavior of an existing mock for a single
invocation. Once invocation onCall has occurred, the mock will revert to
whatever behavior it would have used had mockImplementationOnce() not been
called.
The following example creates a mock function using t.mock.fn(), calls the
mock function, changes the mock implementation to a different function for the
next invocation, and then resumes its previous behavior.
test('changes a mock behavior once', (t) => {
let cnt = 0;
function addOne() {
cnt++;
return cnt;
}
function addTwo() {
cnt += 2;
return cnt;
}
const fn = t.mock.fn(addOne);
assert.strictEqual(fn(), 1);
fn.mock.mockImplementationOnce(addTwo);
assert.strictEqual(fn(), 3);
assert.strictEqual(fn(), 4);
});
ctx.restore()
Resets the implementation of the mock function to its original behavior. The
mock can still be used after calling this function.
Class: MockTracker
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.
mock.fn([original[, implementation]][, options])
original {Function|AsyncFunction} An optional function to create a mock on.
Default: A no-op function.
implementation {Function|AsyncFunction} An optional function used as the
mock implementation for original. This is useful for creating mocks that
exhibit one behavior for a specified number of calls and then restore the
behavior of original. Default: The function specified by original.
options {Object} Optional configuration options for the mock function. The
following properties are supported:
times {integer} The number of times that the mock will use the behavior of
implementation. Once the mock function has been called times times, it
will automatically restore the behavior of original. This value must be an
integer greater than zero. Default:Infinity.
Returns: {Proxy} The mocked function. The mocked function contains a special
mock property, which is an instance of MockFunctionContext, and can
be used for inspecting and changing the behavior of the mocked function.
This function is used to create a mock function.
The following example creates a mock function that increments a counter by one
on each invocation. The times option is used to modify the mock behavior such
that the first two invocations add two to the counter instead of one.
object {Object} The object whose method is being mocked.
methodName {string|symbol} The identifier of the method on object to mock.
If object[methodName] is not a function, an error is thrown.
implementation {Function|AsyncFunction} An optional function used as the
mock implementation for object[methodName]. Default: The original method
specified by object[methodName].
options {Object} Optional configuration options for the mock method. The
following properties are supported:
getter {boolean} If true, object[methodName] is treated as a getter.
This option cannot be used with the setter option. Default: false.
setter {boolean} If true, object[methodName] is treated as a setter.
This option cannot be used with the getter option. Default: false.
times {integer} The number of times that the mock will use the behavior of
implementation. Once the mocked method has been called times times, it
will automatically restore the original behavior. This value must be an
integer greater than zero. Default:Infinity.
Returns: {Proxy} The mocked method. The mocked method contains a special
mock property, which is an instance of MockFunctionContext, and can
be used for inspecting and changing the behavior of the mocked method.
This function is used to create a mock on an existing object method. The
following example demonstrates how a mock is created on an existing object
method.
This function restores the default behavior of all mocks that were previously
created by this MockTracker and disassociates the mocks from the
MockTracker instance. Once disassociated, the mocks can still be used, but the
MockTracker instance can no longer be used to reset their behavior or
otherwise interact with them.
After each test completes, this function is called on the test context’s
MockTracker. If the global MockTracker is used extensively, calling this
function manually is recommended.
mock.restoreAll()
This function restores the default behavior of all mocks that were previously
created by this MockTracker. Unlike mock.reset(), mock.restoreAll() does
not disassociate the mocks from the MockTracker instance.
This function is syntax sugar for MockTracker.method with options.setter
set to true.
Class: TestsStream
Extends {ReadableStream}
A successful call to run() method will return a new {TestsStream}
object, streaming a series of events representing the execution of the tests.
TestsStream will emit events, in the order of the tests definition
Event: 'test:diagnostic'
data {Object}
file {string|undefined} The path of the test file,
undefined if test is not ran through a file.
duration {number} The duration of the test in milliseconds.
error {Error} The error thrown by the test.
file {string|undefined} The path of the test file,
undefined if test is not ran through a file.
name {string} The test name.
nesting {number} The nesting level of the test.
testNumber {number} The ordinal number of the test.
todo {string|boolean|undefined} Present if context.todo is called
skip {string|boolean|undefined} Present if context.skip is called
Emitted when a test fails.
Event: 'test:pass'
data {Object}
details {Object} Additional execution metadata.
duration {number} The duration of the test in milliseconds.
file {string|undefined} The path of the test file,
undefined if test is not ran through a file.
name {string} The test name.
nesting {number} The nesting level of the test.
testNumber {number} The ordinal number of the test.
todo {string|boolean|undefined} Present if context.todo is called
skip {string|boolean|undefined} Present if context.skip is called
Emitted when a test passes.
Event: 'test:plan'
data {Object}
file {string|undefined} The path of the test file,
undefined if test is not ran through a file.
nesting {number} The nesting level of the test.
count {number} The number of subtests that have ran.
Emitted when all subtests have completed for a given test.
Event: 'test:start'
data {Object}
file {string|undefined} The path of the test file,
undefined if test is not ran through a file.
name {string} The test name.
nesting {number} The nesting level of the test.
Emitted when a test starts.
Class: TestContext
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.
context.beforeEach([, fn][, options])
fn {Function|AsyncFunction} The 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. Default: A no-op
function.
options {Object} Configuration options for the hook. The following
properties are supported:
signal {AbortSignal} Allows aborting an in-progress hook.
timeout {number} A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
Default:Infinity.
This function is used to create a hook running
before each subtest of the current test.
test('top level test', async (t) => {
t.beforeEach((t) => t.diagnostics(`about to run ${t.name}`));
await t.test(
'This is a subtest',
(t) => {
assert.ok('some relevant assertion here');
}
);
});
context.after([fn][, options])
fn {Function|AsyncFunction} The 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. Default: A no-op
function.
options {Object} Configuration options for the hook. The following
properties are supported:
signal {AbortSignal} Allows aborting an in-progress hook.
timeout {number} A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
Default:Infinity.
This function is used to create a hook that runs after the current test
finishes.
fn {Function|AsyncFunction} The 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. Default: A no-op
function.
options {Object} Configuration options for the hook. The following
properties are supported:
signal {AbortSignal} Allows aborting an in-progress hook.
timeout {number} A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
Default:Infinity.
This function is used to create a hook running
after each subtest of the current test.
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.
context.name
The name of the test.
context.runOnly(shouldRunOnlyTests)
shouldRunOnlyTests {boolean} Whether or not to run only tests.
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.
context.signal
AbortSignal Can be used to abort test subtasks when the test has been aborted.
Warning
On Node.js v14.x, this feature won’t be available unless you pass the
--experimental-abortcontroller CLI flag or added an external global polyfill
for AbortController.
This function causes the test’s output to indicate the test as skipped. If
message is provided, it is included in the output. Calling skip() does
not terminate execution of the test function. This function does not return a
value.
context.todo([message])
message {string} Optional TODO message.
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.
context.test([name][, options][, fn])
name {string} The name of the subtest, which is displayed when reporting
test results. Default: The name property of fn, or '<anonymous>' if
fn does not have a name.
options {Object} Configuration options for the subtest. The following
properties are supported:
concurrency {number|boolean|null} If a number is provided,
then that many tests would run in parallel.
If true, it would run all subtests in parallel.
If false, it would only run one test at a time.
If unspecified, subtests inherit this value from their parent.
Default:null.
only {boolean} If truthy, and the test context is configured to run
only tests, then this test will be run. Otherwise, the test is skipped.
Default:false.
skip {boolean|string} If truthy, the test is skipped. If a string is
provided, that string is displayed in the test results as the reason for
skipping the test. Default:false.
signal {AbortSignal} Allows aborting an in-progress test.
todo {boolean|string} If truthy, the test marked as TODO. If a string
is provided, that string is displayed in the test results as the reason why
the test is TODO. Default:false.
timeout {number} A number of milliseconds the test will fail after.
If unspecified, subtests inherit this value from their parent.
Default:Infinity.
fn {Function|AsyncFunction} The function under 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. Default: A no-op
function.
Returns: {Promise} Resolved with undefined once the test completes.
This function is used to create subtests under the current test. This function
behaves in the same fashion as the top level test() function.
Class: SuiteContext
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.
context.name
The name of the suite.
context.signal
AbortSignal Can be used to abort test subtasks when the test has been aborted.
Warning
On Node.js v14.x, this feature won’t be available unless you pass the
--experimental-abortcontroller CLI flag or added an external global polyfill
for AbortController.
The
testnpm packageThis is a user-land port of
node:test, the experimental test runner introduced in Node.js 18. This module makes it available in Node.js 14 and later.Minimal dependencies, with full test suite.
Differences from the core implementation:
--experimental-abortcontrollerCLI flag to work on Node.js v14.x. It’s recommended to passNODE_OPTIONS='--experimental-abortcontroller --no-warnings'in your env if you are testing on v14.x.Docs
Test runner
The
node:testmodule facilitates the creation of JavaScript tests. To access it:Tests created via the
testmodule consist of a single function that is processed in one of three ways:Promisethat is considered failing if thePromiserejects, and is considered passing if thePromiseresolves.Promise, the test will fail.The following example illustrates how tests are written using the
testmodule.If any tests fail, the process exit code is set to
1.Subtests
The test context’s
test()method allows subtests to be created. This method behaves identically to the top leveltest()function. The following example demonstrates the creation of a top level test with two subtests.In this example,
awaitis used to ensure that both subtests have completed. This is necessary because parent tests do not wait for their subtests to complete. Any subtests that are still outstanding when their parent finishes are cancelled and treated as failures. Any subtest failures cause the parent test to fail.Skipping tests
Individual tests can be skipped by passing the
skipoption to the test, or by calling the test context’sskip()method as shown in the following example.describe/itsyntaxRunning tests can also be done using
describeto declare a suite anditto declare a test. A suite is used to organize and group related tests together.itis an alias fortest, except there is no test context passed, since nesting is done using suites.describeanditare imported from thetestmodule.onlytestsIf
node--testis started with the--test-onlycommand-line option, it is possible to skip all top level tests except for a selected subset by passing theonlyoption to the tests that should be run. When a test with theonlyoption set is run, all subtests are also run. The test context’srunOnly()method can be used to implement the same behavior at the subtest level.Filtering tests by name
The [
--test-name-pattern][] command-line option can be used to only run tests whose name matches the provided pattern. Test name patterns are interpreted as JavaScript regular expressions. The--test-name-patternoption can be specified multiple times in order to run nested tests. For each test that is executed, any corresponding test hooks, such asbeforeEach(), are also run.Given the following test file, starting Node.js with the
--test-name-pattern="test [1-3]"option would cause the test runner to executetest 1,test 2, andtest 3. Iftest 1did not match the test name pattern, then its subtests would not execute, despite matching the pattern. The same set of tests could also be executed by passing--test-name-patternmultiple times (e.g.--test-name-pattern="test 1",--test-name-pattern="test 2", etc.).Test name patterns can also be specified using regular expression literals. This allows regular expression flags to be used. In the previous example, starting Node.js with
--test-name-pattern="/test [4-5]/i"would matchTest 4andTest 5because the pattern is case-insensitive.Test name patterns do not change the set of files that the test runner executes.
Extraneous asynchronous activity
Once a test function finishes executing, the results are reported as quickly as possible while maintaining the order of the tests. However, it is possible for the test function to generate asynchronous activity that outlives the test itself. The test runner handles this type of activity, but does not delay the reporting of test results in order to accommodate it.
In the following example, a test completes with two
setImmediate()operations still outstanding. The firstsetImmediate()attempts to create a new subtest. Because the parent test has already finished and output its results, the new subtest is immediately marked as failed, and reported later to the {TestsStream}.The second
setImmediate()creates anuncaughtExceptionevent.uncaughtExceptionandunhandledRejectionevents originating from a completed test are marked as failed by thetestmodule and reported as diagnostic warnings at the top level by the {TestsStream}.Running tests from the command line
The Node.js test runner can be invoked from the command line:
By default, Node.js will recursively search the current directory for JavaScript source files matching a specific naming convention. Matching files are executed as test files. More information on the expected test file naming convention and behavior can be found in the test runner execution model section.
Alternatively, one or more paths can be provided as the final argument(s) to the Node.js command, as shown below.
In this example, the test runner will execute the files
test1.jsandtest2.mjs. The test runner will also recursively search thecustom_test_dir/directory for test files to execute.Test runner execution model
When searching for test files to execute, the test runner behaves as follows:
node_modulesdirectories are skipped unless explicitly provided by the user.testis encountered, the test runner will search it recursively for all all.js,.cjs, and.mjsfiles. All of these files are treated as test files, and do not need to match the specific naming convention detailed below. This is to accommodate projects that place all of their tests in a singletestdirectory..js,.cjs, and.mjsfiles matching the following patterns are treated as test files:^test$- Files whose basename is the string'test'. Examples:test.js,test.cjs,test.mjs.^test-.+- Files whose basename starts with the string'test-'followed by one or more characters. Examples:test-example.js,test-another-example.mjs..+[\.\-\_]test$- Files whose basename ends with.test,-test, or_test, preceded by one or more characters. Examples:example.test.js,example-test.cjs,example_test.mjs..nodeand.jsonare not automatically executed by the test runner, but are supported if explicitly provided on the command line.Each matching test file is executed in a separate child process. If the child process finishes with an exit code of 0, the test is considered passing. Otherwise, the test is considered to be a failure. Test files must be executable by Node.js, but are not required to use the
node:testmodule internally.Mocking
The
node:testmodule supports mocking during testing via a top-levelmockobject. The following example creates a spy on a function that adds two numbers together. The spy is then used to assert that the function was called as expected.The same mocking functionality is also exposed on the
TestContextobject of each test. The following example creates a spy on an object method using the API exposed on theTestContext. The benefit of mocking via the test context is that the test runner will automatically restore all mocked functionality once the test finishes.Test reporters
The
node:testmodule supports passing [--test-reporter][] flags for the test runner to use a specific reporter.The following built-reporters are supported:
tapThetapreporter is the default reporter used by the test runner. It outputs the test results in the TAP format.specThespecreporter outputs the test results in a human-readable format.dotThedotreporter outputs the test results in a compact format, where each passing test is represented by a., and each failing test is represented by aX.Custom reporters
[
--test-reporter][] can be used to specify a path to custom reporter. a custom reporter is a module that exports a value accepted by [stream.compose][]. Reporters should transform events emitted by a {TestsStream}Example of a custom reporter using {stream.Transform}:
Example of a custom reporter using a generator function:
The value provided to
--test-reportershould be a string like one used in animport()in JavaScript code, or a value provided for [--import][].Multiple reporters
The [
--test-reporter][] flag can be specified multiple times to report test results in several formats. In this situation it is required to specify a destination for each reporter using [--test-reporter-destination][]. Destination can bestdout,stderr, or a file path. Reporters and destinations are paired according to the order they were specified.In the following example, the
specreporter will output tostdout, and thedotreporter will output tofile.txt:When a single reporter is specified, the destination will default to
stdout, unless a destination is explicitly provided.run([options])options{Object} Configuration options for running tests. The following properties are supported:concurrency{number|boolean} If a number is provided, then that many files would run in parallel. Iftrue, it would runos.availableParallelism() - 1test files in parallel. Iffalse, it would only run one test file at a time. Default:false.files: {Array} An array containing the list of files to run. Default matching files from test runner execution model.signal{AbortSignal} Allows aborting an in-progress test execution.timeout{number} A number of milliseconds the test execution will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity.inspectPort{number|Function} Sets inspector port of test child process. This can be a number, or a function that takes no arguments and returns a number. If a nullish value is provided, each process gets its own port, incremented from the primary’sprocess.debugPort. Default:undefined.test([name][, options][, fn])name{string} The name of the test, which is displayed when reporting test results. Default: Thenameproperty offn, or'<anonymous>'iffndoes not have a name.options{Object} Configuration options for the test. The following properties are supported:concurrency{number|boolean} If a number is provided, then that many tests would run in parallel. Iftrue, it would runos.availableParallelism() - 1tests in parallel. For subtests, it will beInfinitytests in parallel. Iffalse, it would only run one test at a time. If unspecified, subtests inherit this value from their parent. Default:false.only{boolean} If truthy, and the test context is configured to runonlytests, then this test will be run. Otherwise, the test is skipped. Default:false.signal{AbortSignal} Allows aborting an in-progress test.skip{boolean|string} If truthy, the test is skipped. If a string is provided, that string is displayed in the test results as the reason for skipping the test. Default:false.todo{boolean|string} If truthy, the test marked asTODO. If a string is provided, that string is displayed in the test results as the reason why the test isTODO. Default:false.timeout{number} A number of milliseconds the test will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity.fn{Function|AsyncFunction} The function under test. The first argument to this function is aTestContextobject. If the test uses callbacks, the callback function is passed as the second argument. Default: A no-op function.undefinedonce the test completes.The
test()function is the value imported from thetestmodule. Each invocation of this function results in reporting the test to the {TestsStream}.The
TestContextobject passed to thefnargument 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 aPromisethat resolves once the test completes. 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.The
timeoutoption can be used to fail the test if it takes longer thantimeoutmilliseconds 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.describe([name][, options][, fn])name{string} The name of the suite, which is displayed when reporting test results. Default: Thenameproperty offn, or'<anonymous>'iffndoes not have a name.options{Object} Configuration options for the suite. supports the same options astest([name][, options][, fn]).fn{Function|AsyncFunction} The function under suite declaring all subtests and subsuites. The first argument to this function is aSuiteContextobject. Default: A no-op function.undefined.The
describe()function imported from thetestmodule. Each invocation of this function results in the creation of a Subtest. After invocation of top leveldescribefunctions, all top level tests and suites will execute.describe.skip([name][, options][, fn])Shorthand for skipping a suite, same as
describe([name], { skip: true }[, fn]).describe.todo([name][, options][, fn])Shorthand for marking a suite as
TODO, same asdescribe([name], { todo: true }[, fn]).it([name][, options][, fn])name{string} The name of the test, which is displayed when reporting test results. Default: Thenameproperty offn, or'<anonymous>'iffndoes not have a name.options{Object} Configuration options for the suite. supports the same options astest([name][, options][, fn]).fn{Function|AsyncFunction} The function under test. If the test uses callbacks, the callback function is passed as an argument. Default: A no-op function.undefined.The
it()function is the value imported from thetestmodule.it.skip([name][, options][, fn])Shorthand for skipping a test, same as
it([name], { skip: true }[, fn]).it.todo([name][, options][, fn])Shorthand for marking a test as
TODO, same asit([name], { todo: true }[, fn]).before([, fn][, options])fn{Function|AsyncFunction} The hook function. If the hook uses callbacks, the callback function is passed as the second argument. Default: A no-op function.options{Object} Configuration options for the hook. The following properties are supported:signal{AbortSignal} Allows aborting an in-progress hook.timeout{number} A number of milliseconds the hook will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity.This function is used to create a hook running before running a suite.
after([, fn][, options])fn{Function|AsyncFunction} The hook function. If the hook uses callbacks, the callback function is passed as the second argument. Default: A no-op function.options{Object} Configuration options for the hook. The following properties are supported:signal{AbortSignal} Allows aborting an in-progress hook.timeout{number} A number of milliseconds the hook will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity.This function is used to create a hook running after running a suite.
beforeEach([, fn][, options])fn{Function|AsyncFunction} The hook function. If the hook uses callbacks, the callback function is passed as the second argument. Default: A no-op function.options{Object} Configuration options for the hook. The following properties are supported:signal{AbortSignal} Allows aborting an in-progress hook.timeout{number} A number of milliseconds the hook will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity.This function is used to create a hook running before each subtest of the current suite.
afterEach([, fn][, options])fn{Function|AsyncFunction} The hook function. If the hook uses callbacks, the callback function is passed as the second argument. Default: A no-op function.options{Object} Configuration options for the hook. The following properties are supported:signal{AbortSignal} Allows aborting an in-progress hook.timeout{number} A number of milliseconds the hook will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity.This function is used to create a hook running after each subtest of the current test.
Class:
MockFunctionContextThe
MockFunctionContextclass is used to inspect or manipulate the behavior of mocks created via theMockTrackerAPIs.ctx.callsA getter that returns a copy of the internal array used to track calls to the mock. Each entry in the array is an object with the following properties.
arguments{Array} An array of the arguments passed to the mock function.error{any} If the mocked function threw then this property contains the thrown value. Default:undefined.result{any} The value returned by the mocked function.stack{Error} AnErrorobject whose stack can be used to determine the callsite of the mocked function invocation.target{Function|undefined} If the mocked function is a constructor, this field contains the class being constructed. Otherwise this will beundefined.this{any} The mocked function’sthisvalue.ctx.callCount()This function returns the number of times that this mock has been invoked. This function is more efficient than checking
ctx.calls.lengthbecausectx.callsis a getter that creates a copy of the internal call tracking array.ctx.mockImplementation(implementation)implementation{Function|AsyncFunction} The function to be used as the mock’s new implementation.This function is used to change the behavior of an existing mock.
The following example creates a mock function using
t.mock.fn(), calls the mock function, and then changes the mock implementation to a different function.ctx.mockImplementationOnce(implementation[, onCall])implementation{Function|AsyncFunction} The function to be used as the mock’s implementation for the invocation number specified byonCall.onCall{integer} The invocation number that will useimplementation. If the specified invocation has already occurred then an exception is thrown. Default: The number of the next invocation.This function is used to change the behavior of an existing mock for a single invocation. Once invocation
onCallhas occurred, the mock will revert to whatever behavior it would have used hadmockImplementationOnce()not been called.The following example creates a mock function using
t.mock.fn(), calls the mock function, changes the mock implementation to a different function for the next invocation, and then resumes its previous behavior.ctx.restore()Resets the implementation of the mock function to its original behavior. The mock can still be used after calling this function.
Class:
MockTrackerThe
MockTrackerclass is used to manage mocking functionality. The test runner module provides a top levelmockexport which is aMockTrackerinstance. Each test also provides its ownMockTrackerinstance via the test context’smockproperty.mock.fn([original[, implementation]][, options])original{Function|AsyncFunction} An optional function to create a mock on. Default: A no-op function.implementation{Function|AsyncFunction} An optional function used as the mock implementation fororiginal. This is useful for creating mocks that exhibit one behavior for a specified number of calls and then restore the behavior oforiginal. Default: The function specified byoriginal.options{Object} Optional configuration options for the mock function. The following properties are supported:times{integer} The number of times that the mock will use the behavior ofimplementation. Once the mock function has been calledtimestimes, it will automatically restore the behavior oforiginal. This value must be an integer greater than zero. Default:Infinity.mockproperty, which is an instance ofMockFunctionContext, and can be used for inspecting and changing the behavior of the mocked function.This function is used to create a mock function.
The following example creates a mock function that increments a counter by one on each invocation. The
timesoption is used to modify the mock behavior such that the first two invocations add two to the counter instead of one.mock.getter(object, methodName[, implementation][, options])This function is syntax sugar for
MockTracker.methodwithoptions.getterset totrue.mock.method(object, methodName[, implementation][, options])object{Object} The object whose method is being mocked.methodName{string|symbol} The identifier of the method onobjectto mock. Ifobject[methodName]is not a function, an error is thrown.implementation{Function|AsyncFunction} An optional function used as the mock implementation forobject[methodName]. Default: The original method specified byobject[methodName].options{Object} Optional configuration options for the mock method. The following properties are supported:getter{boolean} Iftrue,object[methodName]is treated as a getter. This option cannot be used with thesetteroption. Default: false.setter{boolean} Iftrue,object[methodName]is treated as a setter. This option cannot be used with thegetteroption. Default: false.times{integer} The number of times that the mock will use the behavior ofimplementation. Once the mocked method has been calledtimestimes, it will automatically restore the original behavior. This value must be an integer greater than zero. Default:Infinity.mockproperty, which is an instance ofMockFunctionContext, and can be used for inspecting and changing the behavior of the mocked method.This function is used to create a mock on an existing object method. The following example demonstrates how a mock is created on an existing object method.
mock.reset()This function restores the default behavior of all mocks that were previously created by this
MockTrackerand disassociates the mocks from theMockTrackerinstance. Once disassociated, the mocks can still be used, but theMockTrackerinstance can no longer be used to reset their behavior or otherwise interact with them.After each test completes, this function is called on the test context’s
MockTracker. If the globalMockTrackeris used extensively, calling this function manually is recommended.mock.restoreAll()This function restores the default behavior of all mocks that were previously created by this
MockTracker. Unlikemock.reset(),mock.restoreAll()does not disassociate the mocks from theMockTrackerinstance.mock.setter(object, methodName[, implementation][, options])This function is syntax sugar for
MockTracker.methodwithoptions.setterset totrue.Class:
TestsStreamA successful call to
run()method will return a new {TestsStream} object, streaming a series of events representing the execution of the tests.TestsStreamwill emit events, in the order of the tests definitionEvent:
'test:diagnostic'data{Object}file{string|undefined} The path of the test file, undefined if test is not ran through a file.message{string} The diagnostic message.nesting{number} The nesting level of the test.Emitted when
context.diagnosticis called.Event:
'test:fail'data{Object}details{Object} Additional execution metadata.duration{number} The duration of the test in milliseconds.error{Error} The error thrown by the test.file{string|undefined} The path of the test file, undefined if test is not ran through a file.name{string} The test name.nesting{number} The nesting level of the test.testNumber{number} The ordinal number of the test.todo{string|boolean|undefined} Present ifcontext.todois calledskip{string|boolean|undefined} Present ifcontext.skipis calledEmitted when a test fails.
Event:
'test:pass'data{Object}details{Object} Additional execution metadata.duration{number} The duration of the test in milliseconds.file{string|undefined} The path of the test file, undefined if test is not ran through a file.name{string} The test name.nesting{number} The nesting level of the test.testNumber{number} The ordinal number of the test.todo{string|boolean|undefined} Present ifcontext.todois calledskip{string|boolean|undefined} Present ifcontext.skipis calledEmitted when a test passes.
Event:
'test:plan'data{Object}file{string|undefined} The path of the test file, undefined if test is not ran through a file.nesting{number} The nesting level of the test.count{number} The number of subtests that have ran.Emitted when all subtests have completed for a given test.
Event:
'test:start'data{Object}file{string|undefined} The path of the test file, undefined if test is not ran through a file.name{string} The test name.nesting{number} The nesting level of the test.Emitted when a test starts.
Class:
TestContextAn instance of
TestContextis passed to each test function in order to interact with the test runner. However, theTestContextconstructor is not exposed as part of the API.context.beforeEach([, fn][, options])fn{Function|AsyncFunction} The hook function. The first argument to this function is aTestContextobject. If the hook uses callbacks, the callback function is passed as the second argument. Default: A no-op function.options{Object} Configuration options for the hook. The following properties are supported:signal{AbortSignal} Allows aborting an in-progress hook.timeout{number} A number of milliseconds the hook will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity.This function is used to create a hook running before each subtest of the current test.
context.after([fn][, options])fn{Function|AsyncFunction} The hook function. The first argument to this function is aTestContextobject. If the hook uses callbacks, the callback function is passed as the second argument. Default: A no-op function.options{Object} Configuration options for the hook. The following properties are supported:signal{AbortSignal} Allows aborting an in-progress hook.timeout{number} A number of milliseconds the hook will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity.This function is used to create a hook that runs after the current test finishes.
context.afterEach([, fn][, options])fn{Function|AsyncFunction} The hook function. The first argument to this function is aTestContextobject. If the hook uses callbacks, the callback function is passed as the second argument. Default: A no-op function.options{Object} Configuration options for the hook. The following properties are supported:signal{AbortSignal} Allows aborting an in-progress hook.timeout{number} A number of milliseconds the hook will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity.This function is used to create a hook running after each subtest of the current test.
context.diagnostic(message)message{string}Message to be reported.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.
context.nameThe name of the test.
context.runOnly(shouldRunOnlyTests)shouldRunOnlyTests{boolean} Whether or not to runonlytests.If
shouldRunOnlyTestsis truthy, the test context will only run tests that have theonlyoption 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.context.signalAbortSignalCan be used to abort test subtasks when the test has been aborted.context.skip([message])message{string} Optional skip message.This function causes the test’s output to indicate the test as skipped. If
messageis provided, it is included in the output. Callingskip()does not terminate execution of the test function. This function does not return a value.context.todo([message])message{string} OptionalTODOmessage.This function adds a
TODOdirective to the test’s output. Ifmessageis provided, it is included in the output. Callingtodo()does not terminate execution of the test function. This function does not return a value.context.test([name][, options][, fn])name{string} The name of the subtest, which is displayed when reporting test results. Default: Thenameproperty offn, or'<anonymous>'iffndoes not have a name.options{Object} Configuration options for the subtest. The following properties are supported:concurrency{number|boolean|null} If a number is provided, then that many tests would run in parallel. Iftrue, it would run all subtests in parallel. Iffalse, it would only run one test at a time. If unspecified, subtests inherit this value from their parent. Default:null.only{boolean} If truthy, and the test context is configured to runonlytests, then this test will be run. Otherwise, the test is skipped. Default:false.skip{boolean|string} If truthy, the test is skipped. If a string is provided, that string is displayed in the test results as the reason for skipping the test. Default:false.signal{AbortSignal} Allows aborting an in-progress test.todo{boolean|string} If truthy, the test marked asTODO. If a string is provided, that string is displayed in the test results as the reason why the test isTODO. Default:false.timeout{number} A number of milliseconds the test will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity.fn{Function|AsyncFunction} The function under test. The first argument to this function is aTestContextobject. If the test uses callbacks, the callback function is passed as the second argument. Default: A no-op function.undefinedonce the test completes.This function is used to create subtests under the current test. This function behaves in the same fashion as the top level
test()function.Class:
SuiteContextAn instance of
SuiteContextis passed to each suite function in order to interact with the test runner. However, theSuiteContextconstructor is not exposed as part of the API.context.nameThe name of the suite.
context.signalAbortSignalCan be used to abort test subtasks when the test has been aborted.License
MIT