仕事でNode.js+Jest
を使った時に、調べた内容のメモです。
前提
環境構築
フォルダ構成
JestSample ├app/ │ ├sum.js │ └__tests__/ │ └sum.test.js ├node_modules │ └ (省略) ├package.json └package-lock.json
レベル0:リファレンスサンプル(正の整数の計算を追加)
- Jestのリファレンスにサンプルとして掲載されているテストです。
- 公式リファレンス(はじめましょう)
test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3); });
レベル1意向で上記、テストをベースに単体テストの拡充を実施していきます。
レベル1:正常系テストの追加
- 「正常系テストの追加」を通して、テストパターンを単純に追加できるようになる
レベル1-1:負の整数の計算を追加
test('adds -1 + -2 to equal -3', () => { expect(sum(-1, -2)).toBe(-3); });
レベル1-2:小数の計算を追加
test('adds 1.2 + 3.4 to equal 4.6', () => { expect(sum(1.2, 3.4)).toBe(4.6); });
レベル1-3:境界値のテストを追加
test('adds Number.MAX_SAFE_INTEGER + 0 to equal Number.MAX_SAFE_INTEGER', () => { expect(sum(Number.MAX_SAFE_INTEGER, 0)).toBe(Number.MAX_SAFE_INTEGER); }); test('adds Number.MAX_SAFE_INTEGER-1 + 1 to equal Number.MAX_SAFE_INTEGER', () => { expect(sum(Number.MAX_SAFE_INTEGER-1, 1)).toBe(Number.MAX_SAFE_INTEGER); }); test('adds Number.MIN_SAFE_INTEGER + 0 to equal Number.MIN_SAFE_INTEGER', () => { expect(sum(Number.MIN_SAFE_INTEGER, 0)).toBe(Number.MIN_SAFE_INTEGER); }); test('adds Number.MIN_SAFE_INTEGER+1 + -1 to equal Number.MIN_SAFE_INTEGER', () => { expect(sum(Number.MIN_SAFE_INTEGER+1, -1)).toBe(Number.MIN_SAFE_INTEGER); });
レベル1-4:特異値(0)のテストを追加
レベル1-4-1:_+0
test('adds 1 + 0 to equal 1', () => { expect(sum(1, 0)).toBe(1); });
レベル1-4-2:0+_
test('adds 0 + 1 to equal 1', () => { expect(sum(0, 1)).toBe(1); });
レベル1-4-3:結果が0
test('adds 1 + (-1) to equal 0', () => { expect(sum(1, -1)).toBe(0); }); test('adds (-1) + 1 to equal 0', () => { expect(sum(-1, 1)).toBe(0); });
レベル1-4-4:0+0
test('adds 0 + 0 to equal 0', () => { expect(sum(0, 0)).toBe(0); });
レベル1-5:特異値(0.0)のテストを追加
レベル1-4-1:_+0.0
test('adds 1 + 0.0 to equal 1', () => { expect(sum(1, 0.0)).toBe(1); });
レベル1-4-2:0.0+_
test('adds 0.0 + 1 to equal 1', () => { expect(sum(0.0, 1)).toBe(1); });
レベル1-4-3:結果が0.0
test('adds 1 + (-1) to equal 0', () => { expect(sum(1, -1)).toBe(0); }); test('adds (-1) + 1 to equal 0', () => { expect(sum(-1, 1)).toBe(0); });
レベル1-4-4:0.0+0.0
test('adds 0 + 0 to equal 0', () => { expect(sum(0.0, 0.0)).toBe(0.0); });
レベル1(ボーナス):describe(name, fn)
describe(name, fn)
で関連するテストをまとめたブロックを作成することができます。- 公式リファレンス(describe)
const sum = require('../sum'); // レベル0:リファレンスサンプル describe('Level 0:リファレンスサンプル', () => { describe('Level 0:正の整数', () => { test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3); }); }); }); // レベル1:正常系テスト describe('Level 1:正常系テスト', () => { // 1-1 describe('Level 1-1:負の整数', () => { test('adds -1 + -2 to equal -3', () => { expect(sum(-1, -2)).toBe(-3); }); }); // 1-2 describe('Level 1-2:小数', () => { test('adds 1.2 + 3.4 to equal 4.6', () => { expect(sum(1.2, 3.4)).toBe(4.6); }); }); // 1-3 describe('Level 1-3:境界値', () => { test('adds Number.MAX_SAFE_INTEGER + 0 to equal Number.MAX_SAFE_INTEGER', () => { expect(sum(Number.MAX_SAFE_INTEGER, 0)).toBe(Number.MAX_SAFE_INTEGER); }); test('adds Number.MAX_SAFE_INTEGER-1 + 1 to equal Number.MAX_SAFE_INTEGER', () => { expect(sum(Number.MAX_SAFE_INTEGER-1, 1)).toBe(Number.MAX_SAFE_INTEGER); }); test('adds Number.MIN_SAFE_INTEGER + 0 to equal Number.MIN_SAFE_INTEGER', () => { expect(sum(Number.MIN_SAFE_INTEGER, 0)).toBe(Number.MIN_SAFE_INTEGER); }); test('adds Number.MIN_SAFE_INTEGER+1 + -1 to equal Number.MIN_SAFE_INTEGER', () => { expect(sum(Number.MIN_SAFE_INTEGER+1, -1)).toBe(Number.MIN_SAFE_INTEGER); }); }); // 1-4 describe('Level 1-4:特異値(0)', () => { describe('Level 1-4-1:_ + 0', () => { test('adds 1 + 0 to equal 1', () => { expect(sum(1, 0)).toBe(1); }); }); describe('Level 1-4-2:0 + _', () => { test('adds 0 + 1 to equal 1', () => { expect(sum(0, 1)).toBe(1); }); }); describe('Level 1-4-3:結果が0', () => { test('adds 1 + (-1) to equal 0', () => { expect(sum(1, -1)).toBe(0); }); test('adds (-1) + 1 to equal 0', () => { expect(sum(-1, 1)).toBe(0); }); }); describe('Level 1-4-4:0 + 0', () => { test('adds 0 + 0 to equal 0', () => { expect(sum(0, 0)).toBe(0); }); }); }); // 1-5 describe('Level 1-5:特異値(0.0)', () => { describe('Level 1-5-1:_ + 0.0', () => { test('adds 1 + 0.0 to equal 1', () => { expect(sum(1, 0.0)).toBe(1); }); }); describe('Level 1-5-2:0.0 + _', () => { test('adds 0.0 + 1 to equal 1', () => { expect(sum(0.0, 1)).toBe(1); }); }); describe('Level 1-5-3:結果が0', () => { test('adds 1 + (-1) to equal 0', () => { expect(sum(1, -1)).toBe(0); }); test('adds (-1) + 1 to equal 0', () => { expect(sum(-1, 1)).toBe(0); }); }); describe('Level 1-5-4:0.0 + 0.0', () => { test('adds 0.0 + 0.0 to equal 0', () => { expect(sum(0.0, 0.0)).toBe(0.0); }); }); }); });