$npx -y skills add PramodDutta/qaskills --skill jest-unitUnit testing skill using Jest for TypeScript and JavaScript, covering mocking, spies, snapshots, coverage, async testing, and custom matchers.
| 1 | # Jest Unit Testing Skill |
| 2 | |
| 3 | You are an expert software engineer specializing in unit testing with Jest. When the user asks you to write, review, or debug Jest unit tests, follow these detailed instructions. |
| 4 | |
| 5 | ## Core Principles |
| 6 | |
| 7 | 1. **Test behavior, not implementation** -- Tests should verify what code does, not how it does it. |
| 8 | 2. **One assertion focus per test** -- Each test should verify a single logical concept. |
| 9 | 3. **Arrange-Act-Assert** -- Structure every test into setup, execution, and verification. |
| 10 | 4. **Fast and isolated** -- Unit tests must run in milliseconds and have no external dependencies. |
| 11 | 5. **Descriptive names** -- Test names should read as specifications of the code's behavior. |
| 12 | |
| 13 | ## Project Structure |
| 14 | |
| 15 | ``` |
| 16 | src/ |
| 17 | services/ |
| 18 | user.service.ts |
| 19 | user.service.test.ts |
| 20 | order.service.ts |
| 21 | order.service.test.ts |
| 22 | utils/ |
| 23 | validators.ts |
| 24 | validators.test.ts |
| 25 | formatters.ts |
| 26 | formatters.test.ts |
| 27 | models/ |
| 28 | user.model.ts |
| 29 | __mocks__/ |
| 30 | axios.ts |
| 31 | database.ts |
| 32 | __tests__/ |
| 33 | integration/ |
| 34 | user-order.test.ts |
| 35 | jest.config.ts |
| 36 | ``` |
| 37 | |
| 38 | ## Configuration |
| 39 | |
| 40 | ```typescript |
| 41 | // jest.config.ts |
| 42 | import type { Config } from 'jest'; |
| 43 | |
| 44 | const config: Config = { |
| 45 | preset: 'ts-jest', |
| 46 | testEnvironment: 'node', |
| 47 | roots: ['<rootDir>/src'], |
| 48 | testMatch: ['**/*.test.ts', '**/*.spec.ts'], |
| 49 | collectCoverageFrom: [ |
| 50 | 'src/**/*.ts', |
| 51 | '!src/**/*.d.ts', |
| 52 | '!src/**/*.test.ts', |
| 53 | '!src/**/index.ts', |
| 54 | ], |
| 55 | coverageThresholds: { |
| 56 | global: { |
| 57 | branches: 80, |
| 58 | functions: 80, |
| 59 | lines: 80, |
| 60 | statements: 80, |
| 61 | }, |
| 62 | }, |
| 63 | coverageReporters: ['text', 'lcov', 'json-summary'], |
| 64 | moduleNameMapper: { |
| 65 | '^@/(.*)$': '<rootDir>/src/$1', |
| 66 | }, |
| 67 | setupFilesAfterSetup: ['<rootDir>/jest.setup.ts'], |
| 68 | clearMocks: true, |
| 69 | restoreMocks: true, |
| 70 | }; |
| 71 | |
| 72 | export default config; |
| 73 | ``` |
| 74 | |
| 75 | ## Writing Tests |
| 76 | |
| 77 | ### Basic Test Structure |
| 78 | |
| 79 | ```typescript |
| 80 | // validators.ts |
| 81 | export function isValidEmail(email: string): boolean { |
| 82 | const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 83 | return regex.test(email); |
| 84 | } |
| 85 | |
| 86 | export function isStrongPassword(password: string): boolean { |
| 87 | return ( |
| 88 | password.length >= 8 && |
| 89 | /[A-Z]/.test(password) && |
| 90 | /[a-z]/.test(password) && |
| 91 | /[0-9]/.test(password) && |
| 92 | /[!@#$%^&*]/.test(password) |
| 93 | ); |
| 94 | } |
| 95 | ``` |
| 96 | |
| 97 | ```typescript |
| 98 | // validators.test.ts |
| 99 | import { isValidEmail, isStrongPassword } from './validators'; |
| 100 | |
| 101 | describe('isValidEmail', () => { |
| 102 | it('should return true for valid email addresses', () => { |
| 103 | expect(isValidEmail('user@example.com')).toBe(true); |
| 104 | expect(isValidEmail('first.last@domain.co.uk')).toBe(true); |
| 105 | expect(isValidEmail('user+tag@example.com')).toBe(true); |
| 106 | }); |
| 107 | |
| 108 | it('should return false for invalid email addresses', () => { |
| 109 | expect(isValidEmail('')).toBe(false); |
| 110 | expect(isValidEmail('not-an-email')).toBe(false); |
| 111 | expect(isValidEmail('@missing-local.com')).toBe(false); |
| 112 | expect(isValidEmail('missing-at.com')).toBe(false); |
| 113 | expect(isValidEmail('spaces here@bad.com')).toBe(false); |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | describe('isStrongPassword', () => { |
| 118 | it('should accept a strong password', () => { |
| 119 | expect(isStrongPassword('SecurePass1!')).toBe(true); |
| 120 | }); |
| 121 | |
| 122 | it('should reject passwords shorter than 8 characters', () => { |
| 123 | expect(isStrongPassword('Ab1!')).toBe(false); |
| 124 | }); |
| 125 | |
| 126 | it('should reject passwords without uppercase letters', () => { |
| 127 | expect(isStrongPassword('lowercase1!')).toBe(false); |
| 128 | }); |
| 129 | |
| 130 | it('should reject passwords without lowercase letters', () => { |
| 131 | expect(isStrongPassword('UPPERCASE1!')).toBe(false); |
| 132 | }); |
| 133 | |
| 134 | it('should reject passwords without numbers', () => { |
| 135 | expect(isStrongPassword('NoNumbers!')).toBe(false); |
| 136 | }); |
| 137 | |
| 138 | it('should reject passwords without special characters', () => { |
| 139 | expect(isStrongPassword('NoSpecial1')).toBe(false); |
| 140 | }); |
| 141 | }); |
| 142 | ``` |
| 143 | |
| 144 | ### Testing Classes and Services |
| 145 | |
| 146 | ```typescript |
| 147 | // user.service.ts |
| 148 | import { UserRepository } from './user.repository'; |
| 149 | import { EmailService } from './email.service'; |
| 150 | |
| 151 | export class UserService { |
| 152 | constructor( |
| 153 | private userRepo: UserRepository, |
| 154 | private emailService: EmailService |
| 155 | ) {} |
| 156 | |
| 157 | async createUser(email: string, name: string): Promise<User> { |
| 158 | const existing = await this.userRepo.findByEmail(email); |
| 159 | if (existing) { |
| 160 | throw new Error('User already exists'); |
| 161 | } |
| 162 | |
| 163 | const user = await this.userRepo.create({ email, name }); |
| 164 | await this.emailService.sendWelcomeEmail(user.email, user.name); |
| 165 | return user; |
| 166 | } |
| 167 | |
| 168 | async getUser(id: string): Promise<User | null> { |
| 169 | return this.userRepo.findById(id); |
| 170 | } |
| 171 | |
| 172 | async deleteUser(id: string): Promise<void> { |
| 173 | const user = await this.userRepo.findById(id); |
| 174 | if (!user) { |
| 175 | throw new Error('User not found'); |
| 176 | } |
| 177 | await this.us |