使用Karma - done()运行的Mocha单元测试没有定义

Mocha unit tests running with Karma - done() is not defined

本文关键字:单元测试 Mocha 定义 运行 Karma done 使用      更新时间:2023-09-26

我正在尝试用Mocha编写测试以运行Karma,并且它们可以工作,但是我不能使用done()方法来实现异步测试,这基本上使工具对我来说毫无用处。我错过了什么?

karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '../..',
    frameworks: ['mocha', 'requirejs', 'qunit'],
    client: {
        mocha: {
            ui: 'bdd'
        }
    },
    files: [
      {pattern: 'libs/**/*.js', included: false},
      {pattern: 'src/**/*.js', included: false},
      {pattern: 'tests/mocha/mocha.js', included: false},
      {pattern: 'tests/should/should.js', included: false},
      {pattern: 'tests/**/*Spec.js', included: false},
      'tests/karma/test-main.js'
    ],
    exclude: [
      'src/main.js'
    ],
    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters: ['progress', 'dots'],
    port: 9876,
    colors: true,
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_WARN,
    autoWatch: true,
    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera (has to be installed with `npm install karma-opera-launcher`)
    // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
    // - PhantomJS
    // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
    browsers: ['Chrome'],
    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,
    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });
};

test-main.js (configure RequireJS)

var allTestFiles = [];
var pathToModule = function(path) {
  return path.replace(/^'/base'//, '../').replace(/'.js$/, '');
};
Object.keys(window.__karma__.files).forEach(function(file) {
  if (/Spec'.js$/.test(file)) {
    // Normalize paths to RequireJS module names.
    allTestFiles.push(pathToModule(file));
  }
});
require.config({
  // Karma serves files under /base, which is the basePath from your config file
  baseUrl: '/base/src',
  paths: {
    'should': '../tests/should/should',
    'mocha': '../tests/mocha/mocha',
    'pubsub': '../libs/pubsub/pubsub',
    'jquery': '../libs/jquery/jquery-1.10.2',
    'jquery-mobile': '//code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min'
  },
  // dynamically load all test files
  deps: allTestFiles, 
  // we have to kickoff jasmine, as it is asynchronous
  callback: window.__karma__.start
});

测试/fooSpec.js

define(['music/note'], function(Note) {
describe('nothing', function(done) {
    it('a silly test', function() {
        var note = new Note;
        note.should.not.eql(32);
    });
    done();
});
...

虽然这是一个人为的例子,但如果我删除done()调用,它就会成功。事实上,我得到:

Uncaught TypeError: undefined is not a function
at /Library/WebServer/Documents/vg/tests/mocha/fooSpec.js:8

这是done()行。这是如何/为什么没有定义?我不明白在哪里配置Mocha(或与什么选项)。是否存在某种全局命名空间或元编程魔法导致RequireJS干扰Mocha?

我在OS X 10.9.2的Chrome 33上运行测试,以防这是相关的。我已经在这上面浪费了大量的时间,并且准备放弃自动化测试:(——在使用QUnit/Karma/RequireJS时遇到了类似的瓶颈,并且没有找到任何成功自动化测试的替代方案。我觉得自己像个白痴。

在Mocha中,done回调是针对it, before, after, beforeEach, afterEach的。所以:

describe('nothing', function() {
    it('a silly test', function(done) {
        var note = new Note;
        note.should.not.eql(32);
        done();
    });
});

您在该示例中运行的测试不需要done()回调。它不是异步的。一个需要done回调的例子....

describe('Note', function() {
    it('can be retrieved from database', function(done) {
        var note = new Note();
        cb = function(){
           note.contents.should.eql("stuff retrieved from database");
           done()
        }
        //cb is passed into the async function to be called when it's finished
        note.retrieveFromDatabaseAsync(cb)
    });
});

你的测试不应该有一个完成的回调

describe('nothing', function() {
    it('umm...', function() {
        var note = new Note;
        note.should.not.eql(32);
    });
});

只有'it'函数提供了一个完成回调。Describe没有。你的问题不在于因果报应。您的摩卡测试没有正确定义。

天哪!

我永远也不会想到这会呕吐:

describe('nothing', function(done) {
    it('umm...', function() {
        var note = new Note;
        note.should.not.eql(32);
    });
    done(); // throws error that undefined is not a function
});

但这工作得很好:

describe('nothing', function(done) {
    it('umm...', function() {
        var note = new Note;
        note.should.not.eql(32);
    });
    setTimeout(function() {
        done();  // MAGIC == EVIL.
    }, 1000);
});