Karma:执行了 0 个错误,共 0 个错误

Karma: Executed 0 of 0 Error

我想我已经正确设置了所有内容。我遵循了 RequireJS 的 Karma 教程的规范,但我尝试的一切似乎都会导致相同的错误。

似乎正在加载我的测试-main.js 文件,因为 console.log() 将触发。但是,在 Object.keys 循环中,文件将被列出,但 TEST_REGEXP 失败,因此 allTestFiles 最终成为一个空数组。我确定它有点傻,但它的创建就像教程一样 - 除了使用 node_modules 作为 jquery、require、underscore.

我的 test-main.js 文件:

var allTestFiles    = [];
var TEST_REGEXP = /test\.js$/;

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

if(console) console.log(allTestFiles);

require.config(
{
    // Karma serves files under /base, which is the basePath from the config file
    baseUrl: '/base/src',

    paths:
    {
        'jquery':'../node_modules/jquery/dist/jquery',
        'underscore':'../node_modules/underscore/underscore-min'
    },

    shim:
    {
        'underscore': { exports: '_' }
    },

    // dynamically load all test files
    deps: allTestFiles,

    // kick off jasmine, as it is asynchronous
    callback: window.__karma__.start
});

我的 karma.conf.js 文件:

 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: ['jasmine', 'requirejs'],


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


        // list of files to exclude
        exclude: [
          'src/main.js'
        ],


        // 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_INFO,


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

我找到了解决问题的方法。

我最终将 require.config 部分上方的测试-main.js 文件更改为:

// Karma RequireJS configuration
var tests = [];
for (var file in window.__karma__.files) {
    if (/Spec\.js$/.test(file)) {
        tests.push(file);
    }
}

如果我只是将 TEST_REGEXP 更改为 /Spec\.js$/,我最终会收到时间戳错误。我不知道为什么。更有趣的是为什么遵循指南会产生错误。但现在一切正常了。