こんにちは、CX事業本部 IoT事業部の若槻です。
今回は、Jest CLIで、Gitで未コミットのテストファイルのみをテスト実行対象にする方法が便利だったので紹介します。
方法
o
オプションを使用してjestコマンドを実行するだけです。
jest -o
現在実装を行っているテストファイルのみ動作確認したい場合にとても便利です。
やってみる
oオプションを使ってみる
未コミットのテストファイルfancA.test.ts
とfancB.test.ts
があります。
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
test/fancA.test.ts
test/fancB.test.ts
nothing added to commit but untracked files present (use "git add" to track)
o
オプションを付けてテストをjestコマンドを実行すると、いずれのテストファイルも対象になります。
$ npx jest -o
PASS test/fancA.test.ts
PASS test/fancB.test.ts
Test Suites: 2 passed, 2 total
Tests: 4 passed, 4 total
Snapshots: 0 total
Time: 2.665 s, estimated 3 s
Ran all test suites related to changed files.
一方のテストファイルfancA.test.ts
をGit Commitします。
git add test/fancA.test.ts && git commit -m "impl fancA"
再度o
オプションを付けてテストをjestコマンドを実行すると、今度は未コミットのfancB.test.ts
のみ対象となりました。
$ npx jest -o
PASS test/fancB.test.ts
関数Bが実行された時
パラメータhogeが指定された時
✓ 実行が成功すること (1 ms)
パラメータfugaが指定された時
✓ 実行が成功すること
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.85 s, estimated 3 s
Ran all test suites related to changed files.
tオプションと組み合わせて使う
特定のtestやdescribeのテストケースのみを実行対象とできるt
オプションと組み合わせて使用することもできます。
fancA.test.ts
とfancB.test.ts
の内容はそれぞれ次のようになっています。
test/fancA.test.ts
describe('関数Aが実行された時', () => {
describe('パラメータhogeが指定された時', () => {
test('実行が成功すること', () => {
expect(1 + 1).toBe(2);
});
});
describe('パラメータfugaが指定された時', () => {
test('実行が成功すること', () => {
expect(0 * 1).toBe(0);
});
});
});
test/fancB.test.ts
describe('関数Bが実行された時', () => {
describe('パラメータhogeが指定された時', () => {
test('実行が成功すること', () => {
expect(1 + 1).toBe(2);
});
test('ログが出力されること', () => {
expect(1 - 1).toBe(0);
});
});
describe('パラメータfugaが指定された時', () => {
test('実行が成功すること', () => {
expect(0 * 1).toBe(0);
});
});
});
現在はfancB.test.ts
のみが未コミットの状態です。
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
test/fancB.test.ts
nothing added to commit but untracked files present (use "git add" to track)
この時、t
オプションを次のように指定して実行すると、指定したdescribeのテストケースのみを実行できます。
$ npx jest -o -t 'パラメータhogeが指定された時'
PASS test/fancB.test.ts
関数Bが実行された時
パラメータhogeが指定された時
✓ 実行が成功すること (1 ms)
✓ ログが出力されること
パラメータfugaが指定された時
○ skipped 実行が成功すること
Test Suites: 1 passed, 1 total
Tests: 1 skipped, 2 passed, 3 total
Snapshots: 0 total
Time: 2.295 s, estimated 3 s
Ran all test suites related to changed files with tests matching "パラメータhogeが指定された時".
また、t
オプションで<describe> <test>
とすれば、指定したdescribeおよびtestのテストケースのみを実行することもできます。
$ npx jest -o -t 'パラメータhogeが指定された時 実行が成功すること'
PASS test/fancB.test.ts
関数Bが実行された時
パラメータhogeが指定された時
✓ 実行が成功すること
○ skipped ログが出力されること
パラメータfugaが指定された時
○ skipped 実行が成功すること
Test Suites: 1 passed, 1 total
Tests: 2 skipped, 1 passed, 3 total
Snapshots: 0 total
Time: 2.126 s, estimated 3 s
Ran all test suites related to changed files with tests matching "パラメータhogeが指定された時 実行が成功すること".
ファイル単位よりもさらにきめ細かくテストケースを指定できるのは嬉しいですね。
おわりに
Jest CLIで、Gitで未コミットのテストファイルのみをテスト実行対象にする方法が便利だったのでご紹介しました。
Node.jsを使用した開発でJestはよく使用するので、これを早く知りたかったです。急がば回れ、まずは公式ドキュメントを熟読しろ、ですね。
参考
以上