本页还没有译文
本页 8 段还没译成日本語,下面显示的是英文原文。assert
还没翻译译自英文(developers.cloudflare.com)本页 8 段都还没译
Note
For compatibility dates of 2026-08-04 or later, Workers enables both nodejs_compat and nodejs_compat_v2 by default. These flags are not used for these compatibility dates. Existing projects do not need to remove them when updating their compatibility date. For earlier dates, add nodejs_compat to your Wrangler configuration file to opt in. For instructions to turn off Node.js compatibility, refer to the Node.js compatibility flag.
The node:assert module in Node.js provides a number of useful assertions that are useful when building tests.
import { strictEqual, deepStrictEqual, ok, doesNotReject } from "node:assert";
strictEqual(1, 1); // ok!
strictEqual(1, "1"); // fails! throws AssertionError
deepStrictEqual({ a: { b: 1 } }, { a: { b: 1 } }); // ok!
deepStrictEqual({ a: { b: 1 } }, { a: { b: 2 } }); // fails! throws AssertionError
ok(true); // ok!
ok(false); // fails! throws AssertionError
await doesNotReject(async () => {}); // ok!
await doesNotReject(async () => {
throw new Error("boom");
}); // fails! throws AssertionErrorNote
In the Workers implementation of assert, all assertions run in, what Node.js calls, the strict assertion mode. In strict assertion mode, non-strict methods behave like their corresponding strict methods. For example, deepEqual() will behave like deepStrictEqual().
Refer to the Node.js documentation for assert for more information.