Search code examples
javascriptjqueryqunit

How to reset status in QUnit?


Is there a way of resetting QUnit tests without reloading the page ?

On the documentation it says that QUnit.init( ) reinitializes if it was already initialized, but this doesn't seem to work. I get a message "Running..." where the result should be.

http://docs.jquery.com/QUnit


Solution

  • Wrap all your tests inside a function.

    Call the function when you're ready for the test to run.

    If you want to rerun the test after QUnit has finished, then make and call a function like 'rerunQUnitTest'.

    var runAllTest = function(){
        test( "test 1", function(){
            var a = true;
            equal( a, true, "function a should return false" );
        });
    };
    // call rerunQUnitTest to reset and run your tests again.
    var rerunQUnitTest = function(){
        QUnit.reset();  // should clear the DOM
        QUnit.init();   // resets the qunit test environment
        QUnit.start();  // allows for the new test to be captured.
        runAllTest();   // runs your functions that has all the test wrapped inside.
    };
    runAllTest();   // auto runs your tests.