7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

この記事は、TypeScriptでJestを使うための環境構築手順の備忘録です。Jestに関する公式ドキュメントはこちらです。

動作環境

nodeのバージョン

node -v        
v18.12.1

環境設定

最初に新しいプロジェクトを作成します。

mkdir jest-lesson
cd jest-lesson

次に、npmパッケージマネージャーを使ってプロジェクトを初期化します。
このコマンドはpackage.jsonファイルを生成します。

npm init -y

次に、TypeScriptをプロジェクトに追加します。

npm i -D typescript

そして、TypeScriptの設定ファイルtsconfig.jsonを生成します。

npx tsc --init

次に、Jestと関連パッケージをインストールします。

npm i -D jest @types/jest ts-jest

Jestの設定ファイルを追加または更新します。

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

Jestの動作確認

簡単な関数sumを作成します。

src/sum.ts
export function sum(a: number, b: number): number {
  return a + b;
}

この関数のテストを書きます。

src/sum.test.ts
import { sum } from "./sum";

it("1と2を足すと3になる", () => {
  expect(sum(1, 2)).toBe(3);
});

テストを実行してみましょう。

npm test

動作すれば設定完了です。
フォルダ構成は次のようになります。

.
├── README.md
├── jest.config.js
├── node_modules
├── package-lock.json
├── package.json
├── src
└── tsconfig.json

おまけ

giboを使って.gitignoreファイルを作成します。

gibo dump Node VisualStudioCode macOS >> .gitignore
7
7
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
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?