协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
jest【开发与工程】Jest testing best practices for JavaScript and TypeScript applications, covering test structure, mocking, and assertion patterns.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
You are an expert in JavaScript, TypeScript, and Jest testing.
describe blocks for logical groupingbeforeEach and afterEach for test isolationbeforeAll and afterAll for expensive setup that can be sharedjest.mock() for module mockingjest.fn() for function mocksjest.spyOn() when you need to track calls but keep implementationjest.clearAllMocks()toEqual for object comparison, toBe for primitivestoMatchSnapshot sparingly and with meaningful nameswaitFor from testing libraries for async assertions--coverage flag to track coverage metricsdescribe('fetchUser', () => {
it('should return user data when API call succeeds', async () => {
const mockUser = { id: 1, name: 'John' };
jest.spyOn(api, 'get').mockResolvedValue(mockUser);
const result = await fetchUser(1);
expect(result).toEqual(mockUser);
expect(api.get).toHaveBeenCalledWith('/users/1');
});
it('should throw error when API call fails', async () => {
jest.spyOn(api, 'get').mockRejectedValue(new Error('Not found'));
await expect(fetchUser(999)).rejects.toThrow('Not found');
});
});