Gulp Mocha 和 Browserify 的绝对路径

Absolute Paths with Gulp Mocha and Browserify

我有一个项目正在使用 Browserify and ES6 to handle importing and defining packages. The project is using absolute paths using the 'paths' option when building with Gulp-Browserify

这对于源代码来说工作正常,但现在我试图用 Mocha and run them using gulp-mocha 编写测试,这导致了问题。 Mocha 期待相对路径,但如果我给它一个相对路径到一个文件,该文件具有使用绝对路径的其他导入,测试将失败并显示 MODULE_NOT_FOUND 错误。

例如

Mocha Import at test/actions/user.js:
      import createUser from '../../src/actions/user';
      ...

Source Import at src/actions/user.js:
      import CREATE_USER from 'constants/use
      ...

会导致 MODULE_NOT_FOUND_ERROR

我想知道是否有任何方法可以在 mocha 中设置绝对路径列表,类似于 browserify 的方法?

您可以使用 app-require-path。只需将其安装为开发人员并添加以下两个文件:

test/mocha.opts

--require test/_bootstrap.js

test/_bootstrap.js

require('app-require-path')(__dirname + '/..');

就是这样。您可以将 _bootstrap.js 中的路径更改为任何您想要的路径。您还可以添加多个路径。由你决定。