jest-advanced
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJest Advanced
Jest 高级特性
Master advanced Jest features including custom matchers, parameterized tests with test.each, coverage configuration, and performance optimization. This skill covers sophisticated testing techniques for complex scenarios and large test suites.
掌握 Jest 高级功能,包括自定义匹配器、使用 test.each 的参数化测试、覆盖率配置以及性能优化。本 skill 覆盖了适用于复杂场景和大型测试套件的 sophisticated 测试技术。
Custom Matchers
自定义匹配器
Creating Custom Matchers
创建自定义匹配器
javascript
// matchers/toBeWithinRange.js
export function toBeWithinRange(received, floor, ceiling) {
const pass = received >= floor && received <= ceiling;
if (pass) {
return {
message: () =>
`expected ${received} not to be within range ${floor} - ${ceiling}`,
pass: true
};
} else {
return {
message: () =>
`expected ${received} to be within range ${floor} - ${ceiling}`,
pass: false
};
}
}
// jest.setup.js
import { toBeWithinRange } from './matchers/toBeWithinRange';
expect.extend({
toBeWithinRange
});
// test file
describe('Custom matcher', () => {
it('should check if number is within range', () => {
expect(5).toBeWithinRange(1, 10);
expect(15).not.toBeWithinRange(1, 10);
});
});javascript
// matchers/toBeWithinRange.js
export function toBeWithinRange(received, floor, ceiling) {
const pass = received >= floor && received <= ceiling;
if (pass) {
return {
message: () =>
`expected ${received} not to be within range ${floor} - ${ceiling}`,
pass: true
};
} else {
return {
message: () =>
`expected ${received} to be within range ${floor} - ${ceiling}`,
pass: false
};
}
}
// jest.setup.js
import { toBeWithinRange } from './matchers/toBeWithinRange';
expect.extend({
toBeWithinRange
});
// test file
describe('Custom matcher', () => {
it('should check if number is within range', () => {
expect(5).toBeWithinRange(1, 10);
expect(15).not.toBeWithinRange(1, 10);
});
});Async Custom Matcher
异步自定义匹配器
javascript
// matchers/toResolveWithin.js
export async function toResolveWithin(received, timeout) {
const startTime = Date.now();
try {
await received;
const duration = Date.now() - startTime;
const pass = duration <= timeout;
return {
message: () =>
pass
? `expected promise not to resolve within ${timeout}ms (resolved in ${duration}ms)`
: `expected promise to resolve within ${timeout}ms (took ${duration}ms)`,
pass
};
} catch (error) {
return {
message: () => `expected promise to resolve but it rejected with ${error}`,
pass: false
};
}
}
// Usage
expect.extend({ toResolveWithin });
it('should resolve quickly', async () => {
await expect(fetchData()).toResolveWithin(1000);
});javascript
// matchers/toResolveWithin.js
export async function toResolveWithin(received, timeout) {
const startTime = Date.now();
try {
await received;
const duration = Date.now() - startTime;
const pass = duration <= timeout;
return {
message: () =>
pass
? `expected promise not to resolve within ${timeout}ms (resolved in ${duration}ms)`
: `expected promise to resolve within ${timeout}ms (took ${duration}ms)`,
pass
};
} catch (error) {
return {
message: () => `expected promise to resolve but it rejected with ${error}`,
pass: false
};
}
}
// Usage
expect.extend({ toResolveWithin });
it('should resolve quickly', async () => {
await expect(fetchData()).toResolveWithin(1000);
});Type-Safe Custom Matchers (TypeScript)
类型安全的自定义匹配器(TypeScript)
typescript
// matchers/index.ts
interface CustomMatchers<R = unknown> {
toBeWithinRange(floor: number, ceiling: number): R;
toHaveValidEmail(): R;
}
declare global {
namespace jest {
interface Expect extends CustomMatchers {}
interface Matchers<R> extends CustomMatchers<R> {}
interface InverseAsymmetricMatchers extends CustomMatchers {}
}
}
export function toBeWithinRange(
this: jest.MatcherContext,
received: number,
floor: number,
ceiling: number
): jest.CustomMatcherResult {
const pass = received >= floor && received <= ceiling;
return {
message: () =>
pass
? `expected ${received} not to be within range ${floor} - ${ceiling}`
: `expected ${received} to be within range ${floor} - ${ceiling}`,
pass
};
}
export function toHaveValidEmail(
this: jest.MatcherContext,
received: string
): jest.CustomMatcherResult {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const pass = emailRegex.test(received);
return {
message: () =>
pass
? `expected ${received} not to be a valid email`
: `expected ${received} to be a valid email`,
pass
};
}
// jest.setup.ts
import * as matchers from './matchers';
expect.extend(matchers);typescript
// matchers/index.ts
interface CustomMatchers<R = unknown> {
toBeWithinRange(floor: number, ceiling: number): R;
toHaveValidEmail(): R;
}
declare global {
namespace jest {
interface Expect extends CustomMatchers {}
interface Matchers<R> extends CustomMatchers<R> {}
interface InverseAsymmetricMatchers extends CustomMatchers {}
}
}
export function toBeWithinRange(
this: jest.MatcherContext,
received: number,
floor: number,
ceiling: number
): jest.CustomMatcherResult {
const pass = received >= floor && received <= ceiling;
return {
message: () =>
pass
? `expected ${received} not to be within range ${floor} - ${ceiling}`
: `expected ${received} to be within range ${floor} - ${ceiling}`,
pass
};
}
export function toHaveValidEmail(
this: jest.MatcherContext,
received: string
): jest.CustomMatcherResult {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const pass = emailRegex.test(received);
return {
message: () =>
pass
? `expected ${received} not to be a valid email`
: `expected ${received} to be a valid email`,
pass
};
}
// jest.setup.ts
import * as matchers from './matchers';
expect.extend(matchers);Parameterized Tests
参数化测试
test.each with Arrays
使用数组的 test.each
javascript
describe('Addition', () => {
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('add(%i, %i) should equal %i', (a, b, expected) => {
expect(add(a, b)).toBe(expected);
});
});javascript
describe('Addition', () => {
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('add(%i, %i) should equal %i', (a, b, expected) => {
expect(add(a, b)).toBe(expected);
});
});test.each with Objects
使用对象的 test.each
javascript
describe('User validation', () => {
test.each([
{ email: 'test@example.com', valid: true },
{ email: 'invalid', valid: false },
{ email: 'test@', valid: false },
{ email: '@example.com', valid: false },
])('validateEmail($email) should return $valid', ({ email, valid }) => {
expect(validateEmail(email)).toBe(valid);
});
});javascript
describe('User validation', () => {
test.each([
{ email: 'test@example.com', valid: true },
{ email: 'invalid', valid: false },
{ email: 'test@', valid: false },
{ email: '@example.com', valid: false },
])('validateEmail($email) should return $valid', ({ email, valid }) => {
expect(validateEmail(email)).toBe(valid);
});
});test.each with Template Literals
使用模板字符串的 test.each
javascript
describe('String operations', () => {
test.each`
input | method | expected
${'hello'} | ${'upper'} | ${'HELLO'}
${'WORLD'} | ${'lower'} | ${'world'}
${'HeLLo'} | ${'title'} | ${'Hello'}
`('transform $input using $method should return $expected',
({ input, method, expected }) => {
expect(transform(input, method)).toBe(expected);
}
);
});javascript
describe('String operations', () => {
test.each`
input | method | expected
${'hello'} | ${'upper'} | ${'HELLO'}
${'WORLD'} | ${'lower'} | ${'world'}
${'HeLLo'} | ${'title'} | ${'Hello'}
`('transform $input using $method should return $expected',
({ input, method, expected }) => {
expect(transform(input, method)).toBe(expected);
}
);
});describe.each for Multiple Test Suites
使用 describe.each 创建多个测试套件
javascript
describe.each([
{ browser: 'Chrome', version: 90 },
{ browser: 'Firefox', version: 88 },
{ browser: 'Safari', version: 14 },
])('Browser compatibility - $browser', ({ browser, version }) => {
it(`should support ${browser} version ${version}`, () => {
expect(isSupported(browser, version)).toBe(true);
});
it(`should handle ${browser} specific features`, () => {
expect(getFeatures(browser)).toBeDefined();
});
});javascript
describe.each([
{ browser: 'Chrome', version: 90 },
{ browser: 'Firefox', version: 88 },
{ browser: 'Safari', version: 14 },
])('Browser compatibility - $browser', ({ browser, version }) => {
it(`should support ${browser} version ${version}`, () => {
expect(isSupported(browser, version)).toBe(true);
});
it(`should handle ${browser} specific features`, () => {
expect(getFeatures(browser)).toBeDefined();
});
});Coverage Configuration
覆盖率配置
Advanced Coverage Settings
高级覆盖率设置
javascript
// jest.config.js
module.exports = {
collectCoverage: true,
coverageDirectory: 'coverage',
coverageReporters: ['text', 'lcov', 'html', 'json-summary'],
collectCoverageFrom: [
'src/**/*.{js,jsx,ts,tsx}',
'!src/**/*.d.ts',
'!src/**/*.stories.{js,jsx,ts,tsx}',
'!src/**/__tests__/**',
'!src/**/types/**',
'!src/index.{js,ts}',
],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80
},
'./src/core/': {
branches: 90,
functions: 95,
lines: 95,
statements: 95
},
'./src/utils/': {
branches: 85,
functions: 90,
lines: 90,
statements: 90
}
},
coveragePathIgnorePatterns: [
'/node_modules/',
'/dist/',
'/build/',
'/__mocks__/',
'/coverage/'
]
};javascript
// jest.config.js
module.exports = {
collectCoverage: true,
coverageDirectory: 'coverage',
coverageReporters: ['text', 'lcov', 'html', 'json-summary'],
collectCoverageFrom: [
'src/**/*.{js,jsx,ts,tsx}',
'!src/**/*.d.ts',
'!src/**/*.stories.{js,jsx,ts,tsx}',
'!src/**/__tests__/**',
'!src/**/types/**',
'!src/index.{js,ts}',
],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80
},
'./src/core/': {
branches: 90,
functions: 95,
lines: 95,
statements: 95
},
'./src/utils/': {
branches: 85,
functions: 90,
lines: 90,
statements: 90
}
},
coveragePathIgnorePatterns: [
'/node_modules/',
'/dist/',
'/build/',
'/__mocks__/',
'/coverage/'
]
};Custom Coverage Reporter
自定义覆盖率报告器
javascript
// custom-reporter.js
class CustomCoverageReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig;
this._options = options;
}
onRunComplete(contexts, results) {
const { coverageMap } = results;
if (!coverageMap) {
return;
}
const summary = coverageMap.getCoverageSummary();
const metrics = {
lines: summary.lines.pct,
statements: summary.statements.pct,
functions: summary.functions.pct,
branches: summary.branches.pct
};
console.log('\nCoverage Summary:');
console.log(`Lines: ${metrics.lines}%`);
console.log(`Statements: ${metrics.statements}%`);
console.log(`Functions: ${metrics.functions}%`);
console.log(`Branches: ${metrics.branches}%`);
// Send to external service
if (this._options.webhook) {
this.sendToWebhook(metrics, this._options.webhook);
}
}
async sendToWebhook(metrics, url) {
// Implementation
}
}
module.exports = CustomCoverageReporter;
// jest.config.js
module.exports = {
coverageReporters: [
'text',
['./custom-reporter.js', { webhook: 'https://example.com/coverage' }]
]
};javascript
// custom-reporter.js
class CustomCoverageReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig;
this._options = options;
}
onRunComplete(contexts, results) {
const { coverageMap } = results;
if (!coverageMap) {
return;
}
const summary = coverageMap.getCoverageSummary();
const metrics = {
lines: summary.lines.pct,
statements: summary.statements.pct,
functions: summary.functions.pct,
branches: summary.branches.pct
};
console.log('\nCoverage Summary:');
console.log(`Lines: ${metrics.lines}%`);
console.log(`Statements: ${metrics.statements}%`);
console.log(`Functions: ${metrics.functions}%`);
console.log(`Branches: ${metrics.branches}%`);
// Send to external service
if (this._options.webhook) {
this.sendToWebhook(metrics, this._options.webhook);
}
}
async sendToWebhook(metrics, url) {
// Implementation
}
}
module.exports = CustomCoverageReporter;
// jest.config.js
module.exports = {
coverageReporters: [
'text',
['./custom-reporter.js', { webhook: 'https://example.com/coverage' }]
]
};Performance Optimization
性能优化
Running Tests in Parallel
并行运行测试
javascript
// jest.config.js
module.exports = {
maxWorkers: '50%', // Use 50% of available CPU cores
// or specify a number
// maxWorkers: 4,
// Parallelize tests within a file
maxConcurrency: 5,
// Cache directory
cacheDirectory: '.jest-cache',
// Run tests in band for debugging
// runInBand: false
};javascript
// jest.config.js
module.exports = {
maxWorkers: '50%', // Use 50% of available CPU cores
// or specify a number
// maxWorkers: 4,
// Parallelize tests within a file
maxConcurrency: 5,
// Cache directory
cacheDirectory: '.jest-cache',
// Run tests in band for debugging
// runInBand: false
};Selective Test Execution
选择性执行测试
javascript
// Run only tests that changed
// package.json
{
"scripts": {
"test:changed": "jest --onlyChanged",
"test:related": "jest --findRelatedTests src/modified-file.js"
}
}javascript
// Run only tests that changed
// package.json
{
"scripts": {
"test:changed": "jest --onlyChanged",
"test:related": "jest --findRelatedTests src/modified-file.js"
}
}Test Sharding for CI
CI 中的测试分片
bash
undefinedbash
undefinedSplit tests across multiple CI machines
Split tests across multiple CI machines
jest --shard=1/3 # Run first third
jest --shard=2/3 # Run second third
jest --shard=3/3 # Run last third
undefinedjest --shard=1/3 # Run first third
jest --shard=2/3 # Run second third
jest --shard=3/3 # Run last third
undefinedBail on First Failure
首次失败即终止
javascript
// jest.config.js
module.exports = {
bail: 1, // Stop after first failure
// bail: true, // Stop after any failure
};javascript
// jest.config.js
module.exports = {
bail: 1, // Stop after first failure
// bail: true, // Stop after any failure
};Advanced Mocking
高级 Mock
Mock Implementations with Different Return Values
使用不同返回值的 Mock 实现
javascript
describe('Complex mocking', () => {
it('should return different values on consecutive calls', () => {
const mockFn = jest
.fn()
.mockReturnValueOnce(1)
.mockReturnValueOnce(2)
.mockReturnValue(3);
expect(mockFn()).toBe(1);
expect(mockFn()).toBe(2);
expect(mockFn()).toBe(3);
expect(mockFn()).toBe(3);
});
it('should implement complex logic', () => {
const mockFn = jest.fn((x) => {
if (x < 0) return 'negative';
if (x === 0) return 'zero';
return 'positive';
});
expect(mockFn(-5)).toBe('negative');
expect(mockFn(0)).toBe('zero');
expect(mockFn(5)).toBe('positive');
});
});javascript
describe('Complex mocking', () => {
it('should return different values on consecutive calls', () => {
const mockFn = jest
.fn()
.mockReturnValueOnce(1)
.mockReturnValueOnce(2)
.mockReturnValue(3);
expect(mockFn()).toBe(1);
expect(mockFn()).toBe(2);
expect(mockFn()).toBe(3);
expect(mockFn()).toBe(3);
});
it('should implement complex logic', () => {
const mockFn = jest.fn((x) => {
if (x < 0) return 'negative';
if (x === 0) return 'zero';
return 'positive';
});
expect(mockFn(-5)).toBe('negative');
expect(mockFn(0)).toBe('zero');
expect(mockFn(5)).toBe('positive');
});
});Mock Module Factories
Mock 模块工厂
javascript
// dynamic-mock.test.js
jest.mock('./api', () => {
const actual = jest.requireActual('./api');
return {
...actual,
fetchUser: jest.fn(),
// Use a factory for dynamic values
getDefaultUser: jest.fn(() => ({
id: Math.random(),
name: 'Test User'
}))
};
});
describe('Dynamic mocks', () => {
it('should generate different default users', () => {
const user1 = getDefaultUser();
const user2 = getDefaultUser();
expect(user1.id).not.toBe(user2.id);
});
});javascript
// dynamic-mock.test.js
jest.mock('./api', () => {
const actual = jest.requireActual('./api');
return {
...actual,
fetchUser: jest.fn(),
// Use a factory for dynamic values
getDefaultUser: jest.fn(() => ({
id: Math.random(),
name: 'Test User'
}))
};
});
describe('Dynamic mocks', () => {
it('should generate different default users', () => {
const user1 = getDefaultUser();
const user2 = getDefaultUser();
expect(user1.id).not.toBe(user2.id);
});
});Mocking ES6 Classes
Mock ES6 类
javascript
// Database.js
export class Database {
constructor(config) {
this.config = config;
}
async connect() {
// Implementation
}
async query(sql) {
// Implementation
}
}
// Database.test.js
import { Database } from './Database';
jest.mock('./Database');
describe('Database mock', () => {
beforeEach(() => {
Database.mockClear();
});
it('should mock class constructor', () => {
const mockConnect = jest.fn();
const mockQuery = jest.fn().mockResolvedValue([{ id: 1 }]);
Database.mockImplementation(() => ({
connect: mockConnect,
query: mockQuery
}));
const db = new Database({ host: 'localhost' });
expect(Database).toHaveBeenCalledWith({ host: 'localhost' });
db.connect();
expect(mockConnect).toHaveBeenCalled();
});
});javascript
// Database.js
export class Database {
constructor(config) {
this.config = config;
}
async connect() {
// Implementation
}
async query(sql) {
// Implementation
}
}
// Database.test.js
import { Database } from './Database';
jest.mock('./Database');
describe('Database mock', () => {
beforeEach(() => {
Database.mockClear();
});
it('should mock class constructor', () => {
const mockConnect = jest.fn();
const mockQuery = jest.fn().mockResolvedValue([{ id: 1 }]);
Database.mockImplementation(() => ({
connect: mockConnect,
query: mockQuery
}));
const db = new Database({ host: 'localhost' });
expect(Database).toHaveBeenCalledWith({ host: 'localhost' });
db.connect();
expect(mockConnect).toHaveBeenCalled();
});
});Advanced Assertions
高级断言
Asymmetric Matchers
非对称匹配器
javascript
describe('Asymmetric matchers', () => {
it('should match part of an object', () => {
const user = {
id: 1,
name: 'John',
email: 'john@example.com',
createdAt: new Date()
};
expect(user).toEqual({
id: expect.any(Number),
name: 'John',
email: expect.stringContaining('@'),
createdAt: expect.any(Date)
});
});
it('should match array containing', () => {
const arr = [1, 2, 3, 4, 5];
expect(arr).toEqual(expect.arrayContaining([2, 4]));
});
it('should match object containing', () => {
const obj = { a: 1, b: 2, c: 3 };
expect(obj).toEqual(expect.objectContaining({ a: 1, c: 3 }));
});
it('should use custom matchers', () => {
expect({ a: 1, b: 2 }).toEqual({
a: expect.any(Number),
b: expect.any(Number)
});
});
});javascript
describe('Asymmetric matchers', () => {
it('should match part of an object', () => {
const user = {
id: 1,
name: 'John',
email: 'john@example.com',
createdAt: new Date()
};
expect(user).toEqual({
id: expect.any(Number),
name: 'John',
email: expect.stringContaining('@'),
createdAt: expect.any(Date)
});
});
it('should match array containing', () => {
const arr = [1, 2, 3, 4, 5];
expect(arr).toEqual(expect.arrayContaining([2, 4]));
});
it('should match object containing', () => {
const obj = { a: 1, b: 2, c: 3 };
expect(obj).toEqual(expect.objectContaining({ a: 1, c: 3 }));
});
it('should use custom matchers', () => {
expect({ a: 1, b: 2 }).toEqual({
a: expect.any(Number),
b: expect.any(Number)
});
});
});Complex Assertions
复杂断言
javascript
describe('Complex assertions', () => {
it('should verify complex data structures', () => {
const response = {
status: 200,
data: {
users: [
{ id: 1, name: 'John', roles: ['admin'] },
{ id: 2, name: 'Jane', roles: ['user'] }
],
meta: {
total: 2,
page: 1
}
}
};
expect(response).toMatchObject({
status: 200,
data: {
users: expect.arrayContaining([
expect.objectContaining({
name: 'John',
roles: expect.arrayContaining(['admin'])
})
]),
meta: {
total: expect.any(Number)
}
}
});
});
});javascript
describe('Complex assertions', () => {
it('should verify complex data structures', () => {
const response = {
status: 200,
data: {
users: [
{ id: 1, name: 'John', roles: ['admin'] },
{ id: 2, name: 'Jane', roles: ['user'] }
],
meta: {
total: 2,
page: 1
}
}
};
expect(response).toMatchObject({
status: 200,
data: {
users: expect.arrayContaining([
expect.objectContaining({
name: 'John',
roles: expect.arrayContaining(['admin'])
})
]),
meta: {
total: expect.any(Number)
}
}
});
});
});Test Isolation and Cleanup
测试隔离与清理
Resetting Module Registry
重置模块注册表
javascript
describe('Module isolation', () => {
beforeEach(() => {
jest.resetModules();
});
it('should load fresh module instance', () => {
const module1 = require('./counter');
module1.increment();
expect(module1.getCount()).toBe(1);
jest.resetModules();
const module2 = require('./counter');
expect(module2.getCount()).toBe(0);
});
});javascript
describe('Module isolation', () => {
beforeEach(() => {
jest.resetModules();
});
it('should load fresh module instance', () => {
const module1 = require('./counter');
module1.increment();
expect(module1.getCount()).toBe(1);
jest.resetModules();
const module2 = require('./counter');
expect(module2.getCount()).toBe(0);
});
});Clearing vs Resetting vs Restoring Mocks
clear、reset 与 restore Mock 的区别
javascript
describe('Mock cleanup', () => {
const mockFn = jest.fn();
beforeEach(() => {
mockFn.mockReturnValue(42);
});
afterEach(() => {
// jest.clearAllMocks(); // Clears call history
// jest.resetAllMocks(); // Clears call history and implementations
// jest.restoreAllMocks(); // Restores original implementations
});
it('should understand mock cleanup', () => {
mockFn();
expect(mockFn).toHaveBeenCalledTimes(1);
// clearAllMocks: Removes call history but keeps implementation
jest.clearAllMocks();
expect(mockFn).toHaveBeenCalledTimes(0);
expect(mockFn()).toBe(42); // Implementation still works
// resetAllMocks: Removes call history and implementation
jest.resetAllMocks();
expect(mockFn()).toBeUndefined(); // No implementation
// restoreAllMocks: Restores original (for spies)
const obj = { method: () => 'original' };
const spy = jest.spyOn(obj, 'method');
spy.mockReturnValue('mocked');
expect(obj.method()).toBe('mocked');
jest.restoreAllMocks();
expect(obj.method()).toBe('original');
});
});javascript
describe('Mock cleanup', () => {
const mockFn = jest.fn();
beforeEach(() => {
mockFn.mockReturnValue(42);
});
afterEach(() => {
// jest.clearAllMocks(); // Clears call history
// jest.resetAllMocks(); // Clears call history and implementations
// jest.restoreAllMocks(); // Restores original implementations
});
it('should understand mock cleanup', () => {
mockFn();
expect(mockFn).toHaveBeenCalledTimes(1);
// clearAllMocks: Removes call history but keeps implementation
jest.clearAllMocks();
expect(mockFn).toHaveBeenCalledTimes(0);
expect(mockFn()).toBe(42); // Implementation still works
// resetAllMocks: Removes call history and implementation
jest.resetAllMocks();
expect(mockFn()).toBeUndefined(); // No implementation
// restoreAllMocks: Restores original (for spies)
const obj = { method: () => 'original' };
const spy = jest.spyOn(obj, 'method');
spy.mockReturnValue('mocked');
expect(obj.method()).toBe('mocked');
jest.restoreAllMocks();
expect(obj.method()).toBe('original');
});
});Testing Strategies
测试策略
Contract Testing
契约测试
javascript
// Define contract
const userContract = {
id: expect.any(Number),
name: expect.any(String),
email: expect.stringMatching(/^[^\s@]+@[^\s@]+\.[^\s@]+$/),
roles: expect.arrayContaining([expect.any(String)]),
createdAt: expect.any(String)
};
describe('User API contract', () => {
it('should match user contract', async () => {
const user = await fetchUser(1);
expect(user).toMatchObject(userContract);
});
it('should match users list contract', async () => {
const users = await fetchUsers();
expect(users).toEqual(
expect.arrayContaining([
expect.objectContaining(userContract)
])
);
});
});javascript
// Define contract
const userContract = {
id: expect.any(Number),
name: expect.any(String),
email: expect.stringMatching(/^[^\s@]+@[^\s@]+\.[^\s@]+$/),
roles: expect.arrayContaining([expect.any(String)]),
createdAt: expect.any(String)
};
describe('User API contract', () => {
it('should match user contract', async () => {
const user = await fetchUser(1);
expect(user).toMatchObject(userContract);
});
it('should match users list contract', async () => {
const users = await fetchUsers();
expect(users).toEqual(
expect.arrayContaining([
expect.objectContaining(userContract)
])
);
});
});Data-Driven Testing
数据驱动测试
javascript
const testCases = require('./test-data.json');
describe.each(testCases)('Data-driven tests', (testCase) => {
it(`should handle ${testCase.description}`, () => {
const result = processData(testCase.input);
expect(result).toEqual(testCase.expected);
});
});javascript
const testCases = require('./test-data.json');
describe.each(testCases)('Data-driven tests', (testCase) => {
it(`should handle ${testCase.description}`, () => {
const result = processData(testCase.input);
expect(result).toEqual(testCase.expected);
});
});Best Practices
最佳实践
- Use custom matchers for domain-specific assertions - Create reusable matchers for common validation patterns
- Leverage test.each for parameterized tests - Reduce duplication and improve test coverage
- Configure coverage thresholds per directory - Set stricter requirements for critical code paths
- Optimize test execution with workers - Use parallel execution for faster test runs
- Implement proper mock cleanup - Understand the difference between clear, reset, and restore
- Use asymmetric matchers for flexible assertions - Match partial objects and dynamic data
- Create custom reporters for CI integration - Send coverage data to external services
- Isolate tests with module resets - Prevent test pollution from shared module state
- Use bail for fast feedback - Stop on first failure during development
- Implement contract testing - Ensure API responses match expected shapes
- 为领域特定断言使用自定义匹配器 —— 为常见的校验模式创建可复用的匹配器
- 利用 test.each 进行参数化测试 —— 减少重复代码并提升测试覆盖率
- 按目录配置覆盖率阈值 —— 对关键代码路径设置更严格的要求
- 使用 workers 优化测试执行 —— 通过并行执行加快测试运行速度
- 正确清理 Mock —— 理解 clear、reset 与 restore 之间的区别
- 使用非对称匹配器进行灵活断言 —— 匹配部分对象和动态数据
- 创建自定义报告器以集成 CI —— 将覆盖率数据发送到外部服务
- 使用模块重置实现测试隔离 —— 防止共享模块状态造成测试污染
- 使用 bail 快速获得反馈 —— 开发过程中在首次失败时即停止
- 实施契约测试 —— 确保 API 响应符合预期结构
Common Pitfalls
常见误区
- Not cleaning up mocks properly - Using wrong cleanup method leads to test pollution
- Over-parameterizing tests - Too many test.each cases reduce readability
- Setting unrealistic coverage thresholds - 100% coverage requirements slow development
- Not using maxWorkers appropriately - Too many workers can overwhelm CI systems
- Forgetting to reset modules - Shared state between tests causes flaky failures
- Creating overly complex custom matchers - Keep matchers simple and focused
- Not typing custom matchers in TypeScript - Missing types lose IDE support
- Misusing asymmetric matchers - Overly permissive matchers miss bugs
- Ignoring coverage reports - Not acting on coverage gaps reduces test value
- Running full suite on every change - Use --onlyChanged for faster feedback
- 未正确清理 Mock —— 使用错误的清理方式会导致测试污染
- 过度参数化测试 —— test.each 用例过多会降低可读性
- 设置不切实际的覆盖率阈值 —— 100% 覆盖率要求会拖慢开发进度
- 未合理使用 maxWorkers —— worker 过多可能压垮 CI 系统
- 忘记重置模块 —— 测试间的共享状态会导致测试不稳定、偶发失败
- 创建过于复杂的自定义匹配器 —— 保持匹配器简单且职责单一
- 未在 TypeScript 中为自定义匹配器添加类型 —— 缺失类型会失去 IDE 支持
- 误用非对称匹配器 —— 过于宽松的匹配器会遗漏缺陷
- 忽视覆盖率报告 —— 不处理覆盖率缺口会降低测试的价值
- 每次改动都运行完整测试套件 —— 使用 --onlyChanged 获得更快反馈
When to Use This Skill
何时使用本 Skill
- Creating custom matchers for domain-specific validation
- Implementing parameterized tests with many test cases
- Optimizing test suite performance for large projects
- Setting up coverage requirements for CI/CD
- Implementing advanced mocking strategies
- Testing complex data structures with asymmetric matchers
- Creating custom reporters for external integrations
- Debugging test isolation issues
- Implementing contract testing patterns
- Improving test maintainability and reducing duplication
- 为领域特定的校验创建自定义匹配器
- 实施包含大量测试用例的参数化测试
- 为大型项目优化测试套件性能
- 为 CI/CD 设置覆盖率要求
- 实施高级 Mock 策略
- 使用非对称匹配器测试复杂数据结构
- 创建用于外部集成的自定义报告器
- 排查测试隔离相关问题
- 实施契约测试模式
- 提升测试可维护性并减少重复代码