0
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?

More than 3 years have passed since last update.

Jestで`Cannot use import statement outside a module`

Last updated at Posted at 2021-12-02

遭遇したエラー

JestでテストをしたらCannot use import statement outside a moduleというエラーが出た.調べてもなかなか解決策が見つからなかったのでメモ.

解決策

Jsetはテスト時に対象ファイルをBabelでECMAScriptをCommonJSに変換しているのだが,node_modules配下のファイルは変換しないようにデフォルトで決まっている.

jestの設定を

jest.config.js
transformIgnorePatterns: [
  "/node_modules/(?!(foo|bar)/)"
]

のように正規表現で書き換えればnode_modules/foo/配下のファイルも変換するようになる.

参考: Configuring Jest

0
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
0
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?