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

Test using async/await does not end #358

Closed
mkermani144 opened this issue Apr 14, 2017 · 7 comments
Closed

Test using async/await does not end #358

mkermani144 opened this issue Apr 14, 2017 · 7 comments

Comments

@mkermani144
Copy link

I looked at many issues in this repo, such as #222, #262, etc. but I still don't know why the following test does not end:

test('connection to database', async (assert) => {
  const res = await connectToDatabase(mongoose);
  assert.equal(res, 0, 'It should connect to the database without errors');
  assert.end();
});

when connectToDatabase is an async function.

When I run the test, I get the following result:

# connection to database
  ok 1 It should connect to the database without errors

but the test does not finish. Shouldn't assert.end() finish the test after promise resolution? What is the reason behind this behavior?

@ljharb
Copy link
Collaborator

ljharb commented Apr 14, 2017

You can't really pass an async function to tape because tape does not do anything with the promise it returns.

You might have slightly more success though using assert.plan(1) prior to the first await.

@mkermani144
Copy link
Author

@ljharb

You can't really pass an async function to tape because tape does not do anything with the promise it returns.

I don't understand. Why the test passes, but assert.end() does not work?

You might have slightly more success though using assert.plan(1) prior to the first await.

I tried that, but the problem didn't resolve.

@mkermani144
Copy link
Author

Furthermore, if I add more async tests, i.e.:

test('async test 1', async (assert) => {
  const res = await someAsyncCall();
  assert.equal(res, 0);
  assert.end();
});
test('async test 2', async (assert) => {
  const res = await anotherAsyncCall();
  assert.equal(res, 0);
  assert.end();
});
test('async test 3', async (assert) => {
  const res = await yetAnotherAsyncCall();
  assert.equal(res, 0);
  assert.end();
});

only the last one doesn't finish. The others pass and finish correctly.

@ljharb
Copy link
Collaborator

ljharb commented Apr 14, 2017

An async function is just sugar for a function that returns a promise.

Tape does not do anything special with promises.

Without a synchronous call to .plan(), tape can't know when your test is expected to end, so having multiple async tests like this can confuse it.

You need to only pass a function to tape, not an async function; unless you're willing to jump through all the hoops required, which includes calling .plan before the first await, in every test.

@mkermani144
Copy link
Author

mkermani144 commented Apr 14, 2017

@ljharb As I said, using .plan(1) doesn't help me. Even if I call .plan(1) before each of the awaits, I still get the same result.
I think, however, this issue should be closed, as async/await and Promises are not supported by Tape (currently).

@ljharb
Copy link
Collaborator

ljharb commented Apr 14, 2017

Ah - the likely issue is that one of your tests has a rejected promise, and that's causing the end never to be reached. This pattern should work:

test('async test 1', async (assert) => {
  assert.plan(1);
  try {
    const res = await someAsyncCall();
    assert.equal(res, 0);
    assert.end();
  } catch (e) {
    assert.fail(e);
  }
});

Using await forces you to use try/catch, or to .catch() on the promise that the async function returns.

@Download
Copy link

Tape supports async since version v5

This issue is about Tape v4, but since v5 Tape actually does support Promises / async/await.
Commenting here for future readers because it was confusing me.

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

3 participants