Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting "Do not return promises from tests declared via test.cb" error #824

Closed
gajus opened this issue May 10, 2016 · 1 comment
Closed

Comments

@gajus
Copy link

gajus commented May 10, 2016

$ ava --version
0.14.0

I am getting an error:

✖ createHttpProxyServer › proxies HTTP POST request failed with "Do not return promises from tests declared via test.cb(...), > if you want to return a promise simply declare the test via test(...)"
Unhandled Rejection: tests/createHttpProxyServer.js
TypeError: proxyServer is not a function
resolve (/Users/gajus/Documents/dev/applaudience/forward-proxy/src/createHttpProxyServer.js:72:9)
exports.default.options (/Users/gajus/Documents/dev/applaudience/forward-proxy/src/createHttpProxyServer.js:56:12)
Test. (createHttpProxyServer.js:67:5)
Test._ava2.default.cb as fn

I understand what the error is telling me. I don't understand what is this limitation for?

Consider this test:

test.cb('proxies HTTP POST request', async (t) => {
    t.plan(1);

    const handleRequest = async (requestDefinition) => {
        t.deepEqual(requestDefinition, {
            headers: {
                'content-length': '13',
                connection: 'close',
                host: 'gajus.com'
            },
            method: 'post',
            payload: 'Hello, World!',
            url: 'http://gajus.com/'
        });

        return createHttpResponse();
    };

    const resolvedServer = await createHttpProxyServer({
        handleRequest,
        port: 8000
    });

    await request('http://gajus.com', {
        ...defaultRequestOptions,
        method: 'post',
        body: 'Hello, World!'
    });

    resolvedServer.destroy();

    t.end();
});

As it is, ava cannot execute this test. I need to rewrite it to:

test.cb('proxies HTTP POST request', (t) => {
    t.plan(1);

    const handleRequest = async (requestDefinition) => {
        t.deepEqual(requestDefinition, {
            headers: {
                'content-length': '13',
                connection: 'close',
                host: 'gajus.com'
            },
            method: 'post',
            payload: 'Hello, World!',
            url: 'http://gajus.com/'
        });

        return createHttpResponse();
    };

    createHttpProxyServer({
        handleRequest,
        port: 8000
    })
        .then((resolvedServer) => {
            console.log('Hello!');

            return request('http://gajus.com', {
                ...defaultRequestOptions,
                method: 'post',
                body: 'Hello, World!'
            })
                .then(() => {
                    resolvedServer.destroy();

                    t.end();
                });
        });
});

What is the reason for the limitation?

@jamestalmage
Copy link
Contributor

You are using t.end incorrectly. Don't use it as a timeout method (don't use it at all here). Avoid test.cb when returning promises.

The reason is because AVA needs to know when to end the test. You can defer ending a test by returning a promise, or using test.cb / t.end. We don't allow both because that makes the end of the test ambiguous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants