diff --git a/src/basic.md b/src/basic.md index 72f78d2..92d877c 100644 --- a/src/basic.md +++ b/src/basic.md @@ -4,4 +4,17 @@ Jestを使ってどうやってテストするのか?といった疑問に答えます。 テストを行っていくための最初の一歩になればと思います。 + + それでは、さっそく学んでいきましょう! diff --git a/src/jest-api.md b/src/jest-api.md index d5a36ce..1d77336 100644 --- a/src/jest-api.md +++ b/src/jest-api.md @@ -11,6 +11,19 @@ Jestの代表的な機能を紹介します。 - `toBe` … 与えた値との同一性 (`===`) を検証します - `toEqual` … オブジェクトまたは配列のすべてのプロパティの同一性を再帰的に検証します + + ### 真偽値とそれに類する値の検証 - `toBeNull` … null @@ -47,10 +60,55 @@ Jestの代表的な機能を紹介します。 Jestは、`test` に渡す関数の前に `async` キーワードを記述するだけで、非同期テストを実行できます。 + + より詳しい情報は [Jest公式ドキュメント Async/Await](https://jestjs.io/ja/docs/asynchronous#asyncawait) を参照してください。 ## beforeEach と afterEach `beforeEach` と `afterEach` を使用することでテストの実行の前に繰り返し行う準備や、後片付けの処理を宣言できます。 + + より詳しい情報は [Jest公式ドキュメント セットアップと破棄](https://jestjs.io/ja/docs/setup-teardown) を参照してください。 + +## モック + +モック関数を使用することでコード間の繋がりをテストできます。 + + + +より詳しい情報は [Jest公式ドキュメント モック関数](https://jestjs.io/ja/docs/mock-functions) を参照してください。 diff --git a/templates/callback/babel.config.json b/templates/callback/babel.config.json deleted file mode 100644 index fbe6baa..0000000 --- a/templates/callback/babel.config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "presets": ["@babel/preset-env"], - "targets": "current node" -} diff --git a/templates/callback/callback.test.js b/templates/callback/callback.test.js deleted file mode 100644 index 5cbf13a..0000000 --- a/templates/callback/callback.test.js +++ /dev/null @@ -1,26 +0,0 @@ -/** @license https://github.com/facebook/jest/blob/master/LICENSE */ - -// ダミー -const fetchData = (callback) => callback("peanut butter"); - -// 実行しないでください! -test.skip("the data is peanut butter", () => { - function callback(data) { - expect(data).toBe("peanut butter"); - } - - fetchData(callback); -}); - -test("the data is peanut butter", (done) => { - function callback(data) { - try { - expect(data).toBe("peanut butter"); - done(); - } catch (error) { - done(error); - } - } - - fetchData(callback); -}); diff --git a/templates/callback/main.js b/templates/callback/main.js deleted file mode 100644 index 836a14c..0000000 --- a/templates/callback/main.js +++ /dev/null @@ -1 +0,0 @@ -// callback.test.js をご参照ください diff --git a/templates/callback/package.json b/templates/callback/package.json deleted file mode 100644 index 7132044..0000000 --- a/templates/callback/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "callback", - "version": "1.0.0", - "private": true, - "main": "main.js", - "type": "module", - "scripts": { - "test": "jest" - }, - "devDependencies": { - "@babel/preset-env": "^7.16.11", - "jest": "^27.5.1" - } -} diff --git a/templates/callback/sandbox.config.json b/templates/callback/sandbox.config.json deleted file mode 100644 index f3556b8..0000000 --- a/templates/callback/sandbox.config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "template": "parcel" -} diff --git a/templates/setup-teardown/babel.config.json b/templates/setup-teardown/babel.config.json deleted file mode 100644 index fbe6baa..0000000 --- a/templates/setup-teardown/babel.config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "presets": ["@babel/preset-env"], - "targets": "current node" -} diff --git a/templates/setup-teardown/main.js b/templates/setup-teardown/main.js deleted file mode 100644 index 670300e..0000000 --- a/templates/setup-teardown/main.js +++ /dev/null @@ -1 +0,0 @@ -// setupTeardown.test.js をご参照ください diff --git a/templates/setup-teardown/package.json b/templates/setup-teardown/package.json deleted file mode 100644 index 7bf65e5..0000000 --- a/templates/setup-teardown/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "setup-teardown", - "version": "1.0.0", - "private": true, - "main": "main.js", - "type": "module", - "scripts": { - "test": "jest" - }, - "devDependencies": { - "@babel/preset-env": "^7.16.11", - "jest": "^27.5.1" - } -} diff --git a/templates/setup-teardown/sandbox.config.json b/templates/setup-teardown/sandbox.config.json deleted file mode 100644 index f3556b8..0000000 --- a/templates/setup-teardown/sandbox.config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "template": "parcel" -} diff --git a/templates/setup-teardown/setupTeardown.test.js b/templates/setup-teardown/setupTeardown.test.js deleted file mode 100644 index 3ae5f72..0000000 --- a/templates/setup-teardown/setupTeardown.test.js +++ /dev/null @@ -1,60 +0,0 @@ -/** @license https://github.com/facebook/jest/blob/master/LICENSE */ - -// ダミー -const initializeCityDatabase = async () => { - console.log("Cityデータベースの初期化処理"); -}; -const clearCityDatabase = async () => { - console.log("Cityデータベースの消去"); -}; -const isCity = (city) => { - // …なにかデータベースに依存する処理 - - console.log(`${city} は都市です`); - return true; -}; - -beforeEach(async () => { - await initializeCityDatabase(); -}); - -afterEach(async () => { - await clearCityDatabase(); -}); - -beforeAll(async () => { - await initializeCityDatabase(); -}); - -afterAll(async () => { - await clearCityDatabase(); -}); - -// ダミー -const initializeFoodDatabase = async () => { - console.log("Foodデータベースの初期化処理"); -}; -const isValidCityFoodPair = () => true; - -test("city database has Vienna", () => { - expect(isCity("Vienna")).toBeTruthy(); -}); - -test("city database has San Juan", () => { - expect(isCity("San Juan")).toBeTruthy(); -}); - -describe("matching cities to foods", () => { - // このdescribeブロックのテストにのみ適用されます - beforeEach(async () => { - await initializeFoodDatabase(); - }); - - test("Vienna <3 veal", () => { - expect(isValidCityFoodPair("Vienna", "Wiener Schnitzel")).toBe(true); - }); - - test("San Juan <3 plantains", () => { - expect(isValidCityFoodPair("San Juan", "Mofongo")).toBe(true); - }); -});