- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:module
API - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
Node.js v18.12.1 documentation
- Node.js v18.12.1
-
► Table of contents
- Test runner
- Subtests
- Skipping tests
describe
/it
syntax- Filtering tests by name
- Extraneous asynchronous activity
- Running tests from the command line
run([options])
test([name][, options][, fn])
describe([name][, options][, fn])
describe.skip([name][, options][, fn])
describe.todo([name][, options][, fn])
it([name][, options][, fn])
it.skip([name][, options][, fn])
it.todo([name][, options][, fn])
before([fn][, options])
after([fn][, options])
beforeEach([fn][, options])
afterEach([fn][, options])
- Class:
TapStream
- Class:
TestContext
- Class:
SuiteContext
- Test runner
-
► Index
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:module
API - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- ► Other versions
- ► Options
Table of contents
- Test runner
- Subtests
- Skipping tests
describe
/it
syntax- Filtering tests by name
- Extraneous asynchronous activity
- Running tests from the command line
run([options])
test([name][, options][, fn])
describe([name][, options][, fn])
describe.skip([name][, options][, fn])
describe.todo([name][, options][, fn])
it([name][, options][, fn])
it.skip([name][, options][, fn])
it.todo([name][, options][, fn])
before([fn][, options])
after([fn][, options])
beforeEach([fn][, options])
afterEach([fn][, options])
- Class:
TapStream
- Class:
TestContext
- Class:
SuiteContext
Test runner#
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
模块创建的测试由一个函数组成,该函数以三种方式之一进行处理:
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 thePromise
rejects, and is considered passing if thePromise
resolves.Promise
的函数,如果Promise
拒绝,则该函数被视为失败;如果Promise
解决,则该功能被视为通过。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
。