3

I need a little help understanding QUnit internas. I read its source from time to time, but i'm still writing weird test when it comes to asynchronous tests. I understand the concept of asynchronous tests, and the stop() and start() methods (and why they are needed), but when i combine them with setup and teardown i get a lot of weired situations.

Here is my Testcode:

use(['Psc.Exception','Psc.Code'], function () {
  module("async", {
    setup: function () {
      console.log('setup');
    }, teardown: function () {
      console.log('teardown');
    }
  });

  asyncTest("test1", function () {
    expect(0);

    console.log('test1');
    start();
  });

  asyncTest("test2", function () {
    expect(0);

     console.log('test2');
     start();
  });

  asyncTest("test3", function () {
    expect(0);

    console.log('test3');
    start();
  });

  asyncTest("test4", function () {
    expect(0);

    console.log('test4');
    start();
  });

  asyncTest("test5", function () {
    expect(0);

    console.log('test5');
    start();
  });
});

Allthough these are asynchron tests, i thought i would get something like this in the console:

setup
test1
teardown
setup
test2
teardown
setup
test3
teardown
...

because i thought qunit would call setup and teardown around the test bodys?

but I get everything mixed up, from request to request in another way shuffled.

setup
test1
teardown
setup
setup
setup
setup
test5
teardown 
test4
teardown
test3
teardown
test2
teardown

is someone able to explain it step by step?

pscheit
  • 2,882
  • 27
  • 29
  • 1
    okay, so I stopped trying to understand the issue. I just came to the solution that the setup function is useless if this is called from qunit this way. Because "global" test variables will always be overriden. So i started to include my setup functions into the test itself, which works really well – pscheit Jun 24 '12 at 08:55
  • 1
    Max Cantor believes you may have found a bug in QUnit. You can find his bug report and fix for this on GitHub: https://github.com/jquery/qunit/pull/320 – Troy J. Farrell Sep 20 '12 at 13:53
  • There's still something buggy about setup/teardown with `asyncTest` functions; even with the "fix". In my tests, it seems better but it eventually hiccups and makes multiple calls to setup in succession. Perhaps, if I debugged the qunit code, I'd get a better understanding of the issue but I just don't feel like I should have to do that. Instead, as @P.scheit mentioned, I think I'll be pitching my dependencies on modular setup/teardown. Possibly QUnit altogether... – Vinney Kelly Aug 07 '14 at 16:16

1 Answers1

5

It was a documented issue:

http://api.qunitjs.com/QUnit.config/

its is recommended to set QUnit.config.autostart to false, when loading tests asynchronously. This is my case because "use" is doing it asynchronously.

The head looks like this:

QUnit.config.autostart = false;
use(['Psc.Exception','Psc.Code'], function () {

  QUnit.start();
  module("async", {

So its basically like doing stop() and start() but for loading the tests itself. I tested it and the teardown / setup / test now get correctly executed in the right order

Vinney Kelly
  • 4,975
  • 1
  • 25
  • 31
pscheit
  • 2,882
  • 27
  • 29
  • I'm doing this and the process just closes without running any tests: ```JavaScript QUnit.config.autostart = false; var tests = require("./tests.js"); tests.setup().then(() => { console.log("start..."); QUnit.start(); }); ``` – panzi Aug 18 '21 at 18:14