【问题标题】:Testing async with karma用业力测试异步
【发布时间】:2015-01-18 07:07:41
【问题描述】:

我正在尝试使用 karma 和 jasmine 设置一些异步测试。我显然犯了一个非常愚蠢的错误,但我需要它向我指出。尽可能简化后,我有以下内容:

package.json

{
  "name": "newtest",
  "version": "0.0.0",
  "scripts": {
    "test": "karma start karma.conf.js"
  },
  "devDependencies": {
    "karma": "^0.12.28",
    "karma-chrome-launcher": "^0.1.5",
    "karma-jasmine": "^0.2"
  }
}

karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      'tests/**/*.js'
    ],
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['Chrome'],
    singleRun: true
  });
};

测试/dummy.spec.js

describe("Testing async", function() {
  it('should fail this test', function(done) {
    setTimeout(function(){
      expect(1).toBe(2);
      done();
    }, 1000);
  });
  it('should not fail this test', function(done) {
    done();
  });
});

我得到以下信息:

npm test
> newtest@0.0.0 test /home/mark/Projects/newtest
> karma start karma.conf.js
INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 39.0.2171 (Linux)]: Connected on socket T7j6LvNAwvS89wUdymCb with id 16891024
Chrome 39.0.2171 (Linux) Testing async should not fail this test FAILED
    TypeError: undefined is not a function
        at null.<anonymous> (/home/mark/Projects/newtest/tests/dummy.spec.js:12:5)
Chrome 39.0.2171 (Linux): Executed 2 of 2 (1 FAILED) (0.007 secs / 0.005 secs)
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

所以我认为应该失败的测试通过了,反之亦然。有人可以指出我的错误吗?

【问题讨论】:

标签: karma-runner karma-jasmine


【解决方案1】:

我猜第一个失败是因为当超时到达时,测试已经完成,所以两者都不起作用。

是你没有使用 jasmine 2。

语法对我来说似乎不错,我可以告诉你的是与我的配置的不同(这是有效的):

我已将 karma-jasmine 与 ~ 相关联:

"dependencies": {
...
"karma-jasmine": "~0.2.0"
},

我正在使用 PhantomJS:

browsers:['PhantomJS'],

【讨论】:

  • 谢谢。我确实尝试了它们(尽管我应该已经在 J​​asmine 2 上并且浏览器不应该有所作为)但我得到了完全相同的结果。
【解决方案2】:

我不知道为什么会出现问题,但我已经在另一台机器上尝试过,它按预期工作。

【讨论】:

  • 我不知道这是否可行,但也许你有多个版本的 jasmine install ? (本地和全球?)
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
相关资源
最近更新 更多