Skip to content Skip to sidebar Skip to footer

Jest Test Runs - Cannot Find Module Error

I have been working on a React Typescript repo and have been running into an annoying issue where in jest is not able to resolve imports relative to root dir. Cannot find module '

Solution 1:

Your implementation looks right. But it looks like the option moduleNameWrapper was the wrong option, it's supposed to be moduleNameMapper.

I also have an example as same as you which also uses babel as transplier, it works fine as I added moduleNameMapper. Here is the my example:

Jest configuration:

https://github.com/tmhao2005/lerna-demo/blob/master/packages/helper/jest.config.js

Here is the file for testing:

https://github.com/tmhao2005/lerna-demo/blob/master/packages/helper/src/index.tshttps://github.com/tmhao2005/lerna-demo/blob/master/packages/helper/src/index.test.ts

Solution 2:

forget the ~ character;

first define root directory to jest (i.e. src/);

then import your stuff from that root directory; (e.g. import { abc } from 'lib/dates')

by the way you can always import your stuff from default root without any configuration like this: import { abc } from 'src/lib/dates'

further read if you are using create-react-app jest absolute import

Post a Comment for "Jest Test Runs - Cannot Find Module Error"