我想测试一个递归函数,它可以重试输入函数直到它是成功的(返回true)。
呼唤承诺,等待它的实现
如果返回值为正,则停止。否则,在减少重试计数的同时重试。
以下是实现:
type booleanPromiseFunc = (...args) => Promise<boolean>;
export async function retryIfFails(func: booleanPromiseFunc, retryCount: number)
{
let result = await func();
if (result)
r
我正在编写一个单元测试,测试一个等待超时的函数,然后再继续,我尝试使用
jest.useFakeTimers();
jest.advanceTimersByTime(20000);
但我一直收到一条错误信息:
: Timeout - Async callback was not invoked within the 20000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeou
我正在尝试使用React中的Jest在我的componentDidMount()方法上运行单元测试,而且只运行该方法。我的componentDidMount()如下: componentDidMount() {
//Delay to simulate welcome loading screen
const delay = 2000;
setTimeout(() => {
this.setState({ appLoaded: true }); //this line has no coverage, according to IDE
}, d
我已经创建了一个轮询服务,它递归地调用一个api,在api成功的情况下,如果满足了某些条件,就会再次继续轮询。
/**
* start a timer with the interval specified by the user || default interval
* we are using setTimeout and not setinterval because a slow back end server might take more time than our interval time and that would lead to
* a queue o
所以我试着为我的库写一些单元测试。但是当我在开玩笑的时候,我会感到恐惧:
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't
stopped in your tests. Consider running Jest with `--detectOpenHandles`
to troubleshoot this issue.
我将其追溯到我编写的具有静态构造函数块的特定类。
我有一个rateLimit函数(它只是的一个修改版本):
function rateLimit(func, wait) {
var timeout;
return function () {
var context = this;
var args = arguments;
var later = function () {
timeout = null;
func.apply(context, args);
};
clearTimeout(timeo
我正尝试在中设置一个函数来淡入或淡出页面上的某些元素。淡入淡出本身运行良好,但当其中一个试图取消另一个的操作时,我的问题就出现了。
//I know this is ugly, I'm just learning JavaScript again and plan to recode it after I learn some more...
var fadeOut = false
//set out to true to fade out, false to fade in
function fade(out) {
var steps = 1
var outSpee
我正在尝试迁移到的“现代”版本,它不是27.x版本中的默认版本。
我的测试有一些问题,因为Jest一直说像clearTimeout和setInterval这样的函数不是开玩笑的模仿:
// Poster.ts
// This is the class method I'm testing
startInterval(interval = 1800000) {
this._interval && clearTimeout(this._interval)
this._interval = (setInterval(
() =>
this