Here is my test file
// /imports/components/main.test.js
import React from 'react'
import { shallow, mount } from 'enzyme'
import Main from './main'
import TextInput from "/imports/ui/textInput"
...
and the main.js has
// /imports/components/main.js
import { action1 } from "/imports/actions/myAction"
but it throws an error when I run the test, saying
Cannot find module '/imports/actions/myAction' from 'main.js'
If I comment the import './main'
, same thing happen with importing TextInput. I have no issue with importing modules in node_modules.
How can I tell Jest or webpack to import the component using absolute path from project directory (i.e import Foo from /imports/...
)?
import TextInput from "../ui/textInput"
in main.test.js andimport { action1 } from "../actions/myAction"
. If you want to import with absolute path, then you must do aliasing in your webpack.config and then import likeimport { action1 } from "imports/actions/myAction"
. – Slaver../actions/myAction
is imported from main.js, not test file, and it is working perfectly fine on the component. And I don't need to import that on test file. In test file, I just need to import the main.js component and textInput for testing shallow rendering. I am sorry if it is confusing. – Helpmeet