问题描述
大家好,
我对测试框架 Jasmine 很陌生。我们有一个 TS 项目设置,我正在尝试测试一个由 setTimeout 组成的函数,但它一直失败。我正在尝试使用 Clock
一个重要的点 我注意到我在将 webpack 配置更改为 ts-loader 后立即使用 babel-loader。测试用例不会失败。 (不知道为什么?♀️)。我已经检查了多次但没有运气。
更新 我想出了测试用例失败的原因,但我不知道为什么会发生这种情况。
babel 和 ts-loader 的配置是正确的我只是将 setTimeout 更改为 window.setTimeout(在 babel-loader 中)存储库,现在测试用例执行成功?♀️。这只是 Stack Overflow Link 的疯狂猜测。
我添加了setTimeout和window.setTimeout的console语句,发现两个函数的定义有很大的不同。 window.setTimeout 有一个定义(截图中的绿色圆圈)与我们安装 Jasmine.clock 后相同。 IE。 HERE 但是setTimeout定义(截图中的红圈)很不一样(下图)。
function (/* ...args */) {
return fn.apply(that,arguments);
}
我尝试在 ts-loader 存储库中做同样的事情,但这里的 setTimeout 和 window.setTimeout 定义是相同的。
代码:
hideToast() {
setTimeout(() => {
this.showToast = false;
},5000);
}
测试规范:
beforeEach(() => {
// Sometimes calling install() will fail for some reason,// but calling uninstall() first will make it work
jasmine.clock().uninstall();
jasmine.clock().install();
});
afterEach(() => {
jasmine.clock().uninstall();
});
it('should hide toast',() => {
const obj: Car = new Car();
obj.showToast = true; // This value should change after timeout
obj.hideToast(); // Call the component method that turns the showToast value as false
jasmine.clock().tick(5000);
expect(obj.showToast).toBeFalsy(); // Then executes this
});
任何建议都会有所帮助。
使用 babel-loader(Test case fail's) Screenshot(Branch = "main"):
https://github.com/dollysingh3192/ts-babel-template
使用 ts-loader(测试用例通过)截图(Branch = "tsloader"):
https://github.com/dollysingh3192/ts-babel-template/tree/tsloader
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)