简体   繁体   English

如何配置Karma以与requirejs和qunit一起使用

[英]How to configure Karma to work with requirejs and qunit

I'm trying to piece karma and requirejs together. 我正在尝试将业力和requirejs拼凑在一起。 but find a big issue cannot find any answer. 但发现一个大问题找不到任何答案。 I have a project using requirejs and I'm using qunit as its testing framework. 我有一个使用requirejs的项目,并且使用qunit作为其测试框架。 they work fine before karma comes in. After following the Karma requirejs instruction , I got an error and could not find the proper solution. 在使用业力之前,它们可以正常工作。在遵循业力requirejs指令之后 ,出现了错误,并且找不到正确的解决方案。 The karma version is 0.12.6 业力版本为0.12.6

The error is: 错误是:

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

How can I let them work together? 我如何让他们一起工作?

here is my files structure 这是我的文件结构

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

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

my test-main.js 我的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
});

my hello_test.js 我的hello_test.js

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

Thank you! 谢谢!

ADD the final error report screen: 添加最终错误报告屏幕: 错误画面

You can see my hello_test.js is loaded. 您可以看到我的hello_test.js已加载。 I read the docs about #mismatch at requirejs.org. 我在requirejs.org上阅读了有关#mismatch的文档。 It looks like requirejs cannot handle the module name when it's not loaded through their conventional way. 看起来当requirejs无法通过常规方式加载时,无法处理模块名称。

My Mistake 我的错

two mistakes in my files: 我的档案中有两个错误:

  1. misspell the included in karma.conf.js karma.conf.js中included拼写错误
  2. in qunit test file. 在qunit测试文件中。

correct qunit test case should be 正确的qunit测试用例应该是

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

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

Not sure if with your two changes you got it to work or not, but after spending about six hours fighting this same problem, I'd suggest either: 不知道您是否通过两次更改就可以正常工作,但是在花了大约六个小时解决这个问题之后,我还是建议:

  • Removing your line {pattern: 'src/demo/*.js', included: false}, 删除行{pattern: 'src/demo/*.js', included: false},
  • or, adding 'src/demo/hello.js' to your exclude: [ ] array. 或者,将'src/demo/hello.js'到您的exclude: [ ]数组中。

This was part of what caused many of my headaches. 这是引起我很多头痛的部分原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM