単体テストを足す
単体テストの設計 の表の残りのケースを足し、10 件をそろえます。
| 関数 | 前の節で書いたもの | この節で足すもの |
|---|---|---|
split |
S1 | S2(端数)・S3(0 円)・S4(1 人) |
balances |
B1 | B2(払った人が対象でない)・B3(出てこない人) |
settle |
なし | T1(ふつう)・T2(組み合わせ)・T3(差し引き 0) |
テストを足す
Section titled “テストを足す”apps/api/src/settlement/settle.spec.ts を変更する
Section titled “apps/api/src/settlement/settle.spec.ts を変更する”11import { describe, expect, it } from 'vitest';2import { balances, split } from './settle.js';2import { balances, settle, split } from './settle.js';33
44describe('split', () => {55 // S166 it('割り切れるときは、同じ額に分ける', () => {77 expect(split(9000, 3)).toEqual([3000, 3000, 3000]);88 });9
10 // S211 it('割り切れないときは、前の人から 1 円ずつ足し、合計を元の金額と同じにする', () => {12 const shares = split(10000, 3);13
14 expect(shares).toEqual([3334, 3333, 3333]);15 expect(shares.reduce((a, b) => a + b)).toBe(10000);16 });17
18 // S319 it('0 円は、全員 0 円', () => {20 expect(split(0, 3)).toEqual([0, 0, 0]);21 });22
23 // S424 it('1 人なら、その人が全部', () => {25 expect(split(500, 1)).toEqual([500]);26 });927});1028
1129describe('balances', () => {2139 { memberId: 3, amount: -3000 },2240 ]);2341 });42
43 // B244 it('払った人が対象でなければ、払った額がそのままプラスになる', () => {45 const result = balances([46 { payerId: 1, amount: 6000, participantIds: [2, 3] },47 ]);48
49 expect(result).toEqual([50 { memberId: 1, amount: 6000 },51 { memberId: 2, amount: -3000 },52 { memberId: 3, amount: -3000 },53 ]);54 });55
56 // B357 it('どの立て替えにも出てこない人は、入らない', () => {58 const result = balances([59 { payerId: 1, amount: 1000, participantIds: [1, 2] },60 ]);61
62 expect(result.map((b) => b.memberId)).toEqual([1, 2]);63 });2464});65
66describe('settle', () => {67 // T168 it('受け取る人 1 人に、ほかの人が払う', () => {69 const result = settle([70 { memberId: 1, amount: 6000 },71 { memberId: 2, amount: -3000 },72 { memberId: 3, amount: -3000 },73 ]);74
75 expect(result).toEqual([76 { from: 2, to: 1, amount: 3000 },77 { from: 3, to: 1, amount: 3000 },78 ]);79 });80
81 // T282 it('多く払う人から、多く受け取る人へ払い、回数を少なくする', () => {83 const result = settle([84 { memberId: 1, amount: 5000 },85 { memberId: 2, amount: 1000 },86 { memberId: 3, amount: -6000 },87 ]);88
89 expect(result).toEqual([90 { from: 3, to: 1, amount: 5000 },91 { from: 3, to: 2, amount: 1000 },92 ]);93 });94
95 // T396 it('差し引きが 0 の人しかいなければ、払う人はいない', () => {97 expect(settle([{ memberId: 1, amount: 0 }])).toEqual([]);98 });99});- S2 は、配列が期待どおりかに加えて、合計が元の金額と同じになることも
toBe(10000)で確かめます。toBeは、数のような 1 つの値を比べます - B3 は、結果の
memberIdだけを取り出して比べます。確かめたいこと(出てこない人が入らないこと)だけを比べると、テストが落ちたときに何が破れたかが分かります
npm test ✓ src/settlement/settle.spec.ts (10 tests) 2ms
Test Files 1 passed (1) Tests 10 passed (10)試しに、src/settlement/settle.ts の split() の i < rest ? base + 1 : base を base に変えて npm test を動かすと、S2 が落ちます。余りの 1 円を付ける決まりが破れたことが、テストの名前で分かります(確かめたら戻します)。
FAIL src/settlement/settle.spec.ts > split > 割り切れないときは、前の人から 1 円ずつ足し、合計を元の金額と同じにするAssertionError: expected [ 3333, 3333, 3333 ] to deeply equal [ 3334, 3333, 3333 ]参考にした公式ドキュメント
Section titled “参考にした公式ドキュメント”この節は、次の公式ドキュメントをもとに書いています。
- Vitest: expect …
toEqual・toBe