Node.js v18.12.1 documentation


Table of contents

Test runner#

Stability: 1 - Experimental实验性的

Source Code: lib/test.js

The node:test module facilitates the creation of JavaScript tests that report results in TAP format. node:test模块便于创建以TAP格式报告结果的JavaScript测试。To access it:要访问它:

import test from 'node:test';const test = require('node:test');

This module is only available under the node: scheme. 此模块仅在node:架构下可用。The following will not work:以下操作将不起作用:

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:通过test模块创建的测试由一个函数组成,该函数以三种方式之一进行处理:

  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 resolves.返回Promise的函数,如果Promise拒绝,则该函数被视为失败;如果Promise解决,则该功能被视为通过。
  3. A function that receives a callback function. 接收回调函数的函数。If the callback receives any truthy value as its first argument, the test is considered failing. 如果回调接收到任何truthy值作为其第一个参数,则认为测试失败。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.如果测试函数接收到一个回调函数并返回一个Promise,那么测试将失败。

The following example illustrates how tests are written using the test module.以下示例说明了如何使用test模块编写测试。

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'));
});
});

As a test file executes, TAP is written to the standard output of the Node.js process. 当测试文件执行时,TAP被写入Node.js进程的标准输出。This output can be interpreted by any test harness that understands the TAP format. 该输出可以由任何理解TAP格式的测试线束进行解释。If any tests fail, the process exit code is set to 1.如果任何测试失败,进程退出代码将设置为1