• Home
  • Books
  • Node
  • Testing
  • JavaScript
  • More...
  • Search
  • About
  • Gear & Setup

Testing

A collection of 27 posts

#javascript #node #testing #jest

Configure Timezone for Jest/Node.js with the `TZ` env var

In order to test code that involves date and time manipulation and parsing. It can be useful to set the timezone under which the Jest tests run. Thankfully Node.js supports the TZ environment variable: “The TZ environment variable is used to specify the timezone configurati ...

  • Hugo Di Francesco
    Author
2 min read
#nock #jest #testing #node

Nock on Node 18/20/22 Fails to Intercept isomorphic-unfetch/fetch Request

On Node versions 18, 20 and 22, users may encounter an issue where Nock fails to intercept requests made via isomorphic-unfetch. What follows is a sample error you may see (timeout is reached because the request is not intercepted). returns a valid response thrown: "Exceeded ...

  • Hugo Di Francesco
    Author
2 min read
#javascript #jest #node #Testing #date

Mocking/stubbing the current Date in Jest tests

There are situations where new Date() or Date.now is used in application code. That code needs to be tested, and it’s always a struggle to remember how to mock/stub or spy on Date.now/new Date with Jest. This post goes through multiple approaches to mocking, stubbing and spying o ...

  • Hugo Di Francesco
    Author
4 min read
#node:test #testing #node #javascript

Mocking/stubbing the Date and timers (setTimeout) in Node.js tests with built-in `node:test` MockTimers

MockTimers 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. ...

  • Hugo Di Francesco
    Author
4 min read
#node:test #testing #node

Test Native `fetch` in Node.js with Undici interception and mock utils

Node.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 ...

  • Hugo Di Francesco
    Author
5 min read
#node:test #node #testing #javascript

Node Test Runner: skip a test if an environment variable is missing/empty

The 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 ...

  • Hugo Di Francesco
    Author
5 min read
#jest #testing #javascript #node

What is Jest and why should I use it?

Jest is a delightful JavaScript Testing Framework with a focus on simplicity. jestjs.io Jest is a batteries-included framework. Due to its fully-featured nature and large surface area, we can compare it to test runners (Mocha), assertion libraries (Chai, ShouldJS, power-assert, ...

  • Hugo Di Francesco
    Author
7 min read
#node:test #node #testing #javascript

Node.js Native Test Runner

If 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 ...

  • Hugo Di Francesco
    Author
4 min read
#cypress #testing #javascript

Cypress Scroll Position Assertions

This post goes through approaches to asserting on the scroll position. First we’ll see how to assert that we’re at the top of the page. Next we’ll look at 2 approaches to assert that we’ve scrolled to an element. The examples for this post are available at ...

  • Hugo Di Francesco
    Author
4 min read
#ava #Testing #node #javascript

AVA: pass or fail a test if an environment variable is missing/empty

In a recent project I needed to fail/pass some AVA if a runtime environment variable was unset or empty. Here’s how I solved this issue. AVA is a test runner for Node.js with a concise API, detailed error output, embrace of new language features and process isolation that l ...

  • Hugo Di Francesco
    Author
3 min read
Page 1 of 3  Previous Posts →

Interested in Alpine.js?

Power up your debugging with the Alpine.js Devtools Extension for Chrome and Firefox. Trusted by over 15,000 developers (rated 4.5 ⭐️).

Install Now close
Code with Hugo
Latest Posts Github