javascript - 在 jasmine 单元测试中使用嵌套描述语句时,为什么 karma 或 Webstorm 会崩溃?错误: Cannot read property * of undefined?

标签 javascript unit-testing jasmine webstorm karma-runner

我正在使用 WebStorm 结合 karma 来运行 Jasmine javaScript 单元测试。 如果我嵌套了 describe 语句,那么 karma 就会崩溃,或者可能是 WebStorm 崩溃,我不确定。

karma 错误输出显示:

ERROR [karma]: [TypeError: Cannot read property 'should return a Foo' of undefined]

  • 如果我直接在 Jasmine SpecRunner.html 中运行测试,一切都很好并且测试通过。
  • 如果我删除嵌套的 describe 语句,那么一切都会正常运行。

版本:

  • OS X:10.9 (13A603)
  • WebStorm:7.0.2
  • karma :0.10.8
  • karma Jasmine :0.1.4
  • jasmine.js:jasmine-1.3.1

是什么导致它崩溃?

代码和测试:

var Foo = function Foo(name) {

    this.name = name;

};

Foo.prototype.bar = function bar() {

    return "Hello " + this.name;

};


describe("Foo", function () {


   describe("constructor", function () {


        it("should return a Foo", function () {

            var act,
                target;

            act = function () {
                target = new Foo("Gary");
            };

            expect(act).not.toThrow();

            expect(target).not.toBeNull();


        });


    });



   describe("bar", function () {

        it("should return a string like 'Hello' + name", function () {

            var target, actValue;

            target = new Foo("Gary");

            actValue = target.bar();

            expect(actValue).toBe("Hello Gary");


        });

    });


});

karma 错误输出:

/usr/local/bin/node /Applications/WebStorm.app/plugins/js-karma/js_reporter/karma-intellij/lib/intellijServer.js --karmaPackageDir=/usr/local/lib/node_modules/karma --configFile=/Users/Gary/Documents/DotBox/test/karma.conf.js --coverageTempDir=/private/var/folders/5m/hfxftkwx5fsdnxf3g3nn5_2m0000gn/T/karma-intellij-coverage-4959093687844631591.tmp
INFO [karma]: Karma v0.10.8 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 31.0.1650 (Mac OS X 10.9.0)]: Connected on socket vWDSeE6uTpA6RpuH05Bq
INFO [watcher]: Changed file "/Users/Gary/Documents/DotBox/test/fooSpec.js".
ERROR [karma]: [TypeError: Cannot read property 'should return a Foo' of undefined]
TypeError: Cannot read property 'should return a Foo' of undefined
    at createSpecNode (/Applications/WebStorm.app/plugins/js-karma/js_reporter/karma-intellij/lib/intellijReporter.js:51:37)
    at IntellijReporter.onSpecComplete (/Applications/WebStorm.app/plugins/js-karma/js_reporter/karma-intellij/lib/intellijReporter.js:190:20)
    at EventEmitter.emit (events.js:98:17)
    at onResult (/usr/local/lib/node_modules/karma/lib/browser.js:177:13)
    at Socket.EventEmitter.emit [as $emit] (events.js:117:20)
    at SocketNamespace.handlePacket (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/namespace.js:335:22)
    at Manager.onClientMessage (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/manager.js:488:38)
    at WebSocket.Transport.onMessage (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transport.js:387:20)
    at Parser.<anonymous> (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:39:10)
    at Parser.EventEmitter.emit (events.js:95:17)
    at finish (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:288:16)
    at Parser.expectHandler (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:299:15)
    at Parser.add (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:466:24)
    at Parser.expect (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:499:10)
    at Parser.<anonymous> (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:298:18)
    at Parser.add (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:466:24)
    at Parser.expect (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:499:10)
    at expectData (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:296:16)
    at Parser.<anonymous> (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:317:11)
    at Parser.add (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:466:24)
    at Parser.expect (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:499:10)
    at Parser.opcodeHandlers.1 (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:316:14)
    at Parser.processPacket (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:533:8)
    at Parser.add (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:466:24)
    at Socket.<anonymous> (/usr/local/lib/node_modules/karma/node_modules/socket.io/lib/transports/websocket/hybi-16.js:141:17)
    at Socket.EventEmitter.emit (events.js:117:20)
    at Socket.<anonymous> (_stream_readable.js:746:14)
    at Socket.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:408:10)
    at emitReadable (_stream_readable.js:404:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at Socket.Readable.push (_stream_readable.js:127:10)
    at TCP.onread (net.js:526:21)

Process finished with exit code 1

SpecRunner 输出

SpecRunner Output

最佳答案

看起来这是 7.0.2 版本的 WebStorm 中的一个问题,特别是旧的 Karma 插件。 修复方法是下载新的 karma 插件版本 131.560。或者,当 WebStorm 的下一个版本 7.0.3 发布时,它可能会有新的插件。

请参阅此处的一些文档:

http://youtrack.jetbrains.com/issue/WEB-10010

https://github.com/karma-runner/karma-intellij/commit/589062a9500d889a8d328ff91cc185e6ec1fd127

关于javascript - 在 jasmine 单元测试中使用嵌套描述语句时,为什么 karma 或 Webstorm 会崩溃?错误: Cannot read property * of undefined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20458547/

相关文章:

angularjs - 如何使用 Jasmine 监视 Angular promise 链

AngularJS + Testaulous/Jasmine 单元测试 : Executed 0 of 0 SUCCESS

javascript - 从 .toggle 访问 $(this)

javascript - 使用 JavaScript/jQuery 移动表单中的输入字段位置

java - 如何在单元测试期间将资源文件路径交换为测试文件?

c# - 使用 Asp.Net Boilerplate 进行单元测试时获取新的 DbContext

javascript - Rails 4/ajax - NoMethodError - 未定义方法 `id' for nil :NilClass - Passing variable to js modal

javascript - 如何在一些不同元素之间切换类?

spring - 使用Spring和Jersey测试框架进行单元测试

javascript - Jasmine 测试中的 stub 事件属性