如何配置Karma以使用requirejs和qunit

max*_*e75 6 javascript qunit requirejs karma-runner

我正试图将业力和需求结合在一起.但找到一个大问题找不到任何答案.我有一个使用requirejs的项目,我使用qunit作为它的测试框架.在业力进来之前它们工作正常.在遵循Karma requirejs指令后,我收到错误并找不到合适的解决方案.在业力的版本是0.12.6

错误是:

Uncaught Error: Mismatched anonymous define () module ....
Run Code Online (Sandbox Code Playgroud)

我怎么能让他们一起工作?

这是我的文件结构

projectroot
    |
    |----\src
    |    |
    |    |----\demo
    |    |    |
    |    |    |----hello.js
    |    |
    |    |----\test
    |         |
    |         |----hello_test.js
    |         |----test_main.js      
    |       
    |----karma.conf.js    
Run Code Online (Sandbox Code Playgroud)

我的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
  });
};
Run Code Online (Sandbox Code Playgroud)

我的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
});
Run Code Online (Sandbox Code Playgroud)

我的hello_test.js

define(function(){
  it("is a simple test", function(){
    ok(true, "hope it works");
    });
});
Run Code Online (Sandbox Code Playgroud)

谢谢!

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

你可以看到我的hello_test.js被加载了.我在requirejs.org上阅读了有关#mismatch的文档.requirejs当它没有通过传统方式加载时,看起来无法处理模块名称.

max*_*e75 2

我的错

我的文件中有两个错误:

  1. includedkarma.conf.js 中的拼写错误
  2. 在 qunit 测试文件中。

正确的 qunit 测试用例应该是

define(function(){
    test("is a simple test", function(){
        ok(true, "hope it works");
    });
});
Run Code Online (Sandbox Code Playgroud)

不是it("is a simple test", function()...