Basically my webpack.config.js has this context - context: path.resolve( __dirname, '../app' ), so that in all the various nested files I can import anything directly
import Button from 'app/components/button'
(http://stackoverflow.com/questions/27502608/resolving-require-paths-with-webpack/27512914)
How can configure something like this in mocha, so that it doesn't break at the import statements?
try mocha --require babel-register /path/to/test/files
UPDATE: You will need a Babel plugin or something else to do this. Mocha can't help with import statements yet.
Actually I fixed it with NODE_PATH=. (or =directory where app exists)
@pnkapadia6 have you found any other way to do this? Is there any config in Mocha to do this? Prefer not to set NODE_PATH...
@cbengtson85 No, that's the only solution I could find.
i tired setting mine like so: "test": "SET NODE_PATH= './app' && mocha --require ./test/testSetup.js --compilers babel-core/register --recursive \"./app/**/*.test.js\"",
but still i keeps gettings error that component module cant be found
my app structure:
- app
components
componentA
componentB
index.js(exports all components)
containers
containerA
containerB
index.js(exports all containers)
sometest.test.js
index.js
test
-testSetup.js
@boneskull Doesn't work for me. Can you reopen this please? Looks like a lot of people are having trouble with this.
I have a component from a lib (rc-slider) that needs me to include the CSS from node_modules. I've already set my NODE_PATH. I was using --compilers js:babel-core/register but changing to --require babel-register hasn't changed the problem at all.
ah, I see, you want relative paths. maybe there's a babel plugin that'll help you. Mocha can't support this behavior because it knows nothing of ES modules in Node.js yet. once that API has been finalized in Node.js, we'll implement support.
Looks like I actually had a different problem and I got my tests working. Sorry for the noise.
I'll add what details I can about my situation in the hope that it will help others. I don't fully understand why this works :(
Mocha is run via npm with
NODE_PATH=./frontend mocha --watch --compilers js:babel-core/register --require ignore-styles --require ./frontend/test_helper.js frontend/**/*.spec.js
(test_helper is irrelevant to this example, it just imports libs and sets up jsdom)
In my own source I import rc-slider like this:
import Slider, { Range } from 'rc-slider'
import 'rc-slider/assets/index.css'
All of my client-side source code lives in frontend/ my libraries (including rc-slider) are all under frontend/node_modules/ following npm convention.
test/index.js
//Wrong
let app = require('../app/index.js');
let config = require('../config/xxx.json');
//Right
let app = require('./app/index.js');
let config = require('./config/xxx.json');
mocha is running at Server dir, so the process.env.PWD is /Users/xxx/.../Server
Most helpful comment
i tired setting mine like so:
"test": "SET NODE_PATH= './app' && mocha --require ./test/testSetup.js --compilers babel-core/register --recursive \"./app/**/*.test.js\"",but still i keeps gettings error that component module cant be found
my app structure: