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

timeoutAfter() causes other tests to fail #313

Open
jampy opened this issue Aug 27, 2016 · 2 comments
Open

timeoutAfter() causes other tests to fail #313

jampy opened this issue Aug 27, 2016 · 2 comments

Comments

@jampy
Copy link

jampy commented Aug 27, 2016

timeoutAfter() makes sure that the tests failes after a certain amount of time, unless end() has been called.

However, due to the nature of asynchronous JavaScript, the test still continues to run and may still call end() later. This case is not handled and, even worse, makes successive tests fail due to the unexpected end() call.

Example:

var test = require("tape");

test("takes too long", function(t) {
  // remove/comment-out the following line, and all tests will pass:
  t.timeoutAfter(2000);

  setTimeout(function() {
    // we're done... but let's say for some reason later than expected...
    t.end();
  }, 5000);

});

test("independent test", function(t) {
  t.end();
});

Output:

TAP version 13
# takes too long
not ok 1 test timed out after 2000ms
  ---
    operator: fail
  ...
# independent test
not ok 2 .end() called twice
  ---
    operator: fail
    at: Timeout._onTimeout (/src-protected/tonidb/packages/tonidb/test/collections.test.js:11:7)
  ...

1..2
# tests 2
# pass  0

...or am I using timeoutAfter() incorrectly?

In any case, it surprises me that one test can make another fail. Consider another example:

test("something goes wrong", function(t) {
  t.plan(2);

  [1, 2, 3].forEach(function(value) {
    // let's cause one end() too much..
    setTimeout(function() {
      t.equals(1, 1);
    }, value * 1000);
  });

});


test("dummy test 1", function(t) { t.end(); });

test("dummy test 2", function(t) { t.end(); });

test("dummy test 3", function(t) { t.end(); });

test("last test...", function(t) { t.end(); });

...this shows the last test as failed, even if the problem is in the first test. Since the t is scoped, I'm sure that tape could detect that the superfluous end() was from the first test.

@themicp
Copy link

themicp commented Feb 8, 2017

+1

@ljharb
Copy link
Collaborator

ljharb commented Dec 27, 2019

This seems like a duplicate of #264?

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