A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.gatsbyjs.com/docs/how-to/testing/unit-testing/ below:

Website Navigation


Unit Testing | Gatsby

Unit testing is a great way to protect against errors in your code before you deploy it. While Gatsby does not include support for unit testing out of the box, it only takes a few steps to get up and running. However, there are a few features of the Gatsby build process that mean the standard Jest setup doesn’t quite work. This guide shows you how to set it up.

Setting up your environment

The most popular testing framework for React is Jest, which was created by Facebook. While Jest is a general-purpose JavaScript unit testing framework, it has lots of features that make it work particularly well with React.

1. Installing dependencies

First, you need to install Jest and some more required packages. Install babel-jest and babel-preset-gatsby to ensure that the babel preset(s) that are used match what are used internally for your Gatsby site.

Please note: This guide assumes you’re using Jest 29 or above.

2. Creating a configuration file for Jest

Because Gatsby handles its own Babel configuration, you will need to manually tell Jest to use babel-jest. The easiest way to do this is to add a jest.config.js. You can set up some useful defaults at the same time:

Go over the content of this configuration file:

3. Useful mocks to complete your testing environment Mocking gatsby

Finally, it’s a good idea to mock the gatsby module itself. This may not be needed at first, but will make things a lot easier if you want to test components that use Link, Slice, or GraphQL.

This mocks the graphql() function, Link component, Slice placeholder, and useStaticQuery hook.

Writing tests

This guide explains how you can set up Jest with Gatsby, if you want to know how to write tests we highly recommend checking out the Testing React components guide. It explains how to install @testing-library/react which is our recommended utility for Jest.

Visit the Jest docs to learn more about other tests that you can write.

Running tests

If you look inside package.json you will probably find that there is already a script for test, which just outputs an error message. Change this to use the jest executable that you now have available, like so:

This means you can now run tests by typing npm test. If you want you could also run with a flag that triggers watch mode to watch files and run tests when they are changed: npm test -- --watch.

Using TypeScript

If you are using TypeScript, you need to install typings packages and make two changes to your config.

Update the transform in jest.config.js to run jest-preprocess on files in your project’s root directory.

Note: <rootDir> is replaced by Jest with the root directory of the project. Don’t change it.

Also update jest-preprocess.js with the following Babel preset to look like this:

Once this is changed, you can write your tests in TypeScript using the .ts or .tsx extensions.

Using tsconfig paths

If you are using tsconfig paths there is a single change to your config.

  1. Add ts-jest
  1. Update jest.config.js to import and map tsconfig.json paths
  1. Add paths to jest.config.js moduleNameMapper
Other resources

If you need to make changes to your Babel config, you can edit the config in jest-preprocess.js. You may need to enable some of the plugins used by Gatsby. See the Gatsby Babel config guide for some examples.

For more information on Jest testing, visit the Jest site. Also read our Testing React components guide.

For an example encapsulating all of these techniques and a full unit test suite with @testing-library/react, check out the using-jest example.

Start building today on

Netlify!


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4