puppeteer701
puppeteer701

Reputation: 1285

QUnit Multiple asyncTest with setTimeout

What should happen is, that when add1 is complete, then add2 should be called, and then add3, but with this code it does not work.

Add2 gets called to fast, and the same goes for add3.

asyncTest('add1', function () {
  setTimeout(function () {
     test('add1 t1', function () {
         ok(1==1, 'ok');
     });
     QUnit.start();
  }, 500);
});

asyncTest('add2', function () {
  setTimeout(function () {
     test('add2 t1', function () {
         ok(2==2, 'ok');
     });
     QUnit.start();
  }, 500);
});

asyncTest('add3', function () {
  setTimeout(function () {
     test('add3 t1', function () {
         ok(3==3, 'ok');
     });
     QUnit.start();
  }, 500);
});

Upvotes: 0

Views: 282

Answers (2)

Joel Box
Joel Box

Reputation: 439

Current version of QUnit has done = assert.async();.

QUnit.test("method Debug.numberOne is called", function (assert) {
  var done = assert.async(); // Pause the test
  localStorage.setItem('uid', '1');
  function checkNumberOne() {
     assert.ok(numberOneIsCalled, " detected user is development user #1");
     done();
  }
  setTimeout(checkNumberOne, 6000);
});

Perhaps chaining the result callbacks will help creating a synchronous behaviour.

Upvotes: 0

Jordan Kasper
Jordan Kasper

Reputation: 13273

There's a few things to mention here. First of all, I'm assuming that you are actually using the ok() assertion... there is no test() assertion. That said, when I run the code you have above, but with the ok() assertion, the tests do run in order. However, in general you should not be writing tests that depend on one another. This can cause all sorts of problems including race conditions, environment pollution, and others. You should try to write tests that are idempotent.

QUnit does run tests in the order they are defined, with one exception: on reruns of test files with failing tests the library will rerun failed tests first. You can disable this with the reorder config option.

Basically, the answer here is that the code you have (except for ok() versustest()`) works just fine for me: the tests run in order.

Upvotes: 1

Related Questions