转换为requirejs后,Karma不会运行测试

时间:2013-11-30 01:35:24

标签: javascript requirejs karma-runner

要为当前测试运行的问题添加前缀,但它似乎挂起,它会启动浏览器,但不会自行退出浏览器,并且似乎没有在测试中运行代码。我认为它没有运行,因为浏览器没有加载最终的测试文件,但我不确定原因。这是我的karma.conf.js

// Generated on Mon Nov 18 2013 01:47:10 GMT-0600 (CST)

module.exports = function(config) {
  config.set({

    // base path, that will be used to resolve files and exclude
    basePath: '',


    // frameworks to use
    frameworks: ['jasmine', 'requirejs'],


    // list of files / patterns to load in the browser
    files: [
      {pattern: 'app/scripts/**/*.js', included: false},
      {pattern: 'app/scripts/*.js', included: false},
      {pattern: 'test/spec/**/*.js', included: false},
      'test/test-main.js',
    ],


    // list of files to exclude
    exclude: [],


    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters: ['progress'],


    // 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_DEBUG,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera (has to be installed with `npm install karma-opera-launcher`)
    // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
    // - PhantomJS
    // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
    browsers: ['Chrome'],


    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,


    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });
};

test/test-main.js

'use strict';
var tests = [];
for (var file in window.__karma__.files) {
    if ( window.__karma__.files.hasOwnProperty(file) ) {
            if (/spec\.js$/i.test(file)) {
                    tests.push(file);
            }
    }
}

require.config({
    baseUrl: '/base/src',
    paths: {
            'jQuery'          :'app/bower_components/jquery/jquery',
            'angular'         :'app/bower_components/angular/angular',
            'angular-sanitize':'app/bower_components/angular-sanitize/angular-sanitize',
            'angular-route'   :'app/bower_components/angular-route/angular-route',
            'angular-mocks'   :'app/bower_components/angular-mocks/angular-mocks'
    },
    shim: {
            'jQuery'          : { 'exports':'jQuery'},
            'angular'         : { 'exports':'angular', 'deps':['jQuery'] },
            'angular-sanitize': ['angular'],
            'angular-route'   : ['angular']
    },
    deps: tests,
},[
    'domReady',
    'controllers/main',
], function(domReady) {
    domReady(function() {
            console.log('testing');
            window.__karma__.start;
    })
});

test/spec/controllers/main.js

define(['angular','angular-mocks','controllers/insert'], function( angular, mocks, app ) {
    'use strict';

    describe('Controller: Insert', function () {
            // load the controller's module
            beforeEach(module('pasteyApp'));

            var Insert,
            scope;

            // Initialize the controller and a mock scope
            beforeEach(inject(function ($controller, $rootScope) {
                    scope = $rootScope.$new();
                    Insert = $controller('Insert', {
                      $scope: scope
                    });
            }));

            it('should attach a list of awesomeThings to the scope', function () {
                    expect(scope.code).toBeDefined;
            });
    });

});

grunt test输出

DEBUG [plugin]: Loading inlined plugin (defining ).
INFO [karma]: Karma v0.10.4 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
DEBUG [launcher]: Creating temp dir at /tmp/karma-26802229
DEBUG [launcher]: chromium --user-data-dir=/tmp/karma-26802229 --no-default-browser-check --no-first-run --disable-default-apps --start-maximized http://localhost:9876/?id=26802229
DEBUG [watcher]: Resolved files:
    /home/xenoterracide/dev/Pastey/node_modules/karma-requirejs/lib/require.js
    /home/xenoterracide/dev/Pastey/node_modules/karma-requirejs/lib/adapter.js
    /home/xenoterracide/dev/Pastey/node_modules/karma-jasmine/lib/jasmine.js
    /home/xenoterracide/dev/Pastey/node_modules/karma-jasmine/lib/adapter.js
    /home/xenoterracide/dev/Pastey/app/scripts/app.js
    /home/xenoterracide/dev/Pastey/app/scripts/controllers.js
    /home/xenoterracide/dev/Pastey/app/scripts/controllers/insert.js
    /home/xenoterracide/dev/Pastey/app/scripts/controllers/render.js
    /home/xenoterracide/dev/Pastey/app/scripts/main.js
    /home/xenoterracide/dev/Pastey/app/scripts/pastey.js
    /home/xenoterracide/dev/Pastey/app/scripts/routes.js
    /home/xenoterracide/dev/Pastey/app/scripts/services.js
    /home/xenoterracide/dev/Pastey/test/spec/controllers/insert.js
    /home/xenoterracide/dev/Pastey/test/test-main.js
DEBUG [web-server]: serving: /home/xenoterracide/dev/Pastey/node_modules/karma/static/client.html
DEBUG [web-server]: serving: /home/xenoterracide/dev/Pastey/node_modules/karma/static/karma.js
DEBUG [karma]: A browser has connected on socket nNwHTFhgWd4VWMXxdmm0
INFO [Chrome 31.0.1650 (Linux)]: Connected on socket nNwHTFhgWd4VWMXxdmm0
DEBUG [karma]: All browsers are ready, executing
DEBUG [web-server]: serving: /home/xenoterracide/dev/Pastey/node_modules/karma/static/context.html
DEBUG [web-server]: serving: /home/xenoterracide/dev/Pastey/node_modules/karma-requirejs/lib/require.js
DEBUG [web-server]: serving: /home/xenoterracide/dev/Pastey/node_modules/karma-requirejs/lib/adapter.js
DEBUG [web-server]: serving: /home/xenoterracide/dev/Pastey/node_modules/karma-jasmine/lib/jasmine.js
DEBUG [web-server]: serving: /home/xenoterracide/dev/Pastey/node_modules/karma-jasmine/lib/adapter.js
DEBUG [web-server]: serving: /home/xenoterracide/dev/Pastey/test/test-main.js
DEBUG [Chrome 31.0.1650 (Linux)]: Disconnected during run, waiting for reconnecting.
DEBUG [launcher]: Process Chrome exitted with code 0
ERROR [launcher]: Chrome crashed.

DEBUG [launcher]: Cleaning temp dir /tmp/karma-26802229
WARN [Chrome 31.0.1650 (Linux)]: Disconnected
Chrome 31.0.1650 (Linux): Executed 0 of 0 DISCONNECTED (4.751 secs / 0 secs)
DEBUG [launcher]: Disconnecting all browsers
DEBUG [launcher]: Killing Chrome
Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.

Elapsed time
concurrent:test  2s  ▇▇▇▇▇▇▇▇▇▇▇ 25%
karma:unit       6s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 74%
Total 8s

2 个答案:

答案 0 :(得分:2)

这里真正重要的是karma.conf.js没有加载所有的凉亭文件。然后,一些test-main.js路径出错了。

files: [
    'test/test-main.js',
    {pattern: 'test/spec/**/*.js',            included: false},
    {pattern: 'app/scripts/**/*.js',          included: false},
    {pattern: 'app/scripts/*.js',             included: false},
    {pattern: 'app/bower_components/**/*.js', included: false},
],


// list of files to exclude
exclude: [
    '**/*.swp',
    'app/bower_components/**/*min.js',
    'app/scripts/main.js',
],

我还没有让创建的循环数组工作

var tests = [];
for (var file in window.__karma__.files) {
    if (/spec\.js$/i.test(file)) {
            tests.push(file);
            console.log( file );
    }
}

require.config({
baseUrl: '/base/app/scripts',
    paths: {
            'domReady'        :'../bower_components/requirejs-domready/domReady',
            'jquery'          :'../bower_components/jquery/jquery',
            'angular'         :'../bower_components/angular/angular',
            'angular-sanitize':'../bower_components/angular-sanitize/angular-sanitize',
            'angular-route'   :'../bower_components/angular-route/angular-route',
            'angular-mocks'   :'../bower_components/angular-mocks/angular-mocks',
            'crypto'          :'../bower_components/cryptojs/lib/Crypto',
            'crypto.MD5'      :'../bower_components/cryptojs/lib/MD5',
            'highlight'       :'../bower_components/highlightjs/highlight.pack',
    },
    shim: {
            'jquery'          : { 'exports':'jquery'},
            'angular'         : { 'exports':'angular', 'deps':['jquery'] },
            'angular-sanitize': [ 'angular' ],
            'angular-route'   : [ 'angular' ],
            'angular-mocks'   : [ 'angular' ],
            'crypto.MD5'      : [ 'crypto'  ],
    },
deps: ['pastey','/base/test/spec/controllers/insert.js'],
    callback: window.__karma__.start,
});

答案 1 :(得分:1)

尝试更改文件数组中的规则顺序。看起来第一条规则具有更高的优先级:

files: [
    {pattern: 'test/test-main.js',    included: true},
    {pattern: 'test/spec/**/*.js',    included: false},
    ...
],
应将

tests/test-main.js加载到浏览器中以引导requirejs应用程序。其他模块将由requirejs加载。