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

Jest

A collection of 19 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
#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
#jest #testing #javascript #node

Jest set, clear and reset mock/spy/stub implementation

Between test runs we need mocked/spied on imports and functions to be reset so that assertions don’t fail due to stale calls (from a previous test). This is a way to mitigate what little statefulness is in the system. In unit tests of complex systems, it’s not always ...

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

Jest .fn() and .spyOn() spy/stub/mock assertion reference

Where other JavaScript testing libraries would lean on a specific stub/spy library like Sinon - Standalone test spies, stubs and mocks for JavaScript. Works with any unit testing framework., Jest comes with stubs, mocks and spies out of the box. This post looks at how to instanti ...

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

Jest Array/Object partial match with objectContaining and arrayContaining

It’s possible to do partial matches on Arrays and Objects in Jest using expect.objectContaining and expect.arrayContaining. expect has some powerful matcher methods to do things like the above partial matches. Using Jest at an advanced level means using tools like these to ...

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

Jest explicitly or arbitrarily force fail() a test

When testing code with Jest, it can sometimes be useful to fail a test arbitrarily. This post goes through a few scenarios where that might be useful and how to fail a Jest test explicitly/in a forced manner. It also presents more idiomatic Jest patterns that could be used interc ...

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

Jest Full and Partial Mock/Spy of CommonJS and ES6 Module Imports

JavaScript import/require module testing do’s and don’ts with Jest The example repository is available at github.com/HugoDF/mock-spy-module-import. This post goes through how to achieve different types of module mocking scenarios with Jest. From simple Import interce ...

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

How to run, ignore or skip Jest tests, suites and files

When debugging or writing units tests with Jest it can be useful to specify a single file or test to run or to exclude a specific failing test for the Jest run. This post goes how to skip and exclude a single test, a whole Jest test suite and a whole Jest test file using the CLI ...

  • Hugo Di Francesco
    Author
9 min read
Page 1 of 2  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