#javascript #node #node:test #testingClear Module Cache/Force Import in Node.js with CommonJS and with ES Modules (ESM)Node.js supports two primary module systems: CommonJS and ES Modules (ESM). Both systems cache imported modules, ensuring that a module’s code runs only once, even if it’s imported multiple times. When writing tests it can be useful to bypass caching for example if th ...
#node:test #testing #node #javascriptMocking/stubbing the Date and timers (setTimeout) in Node.js tests with built-in `node:test` MockTimersMockTimers is an experimental class in the node:test module which concerns itself with allowing mocking of timers (setTimeout, setInterval, setImmediate) and the Date built-ins. ...
#node:test #testing #nodeTest Native `fetch` in Node.js with Undici interception and mock utilsNode.js 18+ has a built-in fetch available, where prior versions had to use libraries such a node-fetch, axios, got or other to get such functionality. The “native fetch” was implemented in userland first as the undici package. This post goes through how to use undici ...
#node:test #node #testing #javascriptNode Test Runner: skip a test if an environment variable is missing/emptyThe node:test module is a built-in test runner and orchestrator in Node.js. It’s available as experimental since Node 18 but has stabilised in Node 20 and some features are still experimental at the time of writing, in Node 21. node:test supports test skipping throught mult ...
#node:test #node #testing #javascriptNode.js Native Test RunnerIf anyone missed it, Node.js 18 includes a test runner/test definition module (node --test and node:test respectively). node:test exports a test function and you can run the Node CLI with a --test flag which does some basic search/matching for test files. Full Documentation: node ...