Jest
Set up Jest for JavaScript testing and integrate test results with TestKase.
Overview
Jest is the most popular JavaScript testing framework, maintained by Meta. It provides a batteries-included experience for unit and integration testing of JavaScript and TypeScript applications — zero-config for most projects, built-in mocking, code coverage, and snapshot testing.
To integrate Jest results with TestKase, generate JUnit XML output using the jest-junit package and report
with --format junit.
Prerequisites
- Node.js 18+ and npm (or yarn/pnpm)
- A JavaScript or TypeScript project
Installation
Install Jest as a dev dependency:
npm install --save-dev jestFor TypeScript projects, also install ts-jest and type definitions:
npm install --save-dev jest ts-jest @types/jestProject Setup
Create a jest.config.js in your project root:
// jest.config.js
module.exports = {
testEnvironment: 'node',
testMatch: ['**/__tests__/**/*.test.(js|ts)', '**/*.test.(js|ts)'],
// For TypeScript projects, add the ts-jest transform:
// transform: { '^.+\\.tsx?$': 'ts-jest' },
};Add a test script to your package.json:
{
"scripts": {
"test": "jest"
}
}Writing Tests
Create a test file (e.g., __tests__/login.test.js):
// __tests__/login.test.js
const { login } = require('../src/auth');
describe('Login Page', () => {
test('[48271] should login with valid credentials', () => {
const result = login('user@example.com', 'password123');
expect(result.success).toBe(true);
expect(result.token).toBeDefined();
});
test('[48272] should reject invalid password', () => {
const result = login('user@example.com', 'wrong');
expect(result.success).toBe(false);
expect(result.error).toBe('Invalid credentials');
});
test('[48273] should reject empty email', () => {
expect(() => login('', 'password123')).toThrow('Email is required');
});
});Each test name includes a 5-digit Automation ID in square brackets. The @testkase/reporter CLI
extracts these IDs using the regex \[(\d{5})\]. For the example above, the extracted Automation IDs are:
48271→ linked to the "valid login" test case in TestKase48272→ linked to the "invalid password" test case in TestKase48273→ linked to the "empty email" test case in TestKase
Generate Automation IDs in TestKase first, then embed them in your test names. The [XXXXX] pattern
can appear anywhere in the test name — the reporter extracts all 5-digit IDs found in brackets.
Running Tests
Run all tests:
npx jestGenerating JUnit XML Output
Install the jest-junit reporter:
npm install --save-dev jest-junitRun Jest with the JUnit reporter:
npx jest --reporters=default --reporters=jest-junitBy default, jest-junit writes to ./junit.xml. Control the output location with environment variables:
JEST_JUNIT_OUTPUT_DIR=test-results JEST_JUNIT_OUTPUT_NAME=junit.xml npx jest --reporters=default --reporters=jest-junitThis writes the results to test-results/junit.xml.
You can also configure jest-junit in your jest.config.js or package.json instead of using environment
variables. See the jest-junit documentation for all options.
TestKase Integration
After generating the JUnit XML file, report results to TestKase:
npx @testkase/reporter report \
--token $TESTKASE_PAT \
--project-id PRJ-1 \
--org-id 1173 \
--cycle-id TCYCLE-5 \
--format junit \
--results-file test-results/junit.xml--cycle-id is optional. If not provided, results are reported to TCYCLE-1 — the master test cycle for the project.
Automation ID Mapping
The reporter extracts 5-digit Automation IDs from Jest test names using the [XXXXX] bracket pattern:
| Test Code | Extracted ID |
|---|---|
test('[48271] should login with valid credentials', ...) | 48271 |
test('[48272] should reject invalid password', ...) | 48272 |
describe('API', () => { test('[48273] returns 401', ...) }) | 48273 |
The [XXXXX] pattern can appear anywhere in the test() name. The describe block structure does
not affect the Automation ID — only the 5-digit number inside brackets matters.
Complete Example
1. Test File
// __tests__/login.test.js
const { login } = require('../src/auth');
describe('Login Page', () => {
test('[48271] should login with valid credentials', () => {
const result = login('user@example.com', 'password123');
expect(result.success).toBe(true);
});
test('[48272] should reject invalid password', () => {
const result = login('user@example.com', 'wrong');
expect(result.success).toBe(false);
});
});2. Run Tests and Generate JUnit XML
JEST_JUNIT_OUTPUT_DIR=test-results JEST_JUNIT_OUTPUT_NAME=junit.xml \
npx jest --reporters=default --reporters=jest-junit3. Report Results to TestKase
npx @testkase/reporter report \
--token $TESTKASE_PAT \
--project-id PRJ-1 \
--org-id 1173 \
--cycle-id TCYCLE-5 \
--format junit \
--results-file test-results/junit.xmlTroubleshooting
jest-junit not generating the XML file
Ensure jest-junit is listed in the --reporters flag. If you are using jest.config.js, verify the
reporters array includes jest-junit:
// jest.config.js
module.exports = {
reporters: ['default', 'jest-junit'],
};Also check that the output directory exists or that jest-junit has write permissions.
Automation IDs not being extracted
Use --dry-run to preview which [XXXXX] Automation IDs the reporter extracts from your test names:
npx @testkase/reporter report \
--format junit \
--results-file test-results/junit.xml \
--dry-run --verboseCompare the output Automation IDs with the values set on your TestKase test cases.
TypeScript tests not found
If Jest cannot find or compile .ts / .tsx test files, ensure ts-jest is installed and the
transform is configured in jest.config.js:
// jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};Alternatively, set the transform explicitly:
module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
};