testing - karma ,空测试套件

标签 testing karma-runner karma-jasmine

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

我有一个 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
  })
}

test-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 run 也什么都不做。根本没有错误信息。路径正确,没有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 才会执行测试。

关于testing - karma ,空测试套件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34271282/

相关文章:

android - 使用 calabash-android 解锁图案

karma-runner - 如何在 Visual Studio 中调试 typescript jasmine 测试?

javascript - AngularJS 测试服务结果为 'UnknownProvider'

testing - 无法使用 Angular-cli 运行 Karma

java - 如何从接口(interface)实现中访问字段以进行测试

java - Selenium webdriver使用计划?

python - 导入作为符号链接(symbolic link)的文件

javascript - 错误: No provider for "framework:browserify"!(解决:framework:browserify)

angular - 使用 Angular : how to test changes on parent to child 进行单元测试

javascript - 使用 ControllerAs 和 Ng-Show 测试数据指令