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?

More than 1 year has passed since last update.

DDDでポケモンバトルを実装する(Step2:ポケモンのEntityのテストコードを書く)

0
Posted at

はじめに

前回までにポケモンのEntitiyを作成しました。今回はテストコードを実装していこうと思います。

成果物

テストの実行準備

今回テストはjestで実装していきます。必要なライブラリのinstallや設定をしていきます。

インストール
 yarn add --dev jest ts-jest @types/jest
 yarn add --dev typescript

ライブラリのインストールを実施します。

jest.config.jsの作成
npx ts-jest config:init
jest.config.js
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
    '^@entities/(.*)$': '<rootDir>/src/entities/$1',
    '^@enums/(.*)$': '<rootDir>/src/enums/$1'
  }
};

設定情報を編集しておきます。エイリアス情報はjestには引き継がれないため、事前にtsconfig.jsonと同じ設定をしておく必要があります。

テストコードの実装

src/entities/Pokemon.test.ts
import { PokemonType } from "@/enums/PokemonType";
import { Pokemon } from "@entities/Pokemon";
import type { Stats } from "@entities/Stats";
import type { Move } from "@entities/Move";

describe("Pokemon Entity", () => {
	it("should initialize with correct properties", () => {
		const stats: Stats = { hp: 100, attack: 50, defense: 30, speed: 40 };
		const moves: Move[] = [
			{ name: "Tackle", type: PokemonType.Electric, power: 40 },
			{ name: "Thunderbolt", type: PokemonType.Electric, power: 90 },
		];
		const pokemon = new Pokemon(
			25,
			"Pikachu",
			[PokemonType.Electric],
			stats,
			moves,
		); // タイプを配列で渡す
		expect(pokemon.name).toBe("Pikachu");
		expect(pokemon.type).toEqual([PokemonType.Electric]);
		expect(pokemon.getStats().hp).toBe(100);
		expect(pokemon.getStats().attack).toBe(50);
	});
});

テストコードの実行

テストコードの実行
~/develop/pokemon_ddd (feat/step2_test)$ yarn test
yarn run v1.22.19
$ jest
 PASS  src/entities/Pokemon.test.ts
  Pokemon Entity
    ✓ should initialize with correct properties (2 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.747 s, estimated 1 s
Ran all test suites.
✨  Done in 1.58s.

→良さそうですね!!うまく動いているのですが、念の為期待値を変更し、エラーが出るかを確認しておきます。

テストコードを改修し、テストを失敗させる。
		expect(pokemon.name).toBe("Pikachu");
		expect(pokemon.type).toEqual([PokemonType.Electric]);
		expect(pokemon.getStats().hp).toBe(200); //期待結果を100→200に変更
		expect(pokemon.getStats().attack).toBe(50);
失敗時の結果
~/develop/pokemon_ddd (feat/step2_test)$ yarn test
yarn run v1.22.19
$ jest
 FAIL  src/entities/Pokemon.test.ts
  Pokemon Entity
    ✕ should initialize with correct properties (2 ms)

  ● Pokemon Entity › should initialize with correct properties

    expect(received).toBe(expected) // Object.is equality

    Expected: 200
    Received: 100

      20 |              expect(pokemon.name).toBe("Pikachu");
      21 |              expect(pokemon.type).toEqual([PokemonType.Electric]);
    > 22 |              expect(pokemon.getStats().hp).toBe(200);
         |                                            ^
      23 |              expect(pokemon.getStats().attack).toBe(50);
      24 |      });
      25 | });

      at Object.<anonymous> (src/entities/Pokemon.test.ts:22:33)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.667 s, estimated 1 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

はい、想定通りのエラーが出ました。

ちなみにVSCodeのプラグインは優秀で、実行前からテストが失敗してくれる事を通知してくれました!!優秀ですね😊
スクリーンショット 2024-06-22 17.39.56.png

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?