lundi 20 juillet 2020

Jest with async function calls before and after setTimeout

I have a main function which calls two async functions with sleep function in between. This is a basic example:

index.js

const func1 = async() => {
    setTimeout(()=>{console.log('func 1...')}, 1000);
}

const func2 = async() => {
    setTimeout(()=>{console.log('func 2...')}, 1000);
}

const sleep = ms => {
    console.log(`Sleeping for ${ms/1000} seconds`);
    return new Promise(resolve => {
        setTimeout(resolve, ms);
    })
}

const main = async() => {
    try {
    await func1();
    // Sleeping for a long long time
    console.log('Before Sleep');
    await sleep(2000000);
    console.log('After Sleep')
    await func2();
    return 'success';
    } catch(err) {
        console.log(err);
        return 'error'
    }
}

And this is my test code:

index.test.js

const index = require('./index');

jest.useFakeTimers();

describe('Testing index.js...', () => {
    test('Should return success', async() => {
        const promise = index();
        jest.advanceTimersByTime(2000000);
        promise.then(response => {
            expect(response).toBe('success');
        }) 
    });
})

The test passes, but the console shows the following:

func 1...
Before Sleep
Sleeping for 2000 seconds

I tried the same this, but with func1() and func2() being synchronous functions:

const func1 = () => {
    console.log('func 1...');
}

const func2 = () => {
    console.log('func 2...');

}

const sleep = ms => {
    // Sleeping for a long time
    console.log(`Sleeping for ${ms/1000} seconds`);
    return new Promise(resolve => {
        setTimeout(resolve, ms);
    })
}

const main = async() => {
    try {
    func1();
    // Sleeping for a long long time
    console.log('Before Sleep');
    await sleep(2000000);
    console.log('After Sleep')
    func2();
    return 'success';
    } catch(err) {
        console.log(err);
        return 'error'
    }
}

In that case, the test passes and the logs are also as expected:

func 1...
Before Sleep
Sleeping for 2000 seconds
After Sleep
func 2...

In the same synchronous code, if I make func1 async (keeping func2 synchronous), the problem reappears. If func1 is synchronous and func2 is async, everything works as expected.

How can I make this work? I have gone through and tried a solutions on lot of Github issues in jest, and also a lot of questions on stack overflow, none of the solutions seem to work.

I have also tried using jest.runAllTimers() and jest.runOnlyPendingTimers(). I have also tried using async-await in the test file, but that (understandably) gives a timeout error.

How can I make this work? I am using jest 25.5.4

Edit: I also tried increasing the value in jest.advanceTimersBytTime() to a day. And also tried making the function in describe async.

Aucun commentaire:

Enregistrer un commentaire