Jest exception “cannot find module” with spfx solutions

I have written some unit test cases to test my SPFx web part using Jest and Enzyme. However, when I ran the tests, I encountered a “Cannot find module” exception on the import statement.

See below error details –

Cannot find module './httpClient/HttpClient' from 'node_modules/@microsoft/sp-http/lib-commonjs/index.js'
  Require stack:
  node_modules/@microsoft/sp-http/lib-commonjs/index.js
  src/common/services/sp-services.ts
  src/webparts/TestWebpart/components/custom/Services.ts
  src/webparts/TestWebpart/components/TestWebpart.tsx
  src/webparts/TestWebpart/test/TestWebpart.test.tsx

  at Resolver.resolveModule (node_modules/jest/node_modules/jest-resolve/build/resolver.js:324:11)
  at Object.<anonymous> (node_modules/@microsoft/sp-http/lib-commonjs/index.js:14:20)

Read more here: Source link