使用Mocha进行测试时处理WebPack CSS导入

and*_*487 10 javascript mocha.js webpack

使用WebPack,您可以在代码中导入样式:import './PageSpinner.styl';但是当您尝试使用Mocha测试此代码时,您的测试将与SyntaxError崩溃,因为引擎会尝试处理JS代码等样式.

如何使用Mocha测试这样的代码?

Qus*_*uda 27

我最近遇到了同样的问题,解决方案是通过Mocha编译器.

创建一个文件,我们称之为'css-null-compiler.js',它有:

function noop() {
  return null;
}

require.extensions['.styl'] = noop;
// you can add whatever you wanna handle
require.extensions['.scss'] = noop;
require.extensions['.png'] = noop;
// ..etc
Run Code Online (Sandbox Code Playgroud)

从命令行运行mocha时,将此文件作为编译器传递

mocha /your/test.spec.js --compilers css:css-null-compiler.js
Run Code Online (Sandbox Code Playgroud)


bre*_*ent 5

这可以通过ignore-styles包来完成。

安装包,然后在运行 mocha 时需要。

例如

mocha --require babel-register --require ignore-styles
Run Code Online (Sandbox Code Playgroud)