'How to work around the use of done in Jest tests

We have many tests with done(), similar to following pattern:

test('my desc', done => {
    const source = myValue;

    source.on('data', (e) => {
        if (condition) {
            expect(source.property).toBe('myEcpectation');
            done();
        }
    });

    source.methodeToTriggerDataEvent;
});

I want to change them like this:

test('my desc', () => {
    let checkIfEventWasCalled = false;
    const source = myValue;

    source.on('data', (e) => {
        if (condition) {
            expect(source.property).toBe('myEcpectation');
            checkIfEventWasCalled = true;
        }
    });

    source.methodeToTriggerDataEvent;
    expect(checkIfEventWasCalled).toBe(true);
});

The tests pass after my changes.

Now I wonder if I can really exchange done so easily. Am I missing something?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source