@balazsbarta/mp-skills 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/skills/brainstorming/SKILL.md +201 -0
- package/.agents/skills/brainstorming/references/option-evaluation.md +64 -0
- package/.agents/skills/brainstorming/references/questioning-playbook.md +57 -0
- package/.agents/skills/brainstorming/references/repo-analysis.md +60 -0
- package/.agents/skills/conventional-commits/SKILL.md +124 -0
- package/.agents/skills/conventional-commits/references/commit-types-scopes.md +75 -0
- package/.agents/skills/conventional-commits/references/semantic-release.md +71 -0
- package/.agents/skills/jest/SKILL.md +219 -0
- package/.agents/skills/jest/references/common-errors.md +274 -0
- package/.agents/skills/jest/references/configuration.md +175 -0
- package/.agents/skills/jest/references/embedded-docs.md +44 -0
- package/.agents/skills/jest/references/mocking.md +206 -0
- package/.agents/skills/jest/references/remote-docs.md +19 -0
- package/.agents/skills/jest/references/snapshot-testing.md +181 -0
- package/.agents/skills/jest/references/transforms.md +216 -0
- package/.agents/skills/maestro/SKILL.md +230 -0
- package/.agents/skills/maestro/references/assertions-commands.md +259 -0
- package/.agents/skills/maestro/references/common-errors.md +273 -0
- package/.agents/skills/maestro/references/eas-ci-integration.md +219 -0
- package/.agents/skills/maestro/references/flow-authoring.md +224 -0
- package/.agents/skills/maestro/references/remote-docs.md +23 -0
- package/.agents/skills/mastra/SKILL.md +159 -0
- package/.agents/skills/mastra/references/common-errors.md +535 -0
- package/.agents/skills/mastra/references/create-mastra.md +220 -0
- package/.agents/skills/mastra/references/embedded-docs.md +123 -0
- package/.agents/skills/mastra/references/migration-guide.md +180 -0
- package/.agents/skills/mastra/references/remote-docs.md +193 -0
- package/.agents/skills/next-js/SKILL.md +209 -0
- package/.agents/skills/next-js/references/api-routes.md +213 -0
- package/.agents/skills/next-js/references/app-router.md +206 -0
- package/.agents/skills/next-js/references/caching-revalidation.md +211 -0
- package/.agents/skills/next-js/references/common-errors.md +251 -0
- package/.agents/skills/next-js/references/embedded-docs.md +43 -0
- package/.agents/skills/next-js/references/metadata-seo.md +257 -0
- package/.agents/skills/next-js/references/remote-docs.md +22 -0
- package/.agents/skills/playwright/SKILL.md +218 -0
- package/.agents/skills/playwright/references/ci-configuration.md +208 -0
- package/.agents/skills/playwright/references/common-errors.md +258 -0
- package/.agents/skills/playwright/references/embedded-docs.md +41 -0
- package/.agents/skills/playwright/references/fixtures-assertions.md +208 -0
- package/.agents/skills/playwright/references/page-objects.md +167 -0
- package/.agents/skills/playwright/references/remote-docs.md +23 -0
- package/.agents/skills/playwright/references/visual-regression.md +206 -0
- package/.agents/skills/pull-request-lifecycle/SKILL.md +116 -0
- package/.agents/skills/pull-request-lifecycle/references/changelog-versioning.md +72 -0
- package/.agents/skills/pull-request-lifecycle/references/merge-strategies.md +33 -0
- package/.agents/skills/pull-request-lifecycle/references/pr-description-template.md +72 -0
- package/.agents/skills/pull-request-lifecycle/references/review-process.md +54 -0
- package/.agents/skills/pull-request-lifecycle/scripts/code_review.py +220 -0
- package/.agents/skills/react-native-expo/SKILL.md +212 -0
- package/.agents/skills/react-native-expo/references/common-errors.md +251 -0
- package/.agents/skills/react-native-expo/references/eas-build-submit.md +238 -0
- package/.agents/skills/react-native-expo/references/embedded-docs.md +42 -0
- package/.agents/skills/react-native-expo/references/native-modules.md +181 -0
- package/.agents/skills/react-native-expo/references/navigation-setup.md +229 -0
- package/.agents/skills/react-native-expo/references/remote-docs.md +23 -0
- package/.agents/skills/supabase/SKILL.md +216 -0
- package/.agents/skills/supabase/references/auth-setup.md +206 -0
- package/.agents/skills/supabase/references/common-errors.md +285 -0
- package/.agents/skills/supabase/references/edge-functions.md +178 -0
- package/.agents/skills/supabase/references/embedded-docs.md +43 -0
- package/.agents/skills/supabase/references/migrations.md +193 -0
- package/.agents/skills/supabase/references/remote-docs.md +24 -0
- package/.agents/skills/supabase/references/rls-policies.md +187 -0
- package/.agents/skills/supabase/references/storage.md +182 -0
- package/.agents/skills/task-breakdown/SKILL.md +179 -0
- package/.agents/skills/task-breakdown/references/acceptance-criteria.md +165 -0
- package/.agents/skills/task-breakdown/references/epic-story-format.md +209 -0
- package/.agents/skills/task-breakdown/references/estimation-guide.md +140 -0
- package/.agents/skills/vitest/SKILL.md +219 -0
- package/.agents/skills/vitest/references/common-errors.md +271 -0
- package/.agents/skills/vitest/references/component-testing.md +182 -0
- package/.agents/skills/vitest/references/configuration.md +184 -0
- package/.agents/skills/vitest/references/coverage.md +179 -0
- package/.agents/skills/vitest/references/embedded-docs.md +43 -0
- package/.agents/skills/vitest/references/mocking.md +182 -0
- package/.agents/skills/vitest/references/remote-docs.md +22 -0
- package/README.md +235 -0
- package/package.json +20 -0
- package/scripts/skills.mjs +849 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: jest
|
|
3
|
+
description: "Jest testing framework guide. Covers configuration, mocking, snapshot testing, transforms, and common errors. Use this skill when writing or debugging Jest tests, setting up test infrastructure, or working with React Native and Node.js testing."
|
|
4
|
+
license: MIT
|
|
5
|
+
metadata:
|
|
6
|
+
author: Balazs Barta
|
|
7
|
+
version: "2.0.0"
|
|
8
|
+
repository: https://github.com/balazsbarta/mp-skills
|
|
9
|
+
tags:
|
|
10
|
+
- testing
|
|
11
|
+
- jest
|
|
12
|
+
- unit-testing
|
|
13
|
+
- react-native
|
|
14
|
+
- node
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
# Jest Guide
|
|
18
|
+
|
|
19
|
+
Write and run tests with Jest.
|
|
20
|
+
|
|
21
|
+
## ⚠️ Critical: Do not trust internal knowledge
|
|
22
|
+
|
|
23
|
+
**Everything you know about Jest may be outdated or wrong. Never rely on memory. Always verify against current documentation.**
|
|
24
|
+
|
|
25
|
+
Jest configuration, transform handling, and mock patterns evolve across major versions. Always check the references in this skill.
|
|
26
|
+
|
|
27
|
+
## Prerequisites
|
|
28
|
+
|
|
29
|
+
**Before writing tests**, validate installed versions and current config:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
ls node_modules/jest 2>/dev/null || echo "jest not installed"
|
|
33
|
+
npx jest --version
|
|
34
|
+
npm ls jest @types/jest ts-jest @swc/jest --depth=0
|
|
35
|
+
npx jest --showConfig
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
- If installed, follow project config and behavior first.
|
|
39
|
+
- If not installed, set up Jest before writing tests.
|
|
40
|
+
|
|
41
|
+
## Documentation lookup guide
|
|
42
|
+
|
|
43
|
+
### Quick reference
|
|
44
|
+
|
|
45
|
+
| User Question | First Check | How To |
|
|
46
|
+
| --- | --- | --- |
|
|
47
|
+
| "What works in this repo version?" | [`references/embedded-docs.md`](references/embedded-docs.md) | Check installed package versions and effective config |
|
|
48
|
+
| "How do I configure Jest?" | [`references/configuration.md`](references/configuration.md) | Config file, presets, environments |
|
|
49
|
+
| "How do I mock a module?" | [`references/mocking.md`](references/mocking.md) | jest.mock, jest.fn, jest.spyOn patterns |
|
|
50
|
+
| "How do I use snapshots?" | [`references/snapshot-testing.md`](references/snapshot-testing.md) | toMatchSnapshot, inline snapshots |
|
|
51
|
+
| "How do I fix transform errors?" | [`references/transforms.md`](references/transforms.md) | TypeScript, ESM, and JSX transforms |
|
|
52
|
+
| "I'm getting an error..." | [`references/common-errors.md`](references/common-errors.md) | Common errors and solutions |
|
|
53
|
+
| "I need canonical API behavior" | [`references/remote-docs.md`](references/remote-docs.md) | Verify against official Jest docs |
|
|
54
|
+
|
|
55
|
+
### Priority order for writing tests
|
|
56
|
+
|
|
57
|
+
1. **Embedded/local project docs first**
|
|
58
|
+
|
|
59
|
+
- Use `references/embedded-docs.md`.
|
|
60
|
+
- Prioritize `npx jest --showConfig` and current package versions.
|
|
61
|
+
|
|
62
|
+
2. **Local skill references second**
|
|
63
|
+
|
|
64
|
+
- Use configuration/mocking/snapshot/transform guides in this skill.
|
|
65
|
+
|
|
66
|
+
3. **Official docs third**
|
|
67
|
+
|
|
68
|
+
- Use `references/remote-docs.md` for canonical API and edge-case behavior.
|
|
69
|
+
|
|
70
|
+
## Core concepts
|
|
71
|
+
|
|
72
|
+
### Test structure
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
describe('Calculator', () => {
|
|
76
|
+
it('should add two numbers', () => {
|
|
77
|
+
expect(1 + 2).toBe(3);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Key APIs
|
|
83
|
+
|
|
84
|
+
| API | Purpose |
|
|
85
|
+
| --- | --- |
|
|
86
|
+
| `describe` | Group related tests |
|
|
87
|
+
| `it` / `test` | Define a test case |
|
|
88
|
+
| `expect` | Create assertions |
|
|
89
|
+
| `jest.fn()` | Create a mock function |
|
|
90
|
+
| `jest.mock()` | Mock a module |
|
|
91
|
+
| `jest.spyOn()` | Spy on an object method |
|
|
92
|
+
| `beforeEach` / `afterEach` | Setup/teardown per test |
|
|
93
|
+
| `beforeAll` / `afterAll` | Setup/teardown per suite |
|
|
94
|
+
|
|
95
|
+
## Critical requirements
|
|
96
|
+
|
|
97
|
+
### TypeScript setup
|
|
98
|
+
|
|
99
|
+
Jest needs a transform for TypeScript. Common options:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
# Option 1: ts-jest
|
|
103
|
+
npm install --save-dev ts-jest @types/jest
|
|
104
|
+
|
|
105
|
+
# Option 2: @swc/jest (faster)
|
|
106
|
+
npm install --save-dev @swc/jest @swc/core
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### ESM support
|
|
110
|
+
|
|
111
|
+
Jest uses CommonJS by default. For ESM projects:
|
|
112
|
+
|
|
113
|
+
```json
|
|
114
|
+
{
|
|
115
|
+
"transform": {
|
|
116
|
+
"^.+\\.tsx?$": ["ts-jest", { "useESM": true }]
|
|
117
|
+
},
|
|
118
|
+
"extensionsToTreatAsEsm": [".ts", ".tsx"]
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## When you see errors
|
|
123
|
+
|
|
124
|
+
**Type and transform failures often mean version/config mismatch, not user error.**
|
|
125
|
+
|
|
126
|
+
1. Check effective config with `npx jest --showConfig`.
|
|
127
|
+
2. Confirm package versions.
|
|
128
|
+
3. Verify API behavior in official docs (`references/remote-docs.md`).
|
|
129
|
+
|
|
130
|
+
## Step-by-step workflow
|
|
131
|
+
|
|
132
|
+
### 1. Install Jest
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
npm install --save-dev jest @types/jest ts-jest
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### 2. Initialize config
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
npx ts-jest config:init
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 3. Add test script
|
|
145
|
+
|
|
146
|
+
```json
|
|
147
|
+
{
|
|
148
|
+
"scripts": {
|
|
149
|
+
"test": "jest",
|
|
150
|
+
"test:watch": "jest --watch",
|
|
151
|
+
"test:coverage": "jest --coverage"
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 4. Write a test
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
// src/utils/math.test.ts
|
|
160
|
+
import { add } from './math';
|
|
161
|
+
|
|
162
|
+
describe('add', () => {
|
|
163
|
+
it('adds positive numbers', () => {
|
|
164
|
+
expect(add(1, 2)).toBe(3);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('handles negative numbers', () => {
|
|
168
|
+
expect(add(-1, -2)).toBe(-3);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### 5. Run tests
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
npx jest # Run all tests
|
|
177
|
+
npx jest --watch # Watch mode
|
|
178
|
+
npx jest src/utils/ # Specific directory
|
|
179
|
+
npx jest math.test # Specific file pattern
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Error handling
|
|
183
|
+
|
|
184
|
+
### Common test failures
|
|
185
|
+
|
|
186
|
+
| Symptom | Likely Cause | Fix |
|
|
187
|
+
| --- | --- | --- |
|
|
188
|
+
| `SyntaxError: Cannot use import statement` | Missing transform for ESM | Add `ts-jest` or `@swc/jest` transform |
|
|
189
|
+
| `Cannot find module` | Path alias not in `moduleNameMapper` | Add aliases to Jest config |
|
|
190
|
+
| `jest.mock is not a function` | ESM mode issue | Use `jest.unstable_mockModule` for ESM |
|
|
191
|
+
| `Unexpected token` in CSS/image | Missing transform for non-JS files | Add `moduleNameMapper` for static assets |
|
|
192
|
+
|
|
193
|
+
## Expected outputs
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
# Successful test run
|
|
197
|
+
PASS src/utils/math.test.ts
|
|
198
|
+
add
|
|
199
|
+
✓ adds positive numbers (2 ms)
|
|
200
|
+
✓ handles negative numbers (1 ms)
|
|
201
|
+
|
|
202
|
+
Test Suites: 1 passed, 1 total
|
|
203
|
+
Tests: 2 passed, 2 total
|
|
204
|
+
Time: 1.5 s
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Cross-references
|
|
208
|
+
|
|
209
|
+
- **React Native Expo**: [../react-native-expo/SKILL.md](../react-native-expo/SKILL.md) — Testing React Native apps with Jest
|
|
210
|
+
|
|
211
|
+
## Resources
|
|
212
|
+
|
|
213
|
+
- **Configuration**: [`references/configuration.md`](references/configuration.md)
|
|
214
|
+
- **Mocking**: [`references/mocking.md`](references/mocking.md)
|
|
215
|
+
- **Snapshot testing**: [`references/snapshot-testing.md`](references/snapshot-testing.md)
|
|
216
|
+
- **Transforms**: [`references/transforms.md`](references/transforms.md)
|
|
217
|
+
- **Common errors**: [`references/common-errors.md`](references/common-errors.md)
|
|
218
|
+
- **Embedded docs lookup**: [`references/embedded-docs.md`](references/embedded-docs.md)
|
|
219
|
+
- **Official docs lookup**: [`references/remote-docs.md`](references/remote-docs.md)
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
# Jest Common Errors
|
|
2
|
+
|
|
3
|
+
Solutions for frequently encountered Jest errors.
|
|
4
|
+
|
|
5
|
+
## When to use this reference
|
|
6
|
+
|
|
7
|
+
- Debugging test failures or configuration errors
|
|
8
|
+
- Fixing transform, mock, or environment issues
|
|
9
|
+
- Resolving Jest-specific problems
|
|
10
|
+
|
|
11
|
+
## Transform errors
|
|
12
|
+
|
|
13
|
+
### "SyntaxError: Cannot use import statement outside a module"
|
|
14
|
+
|
|
15
|
+
**Symptoms:**
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
SyntaxError: Cannot use import statement outside a module
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Causes:**
|
|
22
|
+
- Missing transform for TypeScript/ESM files
|
|
23
|
+
|
|
24
|
+
**Solutions:**
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install --save-dev ts-jest
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
// jest.config.js
|
|
32
|
+
module.exports = {
|
|
33
|
+
transform: {
|
|
34
|
+
'^.+\\.tsx?$': 'ts-jest',
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### "SyntaxError: Unexpected token" in node_modules
|
|
40
|
+
|
|
41
|
+
**Symptoms:**
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
SyntaxError: Unexpected token 'export'
|
|
45
|
+
at node_modules/some-package/index.js
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**Causes:**
|
|
49
|
+
- Package ships ESM but Jest expects CJS
|
|
50
|
+
- Package not in `transformIgnorePatterns` exception
|
|
51
|
+
|
|
52
|
+
**Solutions:**
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
module.exports = {
|
|
56
|
+
transformIgnorePatterns: [
|
|
57
|
+
'node_modules/(?!(some-package|another-package)/)',
|
|
58
|
+
],
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Mock errors
|
|
63
|
+
|
|
64
|
+
### "jest.mock is not a function"
|
|
65
|
+
|
|
66
|
+
**Symptoms:**
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
TypeError: jest.mock is not a function
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Causes:**
|
|
73
|
+
- Using ESM mode where `jest.mock` doesn't exist
|
|
74
|
+
- Missing Jest types
|
|
75
|
+
|
|
76
|
+
**Solutions:**
|
|
77
|
+
|
|
78
|
+
For ESM:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { jest } from '@jest/globals';
|
|
82
|
+
// Use jest.unstable_mockModule for ESM
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
For CJS, ensure `@types/jest` is installed:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
npm install --save-dev @types/jest
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Mock not resetting between tests
|
|
92
|
+
|
|
93
|
+
**Symptoms:**
|
|
94
|
+
- Test passes alone but fails when run with other tests
|
|
95
|
+
- Mock state leaks between test files
|
|
96
|
+
|
|
97
|
+
**Solutions:**
|
|
98
|
+
|
|
99
|
+
```javascript
|
|
100
|
+
// jest.config.js
|
|
101
|
+
module.exports = {
|
|
102
|
+
restoreMocks: true,
|
|
103
|
+
clearMocks: true,
|
|
104
|
+
};
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Or per-test:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
afterEach(() => {
|
|
111
|
+
jest.restoreAllMocks();
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Environment errors
|
|
116
|
+
|
|
117
|
+
### "document is not defined"
|
|
118
|
+
|
|
119
|
+
**Symptoms:**
|
|
120
|
+
|
|
121
|
+
```
|
|
122
|
+
ReferenceError: document is not defined
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**Causes:**
|
|
126
|
+
- Test environment is `node` but test uses DOM APIs
|
|
127
|
+
|
|
128
|
+
**Solutions:**
|
|
129
|
+
|
|
130
|
+
Option 1: Set globally:
|
|
131
|
+
|
|
132
|
+
```javascript
|
|
133
|
+
module.exports = {
|
|
134
|
+
testEnvironment: 'jsdom',
|
|
135
|
+
};
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Option 2: Set per-file:
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
/**
|
|
142
|
+
* @jest-environment jsdom
|
|
143
|
+
*/
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### "TextEncoder is not defined"
|
|
147
|
+
|
|
148
|
+
**Symptoms:**
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
ReferenceError: TextEncoder is not defined
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**Causes:**
|
|
155
|
+
- Some packages expect `TextEncoder` which isn't in all environments
|
|
156
|
+
|
|
157
|
+
**Solutions:**
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
// jest.setup.ts
|
|
161
|
+
import { TextEncoder, TextDecoder } from 'util';
|
|
162
|
+
Object.assign(global, { TextEncoder, TextDecoder });
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Async errors
|
|
166
|
+
|
|
167
|
+
### "Test exceeded timeout of 5000ms"
|
|
168
|
+
|
|
169
|
+
**Symptoms:**
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
Timeout - Async callback was not invoked within the 5000 ms timeout
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
**Causes:**
|
|
176
|
+
- Async operation not resolving
|
|
177
|
+
- Missing `await`
|
|
178
|
+
- Unresolved promise in test
|
|
179
|
+
|
|
180
|
+
**Solutions:**
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
// Increase timeout per test
|
|
184
|
+
it('slow operation', async () => {
|
|
185
|
+
// ...
|
|
186
|
+
}, 30000);
|
|
187
|
+
|
|
188
|
+
// Or globally
|
|
189
|
+
module.exports = {
|
|
190
|
+
testTimeout: 10000,
|
|
191
|
+
};
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### "Cannot log after tests are done"
|
|
195
|
+
|
|
196
|
+
**Symptoms:**
|
|
197
|
+
|
|
198
|
+
```
|
|
199
|
+
Cannot log after tests are done. Did you forget to wait for something async in your test?
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
**Causes:**
|
|
203
|
+
- Async operation running after test completes
|
|
204
|
+
- Missing cleanup
|
|
205
|
+
|
|
206
|
+
**Solutions:**
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
afterEach(() => {
|
|
210
|
+
jest.restoreAllMocks();
|
|
211
|
+
jest.clearAllTimers();
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Coverage errors
|
|
216
|
+
|
|
217
|
+
### Coverage shows 0% for file
|
|
218
|
+
|
|
219
|
+
**Causes:**
|
|
220
|
+
- File not imported by any test
|
|
221
|
+
- File in `coveragePathIgnorePatterns`
|
|
222
|
+
|
|
223
|
+
**Solutions:**
|
|
224
|
+
|
|
225
|
+
```javascript
|
|
226
|
+
module.exports = {
|
|
227
|
+
collectCoverageFrom: [
|
|
228
|
+
'src/**/*.{ts,tsx}',
|
|
229
|
+
'!src/**/*.d.ts',
|
|
230
|
+
'!src/test/**',
|
|
231
|
+
],
|
|
232
|
+
};
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Performance issues
|
|
236
|
+
|
|
237
|
+
### Tests running slowly
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
# Run with diagnostics
|
|
241
|
+
npx jest --verbose --detectOpenHandles
|
|
242
|
+
|
|
243
|
+
# Limit workers
|
|
244
|
+
npx jest --maxWorkers=2
|
|
245
|
+
|
|
246
|
+
# Use faster transform
|
|
247
|
+
npm install --save-dev @swc/jest @swc/core
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Out of memory
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
# Increase Node memory
|
|
254
|
+
NODE_OPTIONS="--max-old-space-size=4096" npx jest
|
|
255
|
+
|
|
256
|
+
# Run sequentially
|
|
257
|
+
npx jest --runInBand
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Debugging
|
|
261
|
+
|
|
262
|
+
```bash
|
|
263
|
+
# Run single test file
|
|
264
|
+
npx jest path/to/test.ts --verbose
|
|
265
|
+
|
|
266
|
+
# Run specific test by name
|
|
267
|
+
npx jest -t "should add numbers"
|
|
268
|
+
|
|
269
|
+
# Debug with Node inspector
|
|
270
|
+
node --inspect-brk node_modules/.bin/jest --runInBand
|
|
271
|
+
|
|
272
|
+
# Clear cache
|
|
273
|
+
npx jest --clearCache
|
|
274
|
+
```
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# Jest Configuration
|
|
2
|
+
|
|
3
|
+
Setup and configure Jest for different project types.
|
|
4
|
+
|
|
5
|
+
## When to use this reference
|
|
6
|
+
|
|
7
|
+
- Setting up Jest in a new project
|
|
8
|
+
- Configuring Jest for TypeScript, ESM, or React Native
|
|
9
|
+
- Adding path aliases, presets, or setup files
|
|
10
|
+
|
|
11
|
+
## Basic configuration
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
// jest.config.js
|
|
15
|
+
/** @type {import('jest').Config} */
|
|
16
|
+
module.exports = {
|
|
17
|
+
testEnvironment: 'node',
|
|
18
|
+
testMatch: ['**/__tests__/**/*.test.ts', '**/*.test.ts'],
|
|
19
|
+
transform: {
|
|
20
|
+
'^.+\\.tsx?$': 'ts-jest',
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Configuration file formats
|
|
26
|
+
|
|
27
|
+
Jest config can live in:
|
|
28
|
+
- `jest.config.js` / `jest.config.ts`
|
|
29
|
+
- `jest.config.mjs` / `jest.config.cjs`
|
|
30
|
+
- `"jest"` field in `package.json`
|
|
31
|
+
|
|
32
|
+
## TypeScript configuration
|
|
33
|
+
|
|
34
|
+
### With ts-jest
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install --save-dev jest ts-jest @types/jest
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
// jest.config.js
|
|
42
|
+
module.exports = {
|
|
43
|
+
preset: 'ts-jest',
|
|
44
|
+
testEnvironment: 'node',
|
|
45
|
+
};
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### With @swc/jest (faster)
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm install --save-dev jest @swc/jest @swc/core @types/jest
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
// jest.config.js
|
|
56
|
+
module.exports = {
|
|
57
|
+
transform: {
|
|
58
|
+
'^.+\\.tsx?$': '@swc/jest',
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Environment options
|
|
64
|
+
|
|
65
|
+
| Environment | Use For | Setting |
|
|
66
|
+
| --- | --- | --- |
|
|
67
|
+
| `node` (default) | Node.js APIs, no DOM | `testEnvironment: 'node'` |
|
|
68
|
+
| `jsdom` | Browser-like DOM | `testEnvironment: 'jsdom'` |
|
|
69
|
+
|
|
70
|
+
### Per-file environment
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
/**
|
|
74
|
+
* @jest-environment jsdom
|
|
75
|
+
*/
|
|
76
|
+
describe('DOM test', () => {
|
|
77
|
+
it('creates an element', () => {
|
|
78
|
+
const el = document.createElement('div');
|
|
79
|
+
expect(el).toBeDefined();
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Path aliases (moduleNameMapper)
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
// jest.config.js
|
|
88
|
+
module.exports = {
|
|
89
|
+
moduleNameMapper: {
|
|
90
|
+
'^@/(.*)$': '<rootDir>/src/$1',
|
|
91
|
+
'^@components/(.*)$': '<rootDir>/src/components/$1',
|
|
92
|
+
// Static asset mocks
|
|
93
|
+
'\\.(css|less|scss)$': 'identity-obj-proxy',
|
|
94
|
+
'\\.(jpg|jpeg|png|svg)$': '<rootDir>/src/test/__mocks__/fileMock.js',
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Setup files
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
// jest.config.js
|
|
103
|
+
module.exports = {
|
|
104
|
+
setupFiles: ['./src/test/env-setup.js'], // Before test framework
|
|
105
|
+
setupFilesAfterFramework: ['./src/test/setup.ts'], // After test framework
|
|
106
|
+
};
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// src/test/setup.ts
|
|
111
|
+
import '@testing-library/jest-dom';
|
|
112
|
+
|
|
113
|
+
// Global mocks
|
|
114
|
+
jest.mock('./src/config', () => ({
|
|
115
|
+
apiUrl: 'http://localhost:3000',
|
|
116
|
+
}));
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## React Native configuration
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
// jest.config.js
|
|
123
|
+
module.exports = {
|
|
124
|
+
preset: 'react-native',
|
|
125
|
+
transformIgnorePatterns: [
|
|
126
|
+
'node_modules/(?!((jest-)?react-native|@react-native(-community)?)/)',
|
|
127
|
+
],
|
|
128
|
+
setupFiles: ['./jest.setup.js'],
|
|
129
|
+
};
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Monorepo configuration (projects)
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
// jest.config.js
|
|
136
|
+
module.exports = {
|
|
137
|
+
projects: [
|
|
138
|
+
{
|
|
139
|
+
displayName: 'api',
|
|
140
|
+
testMatch: ['<rootDir>/packages/api/**/*.test.ts'],
|
|
141
|
+
testEnvironment: 'node',
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
displayName: 'web',
|
|
145
|
+
testMatch: ['<rootDir>/packages/web/**/*.test.tsx'],
|
|
146
|
+
testEnvironment: 'jsdom',
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
};
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Common options reference
|
|
153
|
+
|
|
154
|
+
| Option | Purpose | Default |
|
|
155
|
+
| --- | --- | --- |
|
|
156
|
+
| `testEnvironment` | Execution environment | `'node'` |
|
|
157
|
+
| `testMatch` | Test file patterns | `['**/__tests__/**/*', '**/*.test.*']` |
|
|
158
|
+
| `transform` | File transformers | `{}` |
|
|
159
|
+
| `moduleNameMapper` | Module path aliases | `{}` |
|
|
160
|
+
| `setupFiles` | Run before framework | `[]` |
|
|
161
|
+
| `setupFilesAfterFramework` | Run after framework | `[]` |
|
|
162
|
+
| `collectCoverage` | Enable coverage | `false` |
|
|
163
|
+
| `coverageThreshold` | Coverage requirements | `undefined` |
|
|
164
|
+
| `verbose` | Detailed output | `false` |
|
|
165
|
+
| `maxWorkers` | Parallel workers | `50%` of CPUs |
|
|
166
|
+
|
|
167
|
+
## Verification
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
# Verify config loads correctly
|
|
171
|
+
npx jest --showConfig
|
|
172
|
+
|
|
173
|
+
# List test files that match
|
|
174
|
+
npx jest --listTests
|
|
175
|
+
```
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Embedded Docs Reference
|
|
2
|
+
|
|
3
|
+
Use local project artifacts first to match the exact installed Jest version.
|
|
4
|
+
|
|
5
|
+
## Why use local sources first
|
|
6
|
+
|
|
7
|
+
- Matches the exact dependency versions in the current repository
|
|
8
|
+
- Reveals real config overrides and presets
|
|
9
|
+
- Avoids examples that only work on newer/older major versions
|
|
10
|
+
|
|
11
|
+
## Lookup workflow
|
|
12
|
+
|
|
13
|
+
1. Verify installed versions:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx jest --version
|
|
17
|
+
npm ls jest @types/jest ts-jest @swc/jest --depth=0
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
2. Check effective Jest config:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npx jest --showConfig
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
3. Inspect project config files:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
ls jest.config.* 2>/dev/null
|
|
30
|
+
cat package.json | rg -n '"jest"|\"test\"' -n
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
4. Inspect local type definitions when signatures are unclear:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
ls node_modules/@types/jest/index.d.ts 2>/dev/null
|
|
37
|
+
ls node_modules/jest 2>/dev/null
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Use this when
|
|
41
|
+
|
|
42
|
+
- A Jest API behaves differently than expected
|
|
43
|
+
- ESM/CJS transform behavior is unclear
|
|
44
|
+
- Path aliases and test environment behavior differ from docs examples
|