如果没有当前规范,则在成功完成测试后使用“期望”

时间:2019-04-01 13:22:53

标签: angular asynchronous angular6 karma-jasmine

现在几乎要炸一整天,以尝试进行异步测试。对于各种测试,我需要调用whenStable(),等待它,然后执行测试。当我尝试使用所有承诺时,测试全部通过,然后在最后暂停几秒钟,我得到一个错误:“未捕获的错误:在没有当前规范时使用了'期望',这可能是因为异步测试超时”

  it('should switch tabs', async(() => {
    const fixture = TestBed.createComponent(C2Component);
    const component = fixture.debugElement.componentInstance;
    fixture.whenStable().then(() => {
      expect(true).toBeTruthy();
    });
  }));

如果这是我唯一进行过的异步测试,并且我将其注释掉,则测试将通过(全部通过),最后我不会出错。一旦我添加了这些,它们就会(全部通过)运行,然后最后我得到错误消息。

karma.conf.js:

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

我尝试了这些不同版本的测试:

  it('should switch tabs', (done) => {
    const fixture = TestBed.createComponent(C2Component);
    const component = fixture.debugElement.componentInstance;
    fixture.whenStable().then(() => {
      expect(true).toBeTruthy();
      done();
    });
  });
  it('should switch tabs', async () => {
    const fixture = TestBed.createComponent(C2Component);
    const component = fixture.debugElement.componentInstance;
    await fixture.whenStable()
    expect(true).toBeTruthy();
  });

相关的软件包版本:

"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage-istanbul-reporter": "^2.0.4",
"karma-jasmine": "^1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"jasmine-core": "^2.99.1",
"jasmine-spec-reporter": "^4.2.1",

现在也尝试使用以下版本:

"jasmine-core": "^3.3.0",
"jasmine-spec-reporter": "^4.2.1",
"karma": "^4.0.1",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage-istanbul-reporter": "^2.0.5",
"karma-jasmine": "^2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",

0 个答案:

没有答案