- assert断言
- async_hooks异步钩子
- async_hooks/context异步上下文
- buffer缓冲区
- C++插件
- C/C++插件(使用Node-API)
- C++嵌入器
- child_process子进程
- cluster集群
- CLI命令行
- console控制台
- Corepack核心包
- crypto加密
- crypto/webcrypto网络加密
- debugger调试器
- deprecation弃用
- dgram数据报
- diagnostics_channel诊断通道
- dns域名服务器
- domain域
- Error错误
- events事件触发器
- fs文件系统
- global全局变量
- http超文本传输协议
- http2超文本传输协议2.0
- https安全超文本传输协议
- inspector检查器
- Intl国际化
- module模块
- module/cjsCommonJS模块
- module/esmECMAScript模块
- module/package包模块
- net网络
- os操作系统
- path路径
- perf_hooks性能钩子
- policy安全策略
- process进程
- punycode域名代码
- querystring查询字符串
- readline逐行读取
- repl交互式解释器
- report诊断报告
- stream流
- stream/web网络流
- string_decoder字符串解码器
- test测试
- timers定时器
- tls安全传输层
- trace_events跟踪事件
- tty终端
- url网址
- util实用工具
- v8引擎
- vm虚拟机
- wasi网络汇编系统接口
- worker_threads工作线程
- zlib压缩
Node.js v18.7.0 文档
- Node.js 18.7.0
-
►
目录
- test 测试
- 子测试
- 跳过测试
- describe/it 语法
- 无关的异步活动
- 从命令行运行测试
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])
TestContext
类SuiteContext
类
- test 测试
-
►
索引
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS模块
- module/esm ECMAScript模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- policy 安全策略
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- 文档搜索
- 会员登录
目录
- test 测试
- 子测试
- 跳过测试
- describe/it 语法
- 无关的异步活动
- 从命令行运行测试
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])
TestContext
类SuiteContext
类
test 测试#
node:test
模块有助于创建以 TAP 格式报告结果的 JavaScript 测试。
要访问它:
子测试#
测试上下文的 test()
方法允许创建子测试。
此方法的行为与顶层 test()
函数相同。
以下示例演示了如何创建具有两个子测试的顶层测试。
跳过测试#
通过将 skip
选项传给测试,或调用测试上下文的 skip()
方法,可以跳过单个测试。
这两个选项都支持包括在 TAP 输出中显示的消息,如下例所示。
describe/it 语法#
运行测试也可以使用 describe
来声明套件和 it
来声明测试。
套件用于将相关测试组织和分组在一起。
it
是 test
的别名,除了没有通过测试上下文,因为嵌套是使用套件完成的,如本例所示
only
测试#
如果 Node.js 使用 --test-only
命令行选项启动,则可以通过将 only
选项传给应该运行的测试来跳过除选定子集之外的所有顶层测试。
当运行带有 only
选项集的测试时,所有子测试也会运行。
测试上下文的 runOnly()
方法可用于在子测试级别实现相同的行为。
无关的异步活动#
一旦测试函数完成执行,则 TAP 结果会尽快输出,同时保持测试的顺序。 但是,测试函数可能会生成比测试本身寿命更长的异步活动。 测试运行器处理此类活动,但不会延迟报告测试结果以适应它。
从命令行运行测试#
可以通过传入 --test
标志从命令行调用 Node.js 测试运行程序:
测试运行器执行模型#
当搜索要执行的测试文件时,测试运行器的行为如下:
test([name][, options][, fn])
#
test()
函数是从 test
模块导入的值。
每次调用此函数都会在 TAP 输出中创建一个测试点。
describe([name][, options][, fn])
#
从 node:test
模块导入的 describe()
函数。
每次调用此函数都会在 TAP 输出中创建一个子测试和一个测试点。
调用顶层 describe
函数之后,所有顶层测试和套件都将执行
describe.skip([name][, options][, fn])
#
跳过套件的简写,与 describe([name], { skip: true }[, fn])
相同。
describe.todo([name][, options][, fn])
#
将套件标记为 TODO
的简写,与 describe([name], { todo: true }[, fn])
相同。
it([name][, options][, fn])
#
it()
函数是从 node:test
模块导入的值。
每次调用此函数都会在 TAP 输出中创建一个测试点。
it.skip([name][, options][, fn])
#
跳过测试的简写,与 it([name], { skip: true }[, fn])
相同。
it.todo([name][, options][, fn])
#
将测试标记为 TODO
的简写,与 it([name], { todo: true }[, fn])
相同。
TestContext
类#
TestContext
的实例被传给每个测试函数,以便与测试运行器交互。
但是,TestContext
构造函数没有作为 API 的一部分公开。
context.diagnostic(message)
#
此函数用于将 TAP 诊断写入输出。 任何诊断信息都包含在测试结果的末尾。 此函数不返回值。
context.runOnly(shouldRunOnlyTests)
#
如果 shouldRunOnlyTests
为真,则测试上下文将只运行设置了 only
选项的测试。
否则,将运行所有测试。
如果 Node.js 不是使用 --test-only
命令行选项启动的,则此函数是无操作的。
context.signal
#
context.skip([message])
#
此函数使测试的输出指示测试已跳过。
如果提供了 message
,则它将包含在 TAP 输出中。
调用 skip()
不会终止测试函数的执行。
此函数不返回值。
context.todo([message])
#
此函数将 TODO
指令添加到测试的输出中。
如果提供了 message
,则它将包含在 TAP 输出中。
调用 todo()
不会终止测试函数的执行。
此函数不返回值。
context.test([name][, options][, fn])
#
此函数用于在当前测试下创建子测试。
此函数的行为方式与顶层的 test()
函数相同。
SuiteContext
类#
SuiteContext
的实例被传给每个套件函数,以便与测试运行器进行交互。
但是,SuiteContext
构造函数没有作为 API 的一部分公开。