Mocha测试不能与Webpack和mocha-loader一起运行 [英] Mocha tests don't run with Webpack and mocha-loader

查看:134
本文介绍了Mocha测试不能与Webpack和mocha-loader一起运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些npm脚本移植到Webpack加载程序中,以更好地了解Webpack的工作方式,并且除了Mocha测试之外,我已经完成了所有工作:我的测试失败了,但没有显示Mocha正在使用mocha-loader或测试失败:

I am porting some npm scripts to Webpack loaders to better learn how Webpack works and I’ve got everything working except for my Mocha tests: I have one failing test, but it is not showing that Mocha is being run with the mocha-loader or that the test is failing:

要使所有src/**/*.test.js文件与Webpack中的Mocha一起运行,我需要做些什么?

What do I need to do differently to get all src/**/*.test.js files to run with with Mocha in Webpack?

  1. 克隆 https://github.com/trevordmiller/webpack-loaders-playground
  2. 运行npm test以查看测试应如何工作
  3. 运行npm run dev以查看测试如何不与Webpack一起运行
  1. Clone https://github.com/trevordmiller/webpack-loaders-playground
  2. Run npm test to see how tests should work
  3. Run npm run dev to see how tests don't run with Webpack

推荐答案

Mocha加载程序在构建时不会运行测试,它用于创建一个专门包含测试的包,然后可以从浏览器运行该包.

Mocha loader won't run tests while building, it's used to create a bundle specifically containing your tests which you can then run from your browser.

我建议为测试创建一个单独的webpack配置文件,然后将其托管在使用与应用程序不同端口的webpack-dev-server上.这是一个示例,它或多或少是我在自己的项目中使用的模式(撰写此答案时):

I would recommend creating a separate webpack config file for your tests, which you can then host on a webpack-dev-server that uses a different port from your application. Here's an example that's more-or-less the pattern that I use for my own projects (as of writing this answer):

module.exports = {
    entry: 'mocha!./tests/index.js',
    output: {
        filename: 'test.build.js',
        path: 'tests/',
        publicPath: 'http://' + hostname + ':' + port + '/tests'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                loaders: ['babel-loader']
            },
            {
                test: /(\.css|\.less)$/,
                loader: 'null-loader',
                exclude: [
                    /build/
                ]
            },
            {
                test: /(\.jpg|\.jpeg|\.png|\.gif)$/,
                loader: 'null-loader'
            }
        ]
    },
    devServer: {
        host: hostname,
        port: port
    }
};

tests/index.js

// This will search for files ending in .test.js and require them
// so that they are added to the webpack bundle
var context = require.context('.', true, /.+\.test\.js?$/);
context.keys().forEach(context);
module.exports = context;

package.json

"scripts": {
    "test": "find ./ -name '*.test.js' | xargs mocha -R min -r babel/register",
    "devtest": "webpack-dev-server --config webpack.tests.config.js",
    "dev": "webpack-dev-server --config webpack.config.js"
}

test.html

<!DOCTYPE html>
<html>
    <head>
        <title>Mocha</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="./node_modules/mocha/mocha.css" />
        <script src="/tests/test.build.js"></script>
    </head>
    <body>
    </body>
</html>

然后运行npm run devtest,打开http://localhost:<port you picked>/webpack-dev-server/test.html,然后mocha应该运行您的测试.

Then run npm run devtest, open http://localhost:<port you picked>/webpack-dev-server/test.html, and mocha should run your tests.

如果您不需要通过模块的CSS/LESS或图像,则可以从webpack.tests.config.js中删除这些加载器.

If you don't require CSS/LESS or images through your modules, you can remove those loaders from webpack.tests.config.js.

启用热加载后,这是一个非常不错的设置,因为我可以同时在不同的浏览器选项卡中运行我的应用程序和测试,然后更新代码并查看更改并立即重新运行测试.

With hot loading enabled this is a really great setup because I can have both my application and my tests running in different browser tabs, then update my code and see my changes and my tests re-running immediately.

您还可以运行npm run test以通过命令行执行相同的测试.

You can also run npm run test to execute the same tests through the command line.

希望这会有所帮助.

这篇关于Mocha测试不能与Webpack和mocha-loader一起运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆