簡體   English   中英

使用Karma運行的Mocha單元測試 - done()未定義

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

我正在嘗試用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(配置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回調是針對itbeforeafterbeforeEachafterEach 所以:

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

這是文檔

神聖%$#@!

我不會在一百萬年后認為這會是barf:

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);
});

您在該示例中運行的測試不需要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'函數提供完成回調。 描述沒有。 你的問題不在於業力。 您的mocha測試未正確定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM