测试反应和还原与业力和摩卡

时间:2016-03-01 10:09:24

标签: unit-testing reactjs mocha karma-runner redux

我已经开始使用React和Redux了,我想用Karma和Mocha以及PhantomJS2为它编写测试。我在这里使用这些来源作为基础:https://github.com/reactjs/redux/tree/master/examples/counter。我基本上想要使用Phantom在Karma中运行测试,而不是使用节点和“npm测试”。

我已经设置并安装了所需的karma包:

的package.json

"scripts": {
    "test:karma": "karma start",
},
"karma": "^0.13.21",
"karma-babel-preprocessor": "^6.0.1",
"karma-mocha": "^0.2.2",
"karma-phantomjs2-launcher": "^0.5.0",
"phantomjs2": "^2.2.0",

我试图找出如何构建我的karma.config.js,但我似乎没有让我的测试运行,这是我需要帮助的地方。

karma.config.js

module.exports = function(config) {
    process.env.PHANTOMJS_BIN = './node_modules/phantomjs2/lib/phantom/bin';
    config.set({
        basePath: './',
        frameworks: ['mocha'],
        plugins: [ 'karma-mocha',  'karma-phantomjs2-launcher', 'karma-babel-preprocessor' ],
        files: [
            "components/Counter.js",
            "test/components/Counter.spec.js"
        ],
        preprocessors: {
            "components/Counter.js": ["babel"],
            "test/components/Counter.spec.js": ["babel"]
        },
        babelPreprocessor: {
            options: {
                "presets": ["es2015", "react"],
            }
        },
        reporters: ['progress'],
        browsers: ['PhantomJS2'],
        port: 9099,
        runnerPort: 9100,
        urlRoot: '/',
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: false,
        singleRun: false,
        concurrency: Infinity
    })
}

1 个答案:

答案 0 :(得分:2)

对于react-boilerplate,我们有完整的设置 - 请查看我们的karma.conf.js和实施了Karma的PR,如果有帮助请告诉我们!