Best practices for writing JavaScript and TypeScript tests with Jest. Use when asked to write unit tests, improve test coverage, set up mocking, test async code, or test React components with Testing Library. Covers test structure, mock strategies, snapshot testing, and common Jest matchers.
.test.ts or .test.js suffix__tests__ directorydescribe('Component/Function/Class', () => { it('should do something', () => {}) })jest.mock() for module-level mocksjest.spyOn() for specific function mocksmockImplementation() or mockReturnValue() to define mock behaviorjest.resetAllMocks() in afterEachresolves/rejects matchers for promisesjest.setTimeout()userEvent over fireEvent for more realistic user interactions