4
3

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.

Cannot find module '@testing-library/jest-dom/extend-expect'エラーが出る

Posted at

はじめに

Jestを設定していたときにChatGPTに頼っていたところうまく行かなかったのでまとめます

問題

以下のテストを書いたところエラーが出ました

page.test.tsx
import React from "react";
import { render, screen } from "@testing-library/react";
import "@testing-library/dom";
import { About } from "../page";

describe("About", () => {
  it("Aboutというタイトルがあること", () => {
    render(About());
    const aboutImage = screen.getByTestId("about-image").querySelector("img");
    expect(aboutImage!).toBeInTheDocument();
  });
});
 Cannot find module '@testing-library/jest-dom/extend-expect' 

解決方法

そもそも最新の@testing-library/jest-domでは、extend-expectのインポートは不要になりました

なので以下をインポートするだけでうまく動くようになりました

page.test.tsx
import '@testing-library/jest-dom';

おわりに

ChatGPTに頼りすぎると思わぬところで沼ってしまうことがよくわかりました
ある程度のところでネットで調べたり、聞き方を変えてみたりしないといけないです

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?