1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

react-markdown使用時にJestでテスト実行すると"Must use import to load ES Module"のエラーが出る

Posted at

はじめに

react-markdownを使用したコンポーネントに対して、Jestでテストを実行すると"Must use import to load ES Module"のエラーが出ました。

react-markdownはESMで提供されていますが(v7以降)、CommonJS の形式で読み込まれてテスト実行されてしまっていると思われます。

解決方法

テスト時にreact-markdownをモック化しました。

Test.tsx
jest.mock("react-markdown", () => (props) => {
  return <>{props.children}</>;
});

おわりに

Jestの設定をいじることでESMを扱う方法もあります。
(私はうまくいきませんでした。)

参考

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?