Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mocha equivalent to webpack's resolve root

I'm writing some mocha tests that load code that have paths like this:

import MyStore from "stores/MyStore"

This works fine in the web browser because we are using the webpack-dev-server which in turn reads this entry from webpack.config.js: config.resolve.root: [path.resolve(__dirname, "./app")] so it knows to find ./app/stores/MyStore.

This path does not work when running it from mocha --compilers js:babel/register. I'm trying to locate a package or configuration that I may use for this. It would save us from having to change may code references and of course keep our imports more portable.

Not sure if it matters, we use iojs. If this really can't be done it would be fine just to update the paths. Thank you...

like image 717
jcalfee314 Avatar asked Jul 09 '15 19:07

jcalfee314


People also ask

Is Mocha a BDD framework?

Mocha provides a variety of interfaces for defining test suites, hooks, and individual tests, including TSS, Exports, QUnit, and Require. The default interface is behavior-driven development (BDD), which aims to help developers build software that is predictable, resilient to changes, and not error-prone.

What is Mocha used for?

Mocha is a feature-rich JavaScript test framework running on Node. js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

What is Mocha unit test?

Mocha is a widely used JavaScript test framework running on NodeJS and browsers. It supports asynchronous testing running the tests serially, allowing for more flexible and accurate reporting. It is a highly customizable framework that supports different assertions and libraries.

What is Mocha webpack?

mocha-webpack is basically a wrapper around the following command... $ webpack test.js output.js && mocha output.js. ... but in a much more powerful & optimized way. mocha-webpack ... precompiles your test files automatically with webpack before executing tests.


3 Answers

How about including your app directory in $NODE_PATH:

env NODE_PATH=$NODE_PATH:$PWD/app mocha ...
like image 192
robertklep Avatar answered Oct 16 '22 02:10

robertklep


Here's a cross-platform method. First install cross-env:

npm install cross-env --save-dev

then in your package.json:

"scripts": {
    ...
    "test": "cross-env NODE_PATH=./app mocha ..."
}
like image 42
Kai Avatar answered Oct 16 '22 03:10

Kai


In windows, I had to do this:

set NODE_PATH=%CD%/app&& mocha...

for some reason, adding a space after 'app' would cause it not to work

like image 44
Daniel Cefram Ramirez Avatar answered Oct 16 '22 04:10

Daniel Cefram Ramirez