javascript - 如何配置 Karma 以使用 requirejs 和 qunit

标签 javascript requirejs qunit karma-runner

我正在尝试将 karma 和 requirejs 拼凑在一起。但发现一个大问题找不到任何答案。 我有一个使用 requirejs 的项目,我使用 qunit 作为它的测试框架。他们在 karma 进来之前工作得很好。 关注Karma requirejs instruction后,我得到一个错误,找不到合适的解决方案。 karma 版本为 0.12.6

错误是:

Uncaught Error: Mismatched anonymous define () module ....

如何让它们协同工作?

这是我的文件结构

projectroot
    |
    |----\src
    |    |
    |    |----\demo
    |    |    |
    |    |    |----hello.js
    |    |
    |    |----\test
    |         |
    |         |----hello_test.js
    |         |----test_main.js      
    |       
    |----karma.conf.js    

我的karma.conf.js

// Karma configuration
// Generated on Fri Apr 11 2014 11:43:46 GMT+0800 

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '.',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['qunit', 'requirejs'],
   //plugins:['karma-qunit','karma-launcher-chrome'], 

    // list of files / patterns to load in the browser
    files: [
      'src/test/test-main.js',
      {pattern: 'src/demo/*.js', included: false},
      {pattern: 'src/test/*.js', include: false}
    ],


    // list of files to exclude
    exclude: [

    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {

    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_DEBUGs,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  });
};

我的test-main.js

var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;

var pathToModule = function(path) {
  return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};

Object.keys(window.__karma__.files).forEach(function(file) {
  if (TEST_REGEXP.test(file)) {
    // Normalize paths to RequireJS module names.
    allTestFiles.push(pathToModule(file));
  }
});

console.log(allTestFiles);
require.config({
  // Karma serves files under /base, which is the basePath from your config file
  baseUrl: '/base',

  // dynamically load all test files
  deps: allTestFiles,

  // we have to kickoff jasmine, as it is asynchronous
  callback: window.__karma__.start
});

我的hello_test.js

define(function(){
  it("is a simple test", function(){
    ok(true, "hope it works");
    });
});

谢谢!

添加最终错误报告屏幕: error screen

您可以看到我的 hello_test.js 已加载。我在 requirejs.org 上阅读了关于#mismatch 的文档。看起来 requirejs 在没有通过常规方式加载时无法处理模块名称。

最佳答案

我的错误

我的文件中有两个错误:

  1. 在 karma.conf.js 中拼错了 included
  2. 在 qunit 测试文件中。

正确的 qunit 测试用例应该是

define(function(){
    test("is a simple test", function(){
        ok(true, "hope it works");
    });
});

不是 它(“是一个简单的测试”,function()...

关于javascript - 如何配置 Karma 以使用 requirejs 和 qunit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23007289/

相关文章:

javascript - `onclick` 使用 Internet Explorer 9 单击元素时未调用事件

jquery - 主干模型保存上没有方法 'get'

javascript - 错误 : Cannot find module ' in Protractor

RequireJS路径 'alias'问题

javascript - 在 AngularJS 中为按钮添加默认值

javascript - 安全地解析/使用来自 XMLHttpRequest 的 HTML

javascript - 中心动态 Angular 形式

javascript - 如何测试Javascript中另一个函数的函数调用次数?

Angularjs 和 qunit 测试

testing - 使用固定装置在 EmberJS 和 QUnit 中进行数据测试