使用Karma

时间:2016-06-26 18:13:48

标签: javascript webpack karma-runner karma-mocha karma-webpack

我正在设置Karma以简化旧项目中的单元测试。问题是,我收到错误You need to include some adapter that implements __karma__.start method!。正如我所发现的,这是一个非常非特定的错误,所以我不知道如何进一步调试它。我已将设置减少到最低限度,但错误仍然存​​在。

这是karma配置文件:

module.exports = function (config) {
  config.set({
    browsers: [ 'PhantomJS' ], //run in Phantom
    autoWatch: false,
    singleRun: true, //just run once by default
    frameworks: [ 'mocha', 'chai', 'sinon', 'chai-sinon' ], // test frameworks
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    files: [
      './js/test/tests.webpack.js' //just load this file
    ],
    preprocessors: {
      './js/test/tests.webpack.js': [ 'webpack', 'sourcemap' ] //preprocess with webpack and our sourcemap loader
    },
    reporters: [ 'mocha' ], //report results in this format
    webpack: { // webpack settings
      devtool: 'inline-source-map',
      module: {
        loaders: [
          { test: /\.js$/, loader: 'babel-loader' }
        ]
      }
    },
    webpackServer: {
      noInfo: true
    },
    plugins: [
        'karma-mocha',
        'karma-webpack',
        'karma-sourcemap-loader',
        'karma-mocha-reporter',
        'karma-phantomjs-launcher',
        'karma-chai',
        'karma-sinon',
        'karma-chai-sinon'
    ]
  });
};

以下是我安装的软件包(来自package.json):

"babel-core": "^6.9.1",
"babel-loader": "^6.2.4",
"babel-plugin-module-alias": "^1.5.0",
"babel-preset-es2015": "^6.9.0",
"chai": "^3.5.0",
"karma": "^1.0.0",
"karma-chai": "^0.1.0",
"karma-chai-sinon": "^0.1.5",
"karma-chrome-launcher": "^1.0.1",
"karma-mocha": "^1.0.1",
"karma-mocha-reporter": "^2.0.4",
"karma-phantomjs-launcher": "^1.0.1",
"karma-sinon": "^1.0.5",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.7.0",
"mocha": "^2.5.3",
"phantomjs": "^2.1.7",
"phantomjs-polyfill": "0.0.2",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0",
"webpack": "^1.13.1"

这是.babelrc:

{
  "presets": ["es2015"]
}

这是我在karma.conf.js(tests.webpack.js)中引用的测试文件。我最初的想法是要求其中的所有规范文件,但现在我已将其更改为至少运行一次测试。仍然没有运气:

import chai from 'chai';
var expect = chai.expect;

console.log('I was found');
it('works', function () {
    expect(true).to.equal(true);
})

请问您如何进一步调试此问题?

更新:这是一个gist,具有最小的可重现性案例。我必须在那里做错事,因为我仍然收到错误You need to include some adapter that implements __karma__.start method!,但我无法弄清楚我的错误是什么。

1 个答案:

答案 0 :(得分:3)

尝试在karma配置中删除对chai-sinon的引用。当我摆脱它时,测试运行。我无法准确解释原因,但也许版本不兼容。