无法使用requirejs运行Karma

时间:2015-02-25 22:20:53

标签: gruntjs requirejs karma-runner

我创建了一个简单的项目,尝试在需要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 () {
            });
    });
//});

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

1 个答案:

答案 0 :(得分:2)

在对karma.conf.js进行一次更改后,它起作用:

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

到:

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