首页 > 解决方案 > 当测试成功完成后没有当前规范时使用“期望”

问题描述

现在花了一整天的时间试图让异步测试工作。对于各种测试,我需要调用 whenStable(),等待它然后执行测试。一旦我尝试使用任何承诺,测试就会通过,全部通过,然后它在最后暂停几秒钟,我得到错误:“未捕获的错误:在没有当前规范时使用了'expect',这可能是因为异步测试超时”

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

如果这是我进行的唯一异步测试,并且我将其注释掉,则测试将通过(全部通过)并且最后我没有收到任何错误。一旦我添加它,它们就会运行(全部通过),然后我在最后得到错误。

业力.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",

标签: angularasynchronousangular6karma-jasmine

解决方案


推荐阅读