0
# Jest Types
1
2
Jest Types provides comprehensive TypeScript type definitions for the Jest testing framework ecosystem. It contains shared types used across Jest's internal packages and offers public interfaces for Jest configuration, test results, transforms, circus (Jest's test runner), and global utilities.
3
4
## Package Information
5
6
- **Package Name**: @jest/types
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install @jest/types`
10
11
## Core Imports
12
13
```typescript
14
import type { Circus, Config, Global, TestResult, TransformTypes } from "@jest/types";
15
```
16
17
For specific namespace imports:
18
19
```typescript
20
import type { Config } from "@jest/types";
21
import type { Global } from "@jest/types";
22
```
23
24
## Basic Usage
25
26
```typescript
27
import type { Config, Global } from "@jest/types";
28
29
// Define Jest configuration with proper typing
30
const config: Config.InitialOptions = {
31
testEnvironment: "node",
32
collectCoverage: true,
33
coverageDirectory: "coverage",
34
testMatch: ["**/__tests__/**/*.ts"],
35
};
36
37
// Type test functions
38
const testFn: Global.TestFn = async () => {
39
// Test implementation
40
};
41
42
// Type hooks
43
const hookFn: Global.HookFn = async () => {
44
// Setup or teardown logic
45
};
46
```
47
48
## Architecture
49
50
Jest Types is organized into five main namespace modules:
51
52
- **Global Module**: Types for Jest's global test framework functions, hooks, and interfaces
53
- **Config Module**: Comprehensive configuration types for Jest setup and options
54
- **Circus Module**: Types for Jest's Circus test runner including events and state management
55
- **TestResult Module**: Types for test execution results and assertions
56
- **Transform Module**: Types for Jest's code transformation system
57
58
Each module provides complete type coverage for its respective domain, enabling type-safe Jest usage and plugin development.
59
60
## Capabilities
61
62
### Global Test Framework Types
63
64
Core types for Jest's global test functions, hooks, and test organization. Essential for typing test files and custom test utilities.
65
66
```typescript { .api }
67
interface TestFrameworkGlobals {
68
it: ItConcurrent;
69
test: ItConcurrent;
70
fit: ItBase & { concurrent?: ItConcurrentBase };
71
xit: ItBase;
72
xtest: ItBase;
73
describe: Describe;
74
xdescribe: DescribeBase;
75
fdescribe: DescribeBase;
76
beforeAll: HookBase;
77
beforeEach: HookBase;
78
afterEach: HookBase;
79
afterAll: HookBase;
80
}
81
82
type TestFn = PromiseReturningTestFn | GeneratorReturningTestFn | DoneTakingTestFn;
83
type HookFn = TestFn;
84
type DoneFn = (reason?: string | Error) => void;
85
```
86
87
[Global Types](./global.md)
88
89
### Jest Configuration Types
90
91
Complete configuration types for Jest setup including fake timers, coverage, module resolution, and test execution options.
92
93
```typescript { .api }
94
interface InitialOptions {
95
// Core configuration
96
testEnvironment?: string;
97
testMatch?: Array<string>;
98
testRegex?: string | Array<string>;
99
collectCoverage?: boolean;
100
coverageDirectory?: string;
101
102
// Fake timers configuration
103
fakeTimers?: FakeTimers;
104
105
// Module configuration
106
moduleNameMapper?: Record<string, string | Array<string>>;
107
moduleFileExtensions?: Array<string>;
108
109
// Transform configuration
110
transform?: Record<string, string | [string, Record<string, unknown>]>;
111
}
112
113
interface GlobalConfig {
114
bail: number;
115
collectCoverage: boolean;
116
coverageDirectory: string;
117
maxWorkers: number;
118
testPathPatterns: TestPathPatterns;
119
// ... extensive configuration options
120
}
121
```
122
123
[Configuration Types](./config.md)
124
125
### Circus Test Runner Types
126
127
Types for Jest's Circus test runner including event handling, test execution state, and test organization structures.
128
129
```typescript { .api }
130
interface State {
131
currentDescribeBlock: DescribeBlock;
132
currentlyRunningTest?: TestEntry | null;
133
hasFocusedTests: boolean;
134
rootDescribeBlock: DescribeBlock;
135
testTimeout: number;
136
unhandledErrors: Array<Exception>;
137
}
138
139
interface TestEntry {
140
type: 'test';
141
fn: TestFn;
142
name: TestName;
143
parent: DescribeBlock;
144
mode: TestMode;
145
concurrent: boolean;
146
timeout?: number;
147
status?: TestStatus | null;
148
}
149
150
type Event = SyncEvent | AsyncEvent;
151
```
152
153
[Circus Types](./circus.md)
154
155
### Test Result Types
156
157
Types for test execution results, assertions, and error handling to support test reporting and analysis.
158
159
```typescript { .api }
160
interface AssertionResult {
161
ancestorTitles: Array<string>;
162
duration?: number | null;
163
failing?: boolean;
164
failureDetails: Array<unknown>;
165
failureMessages: Array<string>;
166
fullName: string;
167
numPassingAsserts: number;
168
status: Status;
169
title: string;
170
}
171
172
interface SerializableError {
173
code?: unknown;
174
message: string;
175
stack: string | null | undefined;
176
type?: string;
177
}
178
```
179
180
[Test Result Types](./test-result.md)
181
182
### Transform Types
183
184
Types for Jest's code transformation system enabling custom transformers and build tool integration.
185
186
```typescript { .api }
187
interface TransformResult {
188
code: string;
189
originalCode: string;
190
sourceMapPath: string | null;
191
}
192
```
193
194
[Transform Types](./transform.md)