Jest
Requirements
- Node >=16
- Jest 28 || 29
Installing the Plugin
Install the plugin using your favorite package manager.
npm install @testradar/jest --save-dev
Configuring the Plugin
You can view the source code in our working example repository.
To configure the plugin, you need to create 3 modules and add them to your Jest configuration file.
Global Setup Module
This module, which you can name globalSetup.js
or whatever else you like, configures the plugin and will be used later as the value for the globalSetup
option in your Jest configuration file. Pass Jest's globalConfig
and projectConfig
as an object and an options object to the setupTestRadar
function exported from @testradar/jest
.
// globalSetup.js
const { setupTestRadar } = require('@testradar/jest');
module.exports = async function (globalConfig, projectConfig) {
await setupTestRadar(
{ globalConfig, projectConfig },
{
// Replace the example values.
// See the plugin options documented below.
repositoryId: 123456789,
jestConfig: `${globalConfig.rootDir}/jest.config.js`,
}
);
};
Plugin Options:
Option | Description | Type | Required |
---|---|---|---|
repositoryId | Your repository's id, as shown on the Repositories page in TestRadar. | number | Yes |
jestConfig | The path to your Jest configuration file. | string | Yes |
Test Environment Module
This module, which you can name testEnvironment.js
or whatever else you like, extends Jest's test environment and will be used later as the value for the testEnvironment
option in your Jest configuration file. TestRadar supports Jest's Node.js and jsdom environments.
Node.js
Use TestRadarNodeEnvironment
if your current testEnvironment
is node
(Jest's default).
// testEnvironment.js
const { TestRadarNodeEnvironment } = require('@testradar/jest');
module.exports = TestRadarNodeEnvironment;
jsdom
Use TestRadarJSDOMEnvironment
if your current testEnvironment
is jsdom
.
// testEnvironment.js
const { TestRadarJSDOMEnvironment } = require('@testradar/jest');
module.exports = TestRadarJSDOMEnvironment;
Setup Tests Module
This module, which you can name setupTests.js
or whatever else you like, enables test retries and will be used later as the value for the setupFilesAfterEnv
option in your Jest configuration file.
// setupTests.js
jest.retryTimes(2, { logErrorsBeforeRetry: true });
Test retries are required. Flaky test detection won't work without it.
Jest Configuration
After creating the 3 modules above, add them to your Jest configuration file.
module.exports = {
globalSetup: '<rootDir>/globalSetup.js',
testEnvironment: '<rootDir>/testEnvironment.js',
setupFilesAfterEnv: ['<rootDir>/setupTests.js'],
// The rest of your configuration
};
You're almost done, you just need to set your API key to communicate with TestRadar.
Setting Your API Key
TestRadar requires your API key be set on process.env
as TESTRADAR_API_KEY
. We recommend you store this as a secret with your CI provider and set it inside your CI configuration file. For more information, see "Continuous Integration".
Your API key should be treated as a secret. Don't share it with others or store it in plain text.