Skip to content

Instantly share code, notes, and snippets.

@thebuilder
Last active October 29, 2022 02:42
Show Gist options
  • Save thebuilder/15a084f74b1c6a1f163fc6254ad5a5ba to your computer and use it in GitHub Desktop.
Save thebuilder/15a084f74b1c6a1f163fc6254ad5a5ba to your computer and use it in GitHub Desktop.
Use Jest Projects to run both JSDom and Node tests in the same project
module.exports = {
projects: [
{
displayName: 'dom',
testEnvironment: 'jsdom',
snapshotSerializers: ['enzyme-to-json/serializer'],
testMatch: ['**/__tests__/**/*.test.js?(x)']
},
{
displayName: 'node',
testEnvironment: 'node',
testMatch: [
'**/__tests__/**/*.test.node.js?(x)',
]
},
],
};
@thebuilder
Copy link
Author

thebuilder commented Jun 8, 2018

Use Jest Projects to run tests targeting both JSDom and Node. Perfect if you are are doing SSR and want to validate your code, while also testing code running in the browser.

These matches looks for *.test.js and *test.node.js, but feel free to configure these to match your project. Maybe you want to use Node for all tests under server/?

Just use jest like you normally to run the tests.

@Emyboy
Copy link

Emyboy commented Sep 15, 2021

Thanks for this.

@qnp
Copy link

qnp commented Feb 5, 2022

Thanks a lot

@jviel
Copy link

jviel commented Aug 26, 2022

This got me on the right track, thank you!

In my case I was missing another piece:
I have a unified project with both client/ and server/ code, each with their own package.json. I had to set "type": "module" in the package.json corresponding to the client (with jsdom environment) tests.

Now my project root jest config works for all sub-projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment