Back to Gatsby

Add Jest

deprecated-packages/gatsby-recipes/recipes/jest.mdx

2.20.01001 B
Original Source

Add Jest

This recipe helps you setup Jest in your Gatsby site to test components and utilities.

<Config name="gatsbyjs/add-jest" />

This recipe:


Installs the jest package.

<NPMPackage name="jest" />

Adds some jest test files for you to play with.

<File path="src/jest-example/sum.js" content="https://gist.github.com/KyleAMathews/3267b3e09b23dd693aa996b23dfd2d6f/raw/f7c8fa25c295421d8826037b40ac924a33aebf15/sum.js" />

<File path="src/jest-example/sum.test.js" content="https://gist.github.com/KyleAMathews/3267b3e09b23dd693aa996b23dfd2d6f/raw/f7c8fa25c295421d8826037b40ac924a33aebf15/sum.test.js" />


Adds a test & test:watch scripts to your package.json.

<NPMScript name="test" command="jest" />

<NPMScript name="test:watch" command="jest --watch" />


Once you've installed this recipe, try running npm run test and jest will run your test. And, while writing tests, you can run npm run test:watch and tests will re-run as you edit them.