Skip to main content

test

还没翻译译自英文(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.

MockTracker

The MockTracker API in Node.js provides a means of tracking and managing mock objects in a test environment.

import { mock } from 'node:test';

const fn = mock.fn();
fn(1,2,3);  // does nothing... but

console.log(fn.mock.callCount());  // Records how many times it was called
console.log(fn.mock.calls[0].arguments);  // Records the arguments that were passed each call

The full MockTracker API is documented in the Node.js documentation for MockTracker.

The Workers implementation of MockTracker currently does not include an implementation of the Node.js mock timers API.