angular - 当测试成功完成后没有当前规范时使用“期望”
问题描述
现在花了一整天的时间试图让异步测试工作。对于各种测试,我需要调用 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",
解决方案
推荐阅读
- python - 如何杀死以 sudo 启动的 python 子进程?以前的 SO 答案离开僵尸
- bash - 在 bash 中将一个统计/数字与另一个统计/数字分开
- javascript - 如何添加axios代理?
- html - 一个元素没有父元素是什么意思?
- flutter - 如何在 Flutter 中创建全局 TextStyle?
- time - PartiQL 查询中的当前时间
- laravel - 如何从 href 链接传递 vue 数据值或 id 以加载新组件以显示此值或 id?
- css - 固定内容和滚动侧边栏,将网格转换为 flexbox
- python - python创建一个字典列表,每个元素按分钟分隔
- java - new Date 返回 Tomcat 启动日期