Testing Setup

Learn about the different methods of setting up our Jest tests.

What does test setup entail?

Test setup is a series of configurations and hooks that we defined, which create the environment we want to run our tests inside. Most software requires some added context to run. For instance, a server might require a seeded database, and a client might require user authentication. Both a server and a client may need additional packages installed in order to be testable with Jest. Test setup is where we get to do all of this.

Global setup

We can configure a global Jest setup through the Jest config file. In our Jest config, we have access to a globalSetup field, which points to a string—the path to the file holding our global setup configuration. This file exports a function that accepts two arguments, globalConfig and projectConfig. Additionally, we can access Node’s globalThis variable here. This function executes only once before all tests run. Global setup is a good place to make any global variables available to our test environment. For instance, some third-party packages are accessed through an imported script and made available via the global variable. Global setup is where we can mimic this for our tests.

Get hands-on with 1200+ tech skills courses.