0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TypeScriptのプロジェクトにJestをインストールする

Posted at

はじめに

今回はnpmを利用したインストール方法をまとめてみます

インストール

npm install --save-dev jest @types/jest ts-jest

※プロジェクト直下にjest.config.jsが作成されていることを確認

npm testコマンドを定義

package.json
  "scripts": {
    "test": "jest"
  }

これでnpm testでテストを実行できるようになります

テスト

__test__ディレクトリを作成して適当なテストファイルを配置します

sum.test.tsx
import {expect, test} from '@jest/globals'

function sum(a: number, b: number) {
  return a + b
}
test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3)
})

実行

npm test

 PASS  src/__test__/sum.test.tsx
  ✓ adds 1 + 2 to equal 3 (1 ms)

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

無事テストできてそうです

おわりに

いかがだったでしょうか
今回はJestの導入方法をまとめてみました

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?