我试图让茉莉花测试在Karma中运行Aurelia项目,该项目需要自定义配置启动配置。 这是测试
it("dreams to be a plugin", async () => {
const component = StageComponent.withResources();
component.configure = (aurelia) => {
return aurelia.use.standardConfiguration();
};
await component.create(bootstrap);
expect(true).toBeTruthy();
});
不幸的是测试失败了,因为对component.create()
的调用没有返回,并且jasmine超时并且测试失败。
Here是一个显示问题的回购。
我错过了什么?
答案 0 :(得分:0)
component.create()
会返回Promise
吗? async
/ await
如果是Promise
,则应该有效。
it("should support async/await tests", async () => {
let flag = false;
flag = await asyncSetFlagToTrue();
expect(flag).toBeTruthy();
});
function asyncSetFlagToTrue() {
return new Promise(resolve => {
setTimeout(() => {
resolve(true);
}, 1000);
});
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.0.0/jasmine.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.0.0/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.0.0/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.0.0/boot.min.js"></script>
<强>更新强>
我从GitHub克隆并向component-tester.spec.ts
添加了两个测试。两人都过去了。
it('should test async call using done', (done) => {
component.configure = (aurelia) => {
return aurelia.use.standardConfiguration();
};
component.create(bootstrap).then(() => done());
expect(true).toBeTruthy();
});
it('should test async call using async/await', async () => {
component.configure = (aurelia) => {
return aurelia.use.standardConfiguration();
};
await component.create(bootstrap);
expect(true).toBeTruthy();
});