LoginSignup
1
1

More than 5 years have passed since last update.

jest.moduleNameMapper は一番最初にマッチしたパターンが使われる

Posted at

問題

こんな感じに設定してた。

{
  "jest": {
    "moduleNameMapper": {
      "^components": "<rootDir>/src/scripts/components",
      "^components/(.+)": "<rootDir>/src/scripts/components/$1",
    }
  }
}

ただこれだと、import Foo from 'components/foo'としても、components/indexが返されてしまっていた。

解決

順番やパターンを変える。

{
  "moduleNameMapper": {
    "^components/(.+)": "<rootDir>/src/scripts/components/$1",
    "^components": "<rootDir>/src/scripts/components",
  }
}

or

{
  "moduleNameMapper": {
    "^components(?!/)": "<rootDir>/src/scripts/components",
    "^components/(.+)": "<rootDir>/src/scripts/components/$1",
  }
}

or

{
  "moduleNameMapper": {
    "^components(.*)": "<rootDir>/src/scripts/components$1"
  }
}
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