最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - mocha equivalent to webpack's resolve root - Stack Overflow

programmeradmin3浏览0评论

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...

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...

Share Improve this question edited Jul 10, 2015 at 6:22 Felix Kling 816k180 gold badges1.1k silver badges1.2k bronze badges asked Jul 9, 2015 at 19:08 jcalfee314jcalfee314 4,8309 gold badges46 silver badges77 bronze badges 1
  • That's a non-standard webpack feature, it probably won't work easily in Node. Why not use standard relative paths? – loganfsmyth Commented Jul 9, 2015 at 20:00
Add a comment  | 

3 Answers 3

Reset to default 13

How about including your app directory in $NODE_PATH:

env NODE_PATH=$NODE_PATH:$PWD/app mocha ...

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 ..."
}

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

发布评论

评论列表(0)

  1. 暂无评论