このチュートリアルで学ぶこと
✓ Jestのセットアップ
✓ 基本的なテストの書き方
✓ マッチャー(アサーション)
✓ モックとスパイ
✓ 非同期テスト
✓ カバレッジレポート
Step 1: セットアップ
npm init -y
npm install -D jest @types/jest ts-jest typescript
npx ts-jest config:init
jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
collectCoverageFrom: ['src/**/*.ts'],
};
Step 2: 基本的なテスト
// src/math.ts
export function add(a: number, b: number): number {
return a + b;
}
export function divide(a: number, b: number): number {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
// src/math.test.ts
import { add, divide } from './math';
describe('Math functions', () => {
describe('add', () => {
it('should add two numbers', () => {
expect(add(1, 2)).toBe(3);
});
it('should handle negative numbers', () => {
expect(add(-1, 1)).toBe(0);
});
});
describe('divide', () => {
it('should divide two numbers', () => {
expect(divide(10, 2)).toBe(5);
});
it('should throw error for division by zero', () => {
expect(() => divide(10, 0)).toThrow('Division by zero');
});
});
});
Step 3: マッチャー
// 等値
expect(value).toBe(3); // 厳密等価
expect(obj).toEqual({ a: 1 }); // オブジェクト比較
// 真偽値
expect(value).toBeTruthy();
expect(value).toBeFalsy();
expect(value).toBeNull();
expect(value).toBeDefined();
// 数値
expect(value).toBeGreaterThan(3);
expect(value).toBeLessThanOrEqual(5);
expect(0.1 + 0.2).toBeCloseTo(0.3);
// 文字列
expect(str).toMatch(/pattern/);
expect(str).toContain('substring');
// 配列
expect(arr).toContain(item);
expect(arr).toHaveLength(3);
Step 4: モック
// src/userService.ts
import { db } from './db';
export async function getUser(id: number) {
return db.user.findById(id);
}
// src/userService.test.ts
import { getUser } from './userService';
import { db } from './db';
jest.mock('./db');
describe('UserService', () => {
it('should return user', async () => {
const mockUser = { id: 1, name: 'Test' };
(db.user.findById as jest.Mock).mockResolvedValue(mockUser);
const result = await getUser(1);
expect(result).toEqual(mockUser);
expect(db.user.findById).toHaveBeenCalledWith(1);
});
});
Step 5: 非同期テスト
// async/await
it('should fetch data', async () => {
const data = await fetchData();
expect(data).toBeDefined();
});
// Promises
it('should resolve', () => {
return expect(asyncFunc()).resolves.toBe('value');
});
it('should reject', () => {
return expect(asyncFunc()).rejects.toThrow();
});
Step 6: テスト実行
# テスト実行
npm test
# ウォッチモード
npm test -- --watch
# カバレッジ
npm test -- --coverage
まとめ
Jestは使いやすいテストフレームワークで、モック機能も充実しています。テストを書くことでコードの品質と信頼性が向上します。
← 一覧に戻る