How to configure path aliases in Jest

If you use webpack aliases for your imports you need to let Jest know about them before you can test your code.
You can do this in a jest configuration file - `jest.config.js` for instance:
```js
module.exports = {
  moduleNameMapper: {
    //an import like: '@/interfaces/Foo.ts' 
    //becomes '[your-root]/src/interfaces/Foo.ts'
    '@/(.*)$': '<rootDir>/src/$1',
  },
  ...
};
```
If you want to learn how to configure webpack aliases you can [read this post](https://graffino.com/til/FRXaL59FzW-folder-aliases-using-webpack)

Denisa Halmaghi
28 Feb 2022
« Back to post