Skip to main content

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:

  1. A synchronous function that is considered failing if it throws an exception, and is considered passing otherwise.
  2. A function that returns a Promise that is considered failing if the Promise rejects, and is considered passing if the Promise fulfills.
  3. 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 #

c
MockFunctionContext

The MockFunctionContext class is used to inspect or manipulate the behavior of mocks created via the MockTracker APIs.

c
MockModuleContext
No documentation available
c
MockTimers

Mocking 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.

c
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.

c
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.

c
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.

c
TestsStream

A successful call to run() will return a new TestsStream object, streaming a series of events representing the execution of the tests.

Functions #

f
after

This function creates a hook that runs after executing a suite.

    f
    afterEach

    This function creates a hook that runs after each test in the current suite. The afterEach() hook is run even if the test fails.

      f
      assert.register

      Defines a new assertion function with the provided name and function. If an assertion already exists with the same name, it is overwritten.

        f
        before

        This function creates a hook that runs before executing a suite.

          f
          beforeEach

          This function creates a hook that runs before each test in the current suite.

            f
            N
            default

            The test() function is the value imported from the test module. Each invocation of this function results in reporting the test to the TestsStream.

              f
              default.after

              This function creates a hook that runs after executing a suite.

                f
                default.afterEach

                This function creates a hook that runs after each test in the current suite. The afterEach() hook is run even if the test fails.

                  f
                  default.assert.register

                  Defines a new assertion function with the provided name and function. If an assertion already exists with the same name, it is overwritten.

                    f
                    default.before

                    This function creates a hook that runs before executing a suite.

                      f
                      default.beforeEach

                      This function creates a hook that runs before each test in the current suite.

                        f
                        default.describe.only

                        Shorthand for marking a suite as only. This is the same as calling describe with options.only set to true.

                          f
                          default.describe.skip

                          Shorthand for skipping a suite. This is the same as calling describe with options.skip set to true.

                            f
                            default.describe.todo

                            Shorthand for marking a suite as TODO. This is the same as calling describe with options.todo set to true.

                              f
                              N
                              default.it

                              Alias for test.

                                f
                                default.it.only

                                Shorthand for marking a test as only. This is the same as calling it with options.only set to true.

                                  f
                                  default.it.skip

                                  Shorthand for skipping a test. This is the same as calling it with options.skip set to true.

                                    f
                                    default.it.todo

                                    Shorthand for marking a test as TODO. This is the same as calling it with options.todo set to true.

                                      f
                                      default.only

                                      Shorthand for marking a test as only. This is the same as calling test with options.only set to true.

                                        f
                                        default.run

                                        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.

                                          f
                                          default.skip

                                          Shorthand for skipping a test. This is the same as calling test with options.skip set to true.

                                            f
                                            default.snapshot.setDefaultSnapshotSerializers

                                            This function is used to customize the default serialization mechanism used by the test runner.

                                              f
                                              default.snapshot.setResolveSnapshotPath

                                              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.

                                                f
                                                N
                                                default.suite

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

                                                  f
                                                  default.suite.only

                                                  Shorthand for marking a suite as only. This is the same as calling suite with options.only set to true.

                                                    f
                                                    default.suite.skip

                                                    Shorthand for skipping a suite. This is the same as calling suite with options.skip set to true.

                                                      f
                                                      default.suite.todo

                                                      Shorthand for marking a suite as TODO. This is the same as calling suite with options.todo set to true.

                                                        f
                                                        default.todo

                                                        Shorthand for marking a test as TODO. This is the same as calling test with options.todo set to true.

                                                          f
                                                          N
                                                          describe

                                                          Alias for suite.

                                                            f
                                                            describe.only

                                                            Shorthand for marking a suite as only. This is the same as calling describe with options.only set to true.

                                                              f
                                                              describe.skip

                                                              Shorthand for skipping a suite. This is the same as calling describe with options.skip set to true.

                                                                f
                                                                describe.todo

                                                                Shorthand for marking a suite as TODO. This is the same as calling describe with options.todo set to true.

                                                                  f
                                                                  N
                                                                  it

                                                                  Alias for test.

                                                                    f
                                                                    it.only

                                                                    Shorthand for marking a test as only. This is the same as calling it with options.only set to true.

                                                                      f
                                                                      it.skip

                                                                      Shorthand for skipping a test. This is the same as calling it with options.skip set to true.

                                                                        f
                                                                        it.todo

                                                                        Shorthand for marking a test as TODO. This is the same as calling it with options.todo set to true.

                                                                          f
                                                                          only

                                                                          Shorthand for marking a test as only. This is the same as calling test with options.only set to true.

                                                                            f
                                                                            run

                                                                            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.

                                                                              f
                                                                              skip

                                                                              Shorthand for skipping a test. This is the same as calling test with options.skip set to true.

                                                                                f
                                                                                snapshot.setDefaultSnapshotSerializers

                                                                                This function is used to customize the default serialization mechanism used by the test runner.

                                                                                  f
                                                                                  snapshot.setResolveSnapshotPath

                                                                                  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.

                                                                                    f
                                                                                    N
                                                                                    suite

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

                                                                                      f
                                                                                      suite.only

                                                                                      Shorthand for marking a suite as only. This is the same as calling suite with options.only set to true.

                                                                                        f
                                                                                        suite.skip

                                                                                        Shorthand for skipping a suite. This is the same as calling suite with options.skip set to true.

                                                                                          f
                                                                                          suite.todo

                                                                                          Shorthand for marking a suite as TODO. This is the same as calling suite with options.todo set to true.

                                                                                            f
                                                                                            N
                                                                                            test

                                                                                            The test() function is the value imported from the test module. Each invocation of this function results in reporting the test to the TestsStream.

                                                                                              f
                                                                                              test.after

                                                                                              This function creates a hook that runs after executing a suite.

                                                                                                f
                                                                                                test.afterEach

                                                                                                This function creates a hook that runs after each test in the current suite. The afterEach() hook is run even if the test fails.

                                                                                                  f
                                                                                                  test.assert.register

                                                                                                  Defines a new assertion function with the provided name and function. If an assertion already exists with the same name, it is overwritten.

                                                                                                    f
                                                                                                    test.before

                                                                                                    This function creates a hook that runs before executing a suite.

                                                                                                      f
                                                                                                      test.beforeEach

                                                                                                      This function creates a hook that runs before each test in the current suite.

                                                                                                        f
                                                                                                        N
                                                                                                        f
                                                                                                        test.describe.only

                                                                                                        Shorthand for marking a suite as only. This is the same as calling describe with options.only set to true.

                                                                                                          f
                                                                                                          test.describe.skip

                                                                                                          Shorthand for skipping a suite. This is the same as calling describe with options.skip set to true.

                                                                                                            f
                                                                                                            test.describe.todo

                                                                                                            Shorthand for marking a suite as TODO. This is the same as calling describe with options.todo set to true.

                                                                                                              f
                                                                                                              N
                                                                                                              test.it

                                                                                                              Alias for test.

                                                                                                                f
                                                                                                                test.it.only

                                                                                                                Shorthand for marking a test as only. This is the same as calling it with options.only set to true.

                                                                                                                  f
                                                                                                                  test.it.skip

                                                                                                                  Shorthand for skipping a test. This is the same as calling it with options.skip set to true.

                                                                                                                    f
                                                                                                                    test.it.todo

                                                                                                                    Shorthand for marking a test as TODO. This is the same as calling it with options.todo set to true.

                                                                                                                      f
                                                                                                                      test.only

                                                                                                                      Shorthand for marking a test as only. This is the same as calling test with options.only set to true.

                                                                                                                        f
                                                                                                                        test.run

                                                                                                                        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.

                                                                                                                          f
                                                                                                                          test.skip

                                                                                                                          Shorthand for skipping a test. This is the same as calling test with options.skip set to true.

                                                                                                                            f
                                                                                                                            test.snapshot.setDefaultSnapshotSerializers

                                                                                                                            This function is used to customize the default serialization mechanism used by the test runner.

                                                                                                                              f
                                                                                                                              test.snapshot.setResolveSnapshotPath

                                                                                                                              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.

                                                                                                                                f
                                                                                                                                N
                                                                                                                                test.suite

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

                                                                                                                                  f
                                                                                                                                  test.suite.only

                                                                                                                                  Shorthand for marking a suite as only. This is the same as calling suite with options.only set to true.

                                                                                                                                    f
                                                                                                                                    test.suite.skip

                                                                                                                                    Shorthand for skipping a suite. This is the same as calling suite with options.skip set to true.

                                                                                                                                      f
                                                                                                                                      test.suite.todo

                                                                                                                                      Shorthand for marking a suite as TODO. This is the same as calling suite with options.todo set to true.

                                                                                                                                        f
                                                                                                                                        test.todo

                                                                                                                                        Shorthand for marking a test as TODO. This is the same as calling test with options.todo set to true.

                                                                                                                                          f
                                                                                                                                          todo

                                                                                                                                          Shorthand for marking a test as TODO. This is the same as calling test with options.todo set to true.

                                                                                                                                            Interfaces #

                                                                                                                                            Namespaces #

                                                                                                                                            N
                                                                                                                                            assert

                                                                                                                                            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.

                                                                                                                                              N
                                                                                                                                              default.assert

                                                                                                                                              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.

                                                                                                                                                N
                                                                                                                                                default.snapshot
                                                                                                                                                No documentation available
                                                                                                                                                  N
                                                                                                                                                  snapshot
                                                                                                                                                  No documentation available
                                                                                                                                                    N
                                                                                                                                                    test.assert

                                                                                                                                                    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.

                                                                                                                                                      N
                                                                                                                                                      test.snapshot
                                                                                                                                                      No documentation available

                                                                                                                                                        Type Aliases #

                                                                                                                                                        T
                                                                                                                                                        FunctionPropertyNames
                                                                                                                                                        No documentation available
                                                                                                                                                          T
                                                                                                                                                          HookFn

                                                                                                                                                          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.

                                                                                                                                                            T
                                                                                                                                                            Mock
                                                                                                                                                            No documentation available
                                                                                                                                                              T
                                                                                                                                                              NoOpFunction
                                                                                                                                                              No documentation available
                                                                                                                                                                T
                                                                                                                                                                SuiteFn

                                                                                                                                                                The type of a suite test function. The argument to this function is a SuiteContext object.

                                                                                                                                                                  T
                                                                                                                                                                  TestContextHookFn

                                                                                                                                                                  The hook function. The first argument is a TestContext object. If the hook uses callbacks, the callback function is passed as the second argument.

                                                                                                                                                                    T
                                                                                                                                                                    TestFn

                                                                                                                                                                    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.

                                                                                                                                                                      T
                                                                                                                                                                      Timer
                                                                                                                                                                      No documentation available

                                                                                                                                                                        Variables #

                                                                                                                                                                        v
                                                                                                                                                                        default.mock
                                                                                                                                                                        No documentation available
                                                                                                                                                                          v
                                                                                                                                                                          mock
                                                                                                                                                                          No documentation available
                                                                                                                                                                            v
                                                                                                                                                                            test.mock
                                                                                                                                                                            No documentation available

                                                                                                                                                                              Did you find what you needed?

                                                                                                                                                                              Privacy policy