Skip to content
Cloudflare Docs

test

To enable built-in Node.js APIs and polyfills, add the nodejs_compat compatibility flag to your Wrangler configuration file. This also enables nodejs_compat_v2 as long as your compatibility date is 2024-09-23 or later. Learn more about the Node.js compatibility flag and v2.

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)); // Recoreds 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 โ†—.