import { describe, it, expect } from "vitest";
import fetch from "node-fetch";
// テスト対象の関数を再定義(本来はimportするが、ここでは自己完結のため再掲)
type PokeApiResponse = {
name: string;
id: number;
height: number;
weight: number;
types: { type: { name: string } }[];
};
async function getPokemonInfo(nameOrId: string) {
const res = await fetch(
`https://pokeapi.co/api/v2/pokemon/${encodeURIComponent(nameOrId)}`
);
if (!res.ok) throw new Error("Not found");
const data = (await res.json()) as PokeApiResponse;
return {
name: data.name,
id: data.id,
height: data.height,
weight: data.weight,
types: data.types.map((t) => t.type.name),
};
}
describe("getPokemonInfo", () => {
it("should fetch Pikachu info correctly", async () => {
const result = await getPokemonInfo("pikachu");
expect(result.name).toBe("pikachu");
expect(result.id).toBe(25);
expect(Array.isArray(result.types)).toBe(true);
expect(result.types).toContain("electric");
expect(typeof result.height).toBe("number");
expect(typeof result.weight).toBe("number");
});
it("should throw on not found", async () => {
await expect(getPokemonInfo("notapokemon")).rejects.toThrow("Not found");
});
});