#TIL 8 - Jest testing with absolute import | ProductsWay

#TIL 8 - Jest testing with absolute import

Feb 3, 2021 · Dung Huynh

What

Configure Jest so it resolves absolute imports the same way your app does (e.g., @/components/Button).

Why

Jest does not automatically apply your TypeScript path aliases. Map the @/ prefix to the project root with moduleNameMapper so tests resolve @/components/Button like the application does.

How

// jest.config.js
module.exports = {
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/$1",
  },
};