简体   繁体   English

无法使用requirejs运行Karma

[英]Can not run Karma with requirejs

I create a simple project to try run a simple test case in Karma under require js. 我创建了一个简单的项目,尝试在require js下在Karma中运行一个简单的测试用例。 Something goes wrong but no error message. 出现问题,但没有错误消息。 Here is my configuration files. 这是我的配置文件。

Gruntfile.js 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 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 main.js //测试主

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

If I wrap describe function in define function, the test won't run any more. 如果我将describe函数包装在define函数中,则测试将不再运行。

It works after one change made to karma.conf.js from: 在对karma.conf.js进行以下更改后,它可以工作:

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

to: 至:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM