业力,空测试套件

Karma, empty test suite

首先我知道有类似的问题,但 none 对我有用。

我有一个 Angular 应用程序,我想用 Karma、Jasmin 和 RequireJs 对其进行测试。

我已经安装了我需要的一切,并按照这里写的配置了一切:http://karma-runner.github.io/0.13/plus/requirejs.html

下面是我的文件的样子

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: [
        'src/test/Tests/*.js',
        'src/test/Tests/directives/*.js',

        //source code
        {pattern: 'src/main/webapp/app/*.js', included: false},
        {pattern: 'src/main/webapp/app/**/*.js', included: false},

        {pattern: 'src/test/test-main.js', included: true}
    ],


    // 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/Tests/*.js': 'coverage'
    },

      coverageReporter:{
          type:'html',
          dir:'../coverage/'
      },
    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
      reporters: ['progress', 'coverage'],


    // 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: ['PhantomJS'],

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

    // Concurrency level
    // how many browser should be started simultanous
    concurrency: Infinity
  })
}

测试-main.js

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

// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function(file) {
    if (TEST_REGEXP.test(file)) {
        // Normalize paths to RequireJS module names.
        // If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
        // then do not normalize the paths
        var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
        allTestFiles.push(normalizedTestModule);
    }
});

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

输出在 IntelliJ IDEA 14.1.5

空测试套件。

进程已完成,退出代码为 0


服务器正常启动。我也尝试过命令行。 $ karma start 和 $ karma 运行,它们也什么都不做。根本没有错误信息。路径正确,没有404错误。

你猜猜可能是什么问题?

从测试文件中删除模块名称。

就我而言,这就是解决方案。

根据 RequireJS 文档 (http://requirejs.org/docs/api.html#modulename),您可以包含模块的名称,如下所示:

//Explicitly defines the "foo/title" module:
define("foo/title",
    ["my/cart", "my/inventory"],
    function(cart, inventory) {
        //Define foo/title object in here.
   }
);

但对我来说,只有当我删除 "foo/title" 名称时,Karma 才会执行测试。