用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/tomevault-io/skills-registry --skill jest命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | jest |
| description | | Use when this capability is needed. |
Deep Knowledge: Use
mcp__documentation__fetch_docswith technology:jestfor comprehensive documentation.
playwright or cypress for browser automationvitest which is faster and Vite-nativetesting-library for React/Vue componentsdescribe('UserService', () => {
let service: UserService;
beforeEach(() => {
service = new UserService();
});
afterEach(() => {
jest.clearAllMocks();
});
it('should create user', async () => {
const user = await service.create({ name: 'John' });
expect(user.name).toBe('John');
});
});
// Mock function
const mockFn = jest.fn();
mockFn.mockReturnValue(42);
mockFn.mockResolvedValue({ data: [] });
// Mock module
jest.mock('./api', () => ({
fetchUsers: jest.fn().mockResolvedValue([])
}));
// Spy
const spy = jest.spyOn(console, 'log');
expect(spy).toHaveBeenCalledWith('message');
// Mock implementation
mockFn.mockImplementation((x) => x * 2);
// Same as Vitest, Jest-compatible
expect(value).toBe(exact);
expect(value).toEqual(deepEqual);
expect(obj).toMatchObject({ name: 'John' });
expect(arr).toContainEqual({ id: 1 });
it('should match snapshot', () => {
const component = render(<UserCard user={user} />);
expect(component).toMatchSnapshot();
});
it('should match inline snapshot', () => {
expect(formatDate(date)).toMatchInlineSnapshot(`"2024-01-15"`);
});
it('should resolve', async () => {
await expect(fetchData()).resolves.toEqual({ data: [] });
});
it('should reject', async () => {
await expect(fetchFail()).rejects.toThrow('Error');
});
// jest.config.js
module.exports = {
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
moduleNameMapper: { '^@/(.*)$': '<rootDir>/src/$1' }
};
// Isolated tests with proper setup/teardown
describe('OrderService', () => {
let service: OrderService;
let mockRepository: jest.Mocked<OrderRepository>;
beforeEach(() => {
mockRepository = {
findById: jest.fn(),
save: jest.fn(),
delete: jest.fn(),
} as jest.Mocked<OrderRepository>;
service = new OrderService(mockRepository);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('processOrder', () => {
it('should process valid order', async () => {
// Arrange
const order = createTestOrder({ status: 'pending' });
mockRepository.findById.mockResolvedValue(order);
mockRepository.save.mockResolvedValue({ ...order, status: 'processed' });
// Act
const result = await service.processOrder(order.);
(result.).();
(mockRepository.).(
expect.({ : })
);
});
});
});
// jest.config.js
module.exports = {
collectCoverageFrom: [
'src/**/*.{ts,tsx}',
'!src/**/*.d.ts',
'!src/**/index.ts',
'!src/**/*.stories.tsx',
],
coverageThreshold: {
global: {
branches: 75,
functions: 80,
lines: 80,
statements: 80,
},
},
coverageReporters: ['text', 'lcov', 'html'],
};
# GitHub Actions
- name: Run tests
run: npm test -- --ci --coverage --reporters=default --reporters=jest-junit
env:
JEST_JUNIT_OUTPUT_DIR: ./reports
- name: Upload coverage
uses: codecov/codecov-action@v3
// package.json
{
"scripts": {
"test": "jest",
"test:ci": "jest --ci --coverage --maxWorkers=2",
"test:watch": "jest --watch"
}
}
// jest.config.js - Speed up tests
module.exports = {
// Run tests in parallel
maxWorkers: '50%',
// Cache transformations
cacheDirectory: '<rootDir>/.jest-cache',
// Only run affected tests
onlyChanged: true,
// Fail fast in CI
bail: process.env.CI ? 1 : 0,
};
| Metric | Target |
|---|---|
| Line coverage | > 80% |
| Branch coverage | > 75% |
| Test execution time | < 120s |
| Flaky test rate | 0% |
| Anti-Pattern | Why It's Bad | Solution |
|---|---|---|
| Over-using snapshots | Tests pass without validation | Use snapshots sparingly, prefer explicit assertions |
| Not reviewing snapshot changes | Bugs slip into snapshots | Always review snapshot diffs manually |
| Testing implementation details | Brittle tests | Test user-facing behavior |
| Shared mutable state | Flaky tests | Isolate setup with beforeEach |
| Skipping error paths | Production failures | Test both success and error scenarios |
| Large snapshot files | Hard to review | Break into smaller focused snapshots |
| No mock cleanup | Tests affect each other | jest.clearAllMocks() in afterEach |
| Problem | Likely Cause | Solution |
|---|---|---|
| "Cannot find module" | Mock path incorrect | Ensure mock path matches import exactly |
| Test timeout | Missing await | Add await to all async operations |
| "Received: serializes to same string" | Object reference comparison | Use toEqual() not toBe() for objects |
| Snapshot mismatch | Intentional code change | Review diff, update with -u flag if correct |
| "expect().resolves is not a function" | Old Jest version | Update to Jest 27+ or use async/await |
| Tests pass locally, fail in CI | Timezone/locale differences | Mock Date, use fixed timezones |
Source: claude-dev-suite/claude-dev-suite — distributed by TomeVault.