Skip to content

Prevent accidentally doing async operations in sync tests #122

@sindresorhus

Description

@sindresorhus

Would be good to have a rule to prevent people from mistakenly doing async operations in a sync test test().

A common pitfall is doing:

test(t => {
    fetch().then(data => {
        t.is(data, 'foo');
    });
});

Here they forgot to return the promise, so AVA treats it as a synchronous test.

It should have been:

test(t => {
    return fetch().then(data => {
        t.is(data, 'foo');
    });
});

We could look for assertions inside .then() that isn't returned. Might need code path analysis for less obvious cases.

We could potentially detect incorrect usage of callbacks here too:

test(t => {
    fetch((err, data) => {
        t.is(data, 'bar');
    });
});

Here we could detect that there are assertions inside a callback with a err argument first.

This should have been:

test.cb(t => {
    fetch((err, data) => {
        t.is(data, 'bar');
        t.end();
    });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions