Server-side testing with Jest, without jsdom
You can use Jest to test both server-side components and browser components (which need JSDOM). In both cases, the default environment is jsdom
, which means it’s used even for your server-side tests. This can cause problems, and apparently makes non-DOM tests run slower.
Instead, switch environment in your server-side tests (e.g. I am testing an Express middleware component) by putting the following comment in the header:
/**
* @jest-environment node
*/
I stumbled upon this because I wanted to deliberately create a Network Error for my test. Even though I was catching it correctly, jsdom
’s xhrutils.js
was handling the HTTP request and logging the error to the console
independently, which left a noisy stack trace in the test output for a successful test run.
Changing the environment to node
silenced the logging and meant the actual error was getting through to my exception handling code.