【问题标题】:Can not run Karma with requirejs无法使用 requirejs 运行 Karma
【发布时间】:2015-02-25 22:20:53
【问题描述】:

我创建了一个简单的项目来尝试在需要 js 下在 Karma 中运行一个简单的测试用例。出现问题但没有错误消息。这是我的配置文件。

Gruntfile.js

module.exports = function (grunt) {
    grunt.initConfig({
        karma: {
            frontend: {
                configFile: 'test/karma.conf.js'
            }
        }
    });
    grunt.loadNpmTasks('grunt-karma');
    grunt.registerTask('test', ['karma:frontend']);
};

karma.conf.js

module.exports = function (config) {
    config.set({
        basePath: '../',
        autoWatch: true,
        // web server port
        port: 9876,
        frameworks: ['mocha', 'requirejs', 'chai', 'sinon'],
        files: [
            'test/main.js', 
            'test/*Spec.js'
        ],
        exclude: [],
        browsers: ['PhantomJS'], //'Chrome', 
        logLevel: config.LOG_DEBUG,
        plugins: ['karma-mocha', 'karma-chai', 'karma-sinon', 'karma-requirejs', 'karma-chrome-launcher', 'karma-phantomjs-launcher'],
        singleRun: true
    });
};

main.js //test-main

(function (window, require) {
    'use strict';
    var tests = [];
    for (var file in window.__karma__.files) {
        if (window.__karma__.files.hasOwnProperty(file)) {
            if (/Spec\.js$/.test(file)) {
                console.log('add file = '+file);
                tests.push(file);
            }
        }
    }
    require({
        deps: tests,
        callback: window.__karma__.start
    });
}(window, require));

//define('helloSpec', function(){//if uncomment this line, this spec will not run at all
    'use strict';
    describe('helloSpec',
        function () {
            console.log('helloSpec');

            before(function () {
            });

            it('Say hello', function () {
            });
    });
//});

如果我将describe函数包装在define函数中,测试将不再运行。

【问题讨论】:

    标签: gruntjs requirejs karma-runner


    【解决方案1】:

    在对 karma.conf.js 进行一次更改后,它可以工作:

    files: [
                'test/main.js', 
                'test/*Spec.js'
            ],
    

    到:

    files: [
                'test/test-main.js',
                {pattern: 'test/*Spec.js', included: false}
            ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 2016-03-19
      • 2016-10-13
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多