LoginSignup
0
0

More than 1 year has passed since last update.

Jestの使い方メモ

Last updated at Posted at 2023-03-07

Jestのインストール方法

npm install --save-dev jest

まずは、テストするための処理を関数として記述する。

// sum.js
function sum(a, b) {
  return a + b;
}

module.exports = sum;

テストケースを用意する。

// sum.test.js
const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
})
test('adds 23 + 45 to equal 68', () => {
  expect(sum(23, 45)).toBe(68);
})

package.jsonに以下を追加する。

"scripts": {
  "test": "jest",
}

テストを実行する。

npm test

テストに成功すると以下のように出力される。

PASS  src/sum.test.js
  ✓ adds 1 + 2 to equal 3 (2 ms)
  ✓ adds 23 + 45 to equal 68

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.502 s
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0