仅在使用--single-run标志时使用Karma运行测试时出错

时间:2015-09-02 13:11:48

标签: javascript jasmine karma-runner

我使用Karma和Jasmine对我拥有的一些Javascript文件进行单元测试。如果我在已经启动的karma服务器上运行测试,那么没有问题并且测试运行并通过:

karma start karma.conf.js
02 09 2015 13:51:13.218:WARN [karma]: No captured browser, openhttp://localhost:9876/
02 09 2015 13:51:13.226:INFO [karma]: Karma v0.13.9 server started at http://localhost:9876/
02 09 2015 13:51:13.230:INFO [launcher]: Starting browser PhantomJS
02 09 2015 13:51:14.128:INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket nE6zCoofERlwxbdEAAAA with id 11156374
02 09 2015 13:51:14.198:WARN [web-server]: 404: /img.png
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 3 of 3 SUCCESS (0.003 secs / 0.023 secs)

但是我试图让这些测试在Jenkins上自动运行,所以这个命令不适合服务器继续运行,这会阻止构建完成。将--single-run标志设置为true 作为解决方案,但会产生以下错误:

karma start karma.conf.js --single-run
02 09 2015 15:01:57.755:DEBUG [config]: autoWatch set to false, because of singleRun
02 09 2015 15:01:57.759:DEBUG [plugin]: Loading karma-* from /usr/local/lib/node_modules
02 09 2015 15:01:57.761:DEBUG [plugin]: Loading plugin /usr/local/lib/node_modules/karma-jasmine.
02 09 2015 15:01:57.762:DEBUG [plugin]: Loading plugin /usr/local/lib/node_modules/karma-phantomjs-launcher.
02 09 2015 15:01:57.826:INFO [karma]: Karma v0.13.9 server started at http://localhost:9876/
02 09 2015 15:01:57.830:INFO [launcher]: Starting browser PhantomJS
02 09 2015 15:01:57.830:DEBUG [temp-dir]: Creating temp dir at /var/folders/3d/j75hlwl97r37nsdgn72ypq28dp4wp2/T/karma-61237386
02 09 2015 15:01:57.835:DEBUG [launcher]: /usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs /var/folders/3d/j75hlwl97r37nsdgn72ypq28dp4wp2/T/karma-61237386/capture.js
02 09 2015 15:01:58.655:DEBUG [web-server]: serving: /usr/local/lib/node_modules/karma/static/client.html
02 09 2015 15:01:58.664:DEBUG [web-server]: serving: /usr/local/lib/node_modules/karma/static/karma.js
02 09 2015 15:01:58.687:DEBUG [karma]: A browser has connected on socket -EQr1esSnrr2-1vpAAAA
02 09 2015 15:01:58.690:DEBUG [web-server]: upgrade /socket.io/?EIO=3&transport=websocket&sid=-EQr1esSnrr2-1vpAAAA
02 09 2015 15:01:58.706:INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket -EQr1esSnrr2-1vpAAAA with id 61237386
02 09 2015 15:01:58.706:DEBUG [launcher]: PhantomJS (id 61237386) captured in 0.879 secs
02 09 2015 15:01:58.714:DEBUG [web-server]: serving: /usr/local/lib/node_modules/karma/static/context.html
02 09 2015 15:01:58.715:DEBUG [middlware:source-files]: Requesting /absolute/usr/local/lib/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?578a1e5ff14db21b04e2d6db7fd0eda37042440c /
02 09 2015 15:01:58.715:DEBUG [middlware:source-files]: Fetching /usr/local/lib/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
02 09 2015 15:01:58.716:DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
02 09 2015 15:01:58.717:DEBUG [middlware:source-files]: Requesting /absolute/usr/local/lib/node_modules/karma-jasmine/lib/boot.js?4a7da64f416169520c9d5c43b5a7feac6bde9104 /
02 09 2015 15:01:58.717:DEBUG [middlware:source-files]: Fetching /usr/local/lib/node_modules/karma-jasmine/lib/boot.js
02 09 2015 15:01:58.717:DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma-jasmine/lib/boot.js
02 09 2015 15:01:58.718:DEBUG [middlware:source-files]: Requesting /base/Website/static/js/wayfinder.js?016cf222a0fd28ad11af6a205a0840adba05bc9d /
02 09 2015 15:01:58.718:DEBUG [middlware:source-files]: Fetching /Users/tll01/Documents/way-finder/Website/static/js/wayfinder.js
02 09 2015 15:01:58.718:DEBUG [middlware:source-files]: Requesting /absolute/usr/local/lib/node_modules/karma-jasmine/lib/adapter.js?3030709c83121e1b2ca4d1e657306b834fc13350 /
02 09 2015 15:01:58.718:DEBUG [middlware:source-files]: Fetching /usr/local/lib/node_modules/karma-jasmine/lib/adapter.js
02 09 2015 15:01:58.718:DEBUG [middlware:source-files]: Requesting /base/Website/static/js/jquery/jquery-2.1.4.js?0fed45ad7a48ace869bc725ca474ad86a1ef1562 /
02 09 2015 15:01:58.719:DEBUG [middlware:source-files]: Fetching /Users/tll01/Documents/way-finder/Website/static/js/jquery/jquery-2.1.4.js
02 09 2015 15:01:58.719:DEBUG [middlware:source-files]: Requesting /base/Website/static/js/leaflet/leaflet.js?6b6a0b033c91fd914999d9ce32fc49b567032e3d /
02 09 2015 15:01:58.719:DEBUG [middlware:source-files]: Fetching /Users/tll01/Documents/way-finder/Website/static/js/leaflet/leaflet.js
02 09 2015 15:01:58.719:DEBUG [middlware:source-files]: Requesting /base/Website/static/js/leaflet/leaflet.draw.js?607b1f1da44ef7ca11efa37da98a77b27db11d6f /
02 09 2015 15:01:58.719:DEBUG [middlware:source-files]: Fetching /Users/tll01/Documents/way-finder/Website/static/js/leaflet/leaflet.draw.js
02 09 2015 15:01:58.719:DEBUG [web-server]: serving (cached): /Users/tll01/Documents/way-finder/Website/static/js/wayfinder.js
02 09 2015 15:01:58.720:DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma-jasmine/lib/adapter.js
02 09 2015 15:01:58.720:DEBUG [web-server]: serving (cached): /Users/tll01/Documents/way-finder/Website/static/js/jquery/jquery-2.1.4.js
02 09 2015 15:01:58.720:DEBUG [web-server]: serving (cached): /Users/tll01/Documents/way-finder/Website/static/js/leaflet/leaflet.js
02 09 2015 15:01:58.721:DEBUG [web-server]: serving (cached): /Users/tll01/Documents/way-finder/Website/static/js/leaflet/leaflet.draw.js
02 09 2015 15:01:58.721:DEBUG [middlware:source-files]: Requesting /base/Website/static/js/tests/floorcontroller.test.js?f91ff99985cffbfc55d744029683c60283bc709d /
02 09 2015 15:01:58.721:DEBUG [middlware:source-files]: Fetching /Users/tll01/Documents/way-finder/Website/static/js/tests/floorcontroller.test.js
02 09 2015 15:01:58.721:DEBUG [middlware:source-files]: Requesting /base/Website/static/js/python-models.js?86bc9bb6d49f1a9a2ca5b38037eedf6436774053 /
02 09 2015 15:01:58.721:DEBUG [middlware:source-files]: Fetching /Users/tll01/Documents/way-finder/Website/static/js/python-models.js
02 09 2015 15:01:58.722:DEBUG [middlware:source-files]: Requesting /base/Website/static/js/leaflet/leaflet.distortableimage.js?8c54672d7373c4c51abd6d544949e8953f86d482 /
02 09 2015 15:01:58.722:DEBUG [middlware:source-files]: Fetching /Users/tll01/Documents/way-finder/Website/static/js/leaflet/leaflet.distortableimage.js
02 09 2015 15:01:58.722:DEBUG [middlware:source-files]: Requesting /base/Website/static/js/shared/floorcontroller.js?e00e8e03e847f9415ef608dc97183f9f90570bd4 /
02 09 2015 15:01:58.722:DEBUG [middlware:source-files]: Fetching /Users/tll01/Documents/way-finder/Website/static/js/shared/floorcontroller.js
02 09 2015 15:01:58.722:DEBUG [web-server]: serving (cached): /Users/tll01/Documents/way-finder/Website/static/js/tests/floorcontroller.test.js
02 09 2015 15:01:58.722:DEBUG [web-server]: serving (cached): /Users/tll01/Documents/way-finder/Website/static/js/python-models.js
02 09 2015 15:01:58.722:DEBUG [web-server]: serving (cached): /Users/tll01/Documents/way-finder/Website/static/js/leaflet/leaflet.distortableimage.js
02 09 2015 15:01:58.723:DEBUG [web-server]: serving (cached): /Users/tll01/Documents/way-finder/Website/static/js/shared/floorcontroller.js
02 09 2015 15:01:58.775:DEBUG [middlware:source-files]: Requesting /img.png /
02 09 2015 15:01:58.775:DEBUG [middlware:source-files]: Fetching /img.png
02 09 2015 15:01:58.776:WARN [web-server]: 404: /img.png
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 3 of 3 SUCCESS (0.003 secs / 0.023 secs)
02 09 2015 15:01:58.786:DEBUG [karma]: Run complete, exiting.
PhantomJS 1.9.8 (Mac OS X 0.0.0) ERROR
  TypeError: 'undefined' is not an object (evaluating 'b[0][0]')
  at /Users/tll01/Documents/way-finder/Website/static/js/python-models.js:4
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 3 of 3 ERROR (0.005 secs / 0.023 secs)
02 09 2015 15:01:58.788:DEBUG [karma]: Run complete, exiting.
02 09 2015 15:01:58.788:DEBUG [launcher]: Disconnecting all browsers
02 09 2015 15:01:58.796:DEBUG [launcher]: Process PhantomJS exited with code 0
02 09 2015 15:01:58.797:DEBUG [temp-dir]: Cleaning temp dir /var/folders/3d/j75hlwl97r37nsdgn72ypq28dp4wp2/T/karma-61237386

为了澄清,b[0][0]是javascript文件中的一个对象。我感到困惑的是--single-run标志实际上做了什么。我的假设是它只是在测试运行后关闭服务器。为什么它会影响测试的实际运行?这是karma.conf.js文件:

// Karma configuration
// Generated on Wed Aug 26 2015 11:39:13 GMT+0100 (BST)

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'],

// list of files / patterns to load in the browser
files: [
  'Website/static/js/leaflet/leaflet.js',
  'Website/static/js/leaflet/leaflet.draw.js',
  'Website/static/js/jquery/jquery-2.1.4.js',
  'Website/static/js/wayfinder.js',
  'Website/static/js/shared/floorcontroller.js',
  'Website/static/js/tests/*.js',
  'Website/static/js/python-models.js',
  'Website/static/js/leaflet/leaflet.distortableimage.js'
],


// 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 results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
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_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
})
}

我的单元测试:

describe("Floor controller", function() {
    var div;

    beforeEach(function(){
        div = $("<div id='map'></div>");
        $(document.body).append(div);
        floorplan0 = new floorplan(['img.png', 0, 3]);
        floorplan1 = new floorplan(['img.png', 0, 1]);
        floorplan2 = new floorplan(['img.png', 0, 2]);
        floorController = new floorControls([0,1,2,3], { position: 'bottomright'});
        floorplans = [floorplan0, floorplan1, floorplan2];
        building1 = new building([[[2,2], [1,1]], 'building'])
        buildings = [building1];
        floorController.onAdd();
        loadMap();
    });

    it("should be initialised", function() {
        expect(floorController._level).toEqual(0);

    });

    it("should return the correct button labels", function() {
        var levels = [-1, 0, 1];
        levels = floorController.assignFloorLabels(levels);
        expect(levels).toEqual(
            [
                {num: -1, label: 'B'},
                {num: 0, label: 'G'},
                {num: 1, label: 1}
            ]

            );
    });

    it("should set the correct level", function() {
        floorController.setLevel(1);
        expect(floorController.getLevel()).toEqual(1);
    });

//    it("should remove layers when switching levels", function() {
//        floorController.setLevel(1);
//        expect(imageOverlays).toEqual([]);
//        floorController.setLevel(3);
//         expect(imageOverlays).toEqual([]);
//    });

    afterEach(function(){
        div.remove();
        div = null;
    });
});

以及python-models.js(虽然我怀疑这是错误的地方,因为这个代码在我们的项目中反复使用而没有问题):

function building(b) {
    this.name = b[1];
    this.bounds = new L.LatLngBounds(
        new L.LatLng(b[0][0][0], b[0][0][1]),
        new L.LatLng(b[0][1][0], b[0][1][1])
    );
}

function floorplan(f) {
    this.imageUrl = f[0];
    this.buildingIndex = f[1];
    this.floor = f[2];

    this.isOn = function(level) {
        return this.floor === level;
    }
}

function node(n) {
    this.name = n[0];
    this.coords = new L.LatLng(n[1][0], n[1][1]);
    this.floorNumber = n[2];
    this.type = n[3];
}

2 个答案:

答案 0 :(得分:1)

最后通过在文件数组中添加测试文件来加载测试文件。

使用--single-run flag运行时也添加--no-auto-watch。

另一个问题是您的测试运行了两次。如果在添加--no-auto-watch后仍然存在此问题,请检查此帖子https://medium.com/@SchizoDuckie/so-your-karma-tests-run-twice-this-is-what-you-need-to-do-be74ce9f257e。它基本上是要添加{pattern:&#39; tests / .js&#39;,包括:false}而不是&#39; tests / .js&#39;

答案 1 :(得分:0)

尝试在Karma配置文件本身中将“singleRun”更改为true,而不是在命令中设置标志。

 // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: true