Get error module not found in component file

128 views Asked by At

I'm trying to write unit test. I just import my component file inside test.file. When trying run test. It gives error module not found for file import in file component, while in file component the import is working fine.

Component file - example.tsx

import getConfig from '@/component/utils/appConfig'

jest.config.js

 module.exports = {moduleDirectories:["node_modules","src"]}

test.spec.js

    import example from '../../example.js'

describe('example',() => {
 it('render component file',()=> {
  const container = render(<example/>);
})})

after 'npm run test'

then, get error

cannot find module '@/component/utils/appConfig' from example.tsx

1

There are 1 answers

1
Nishant On

The way we usually use import is based on relative path.

.and .. are similar to how we use to navigate in terminal like cd .. to go out of directory and mv ~/file . to move a file to current directory.

Your Statement Should be like this...

import getConfig from './component/utils/appConfig'