Test suite failed to run Cannot find module or its corresponding type declarations

Viewed 6902

I get the following error every time I try to run test.


  ● Test suite failed to run

    src/repositories/list_contacts/ListContactRepository.test.ts:4:37 - error TS2307: Cannot find module './ListContactRepository' or its corresponding type declarations.

    4 import {ListContactRepository} from './ListContactRepository';
                                          ~~~~~~~~~~~~~~~~~~~~~~~~~

Earlier everything worked fine but this happened after I rename
src/repositories/quick_contacts/QuickListContactRepository.test.ts to

src/repositories/list_contacts/ListContactRepository.test.ts

And run git checkout -b list. After some changes I committed there and come back to previous branch by git checkout dev now I face this error whenever I try to run test.

This file src/repositories/list_contacts/ListContactRepository.test.ts doesn't even exist on my current branch. But jest keep complaining. Any suggestion to solve this will be appreciated.

2 Answers

by default jest doesn't import files correctly so you have to configure jest to do so

in jest config file jest-e2e.json you have to add a moduleNameMapper json object and specify the importing setting to start from root:

"moduleNameMapper": {
    "~src/(.*)": "<rootDir>/src/$1"
  }

after that you need to make sure that jest is applying you modification on jest by clearing the jest cashe

npx jest --clearCache

finally, rerun your test and it should work

Related