Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
From time to time, we may need to write tests that modify the DOM.
Managing the cleanup of such operations between tests can be a genuine
pain, but thankfully QUnit has a solution to this problem in the form of
the #qunit-fixture:
<!DOCTYPE html><html><head><title>QUnit Test</title><linkrel="stylesheet"href="qunit.css"><scriptsrc="qunit.js"></script><scriptsrc="app.js"></script><scriptsrc="tests.js"></script></head><body><h1id="qunit-header">QUnit Test</h1><h2id="qunit-banner"></h2><divid="qunit-testrunner-toolbar"></div><h2id="qunit-userAgent"></h2><olid="qunit-tests"></ol><divid="qunit-fixture"></div></body></html>
We can either opt to place static markup in the fixture or just
insert/append any DOM elements we may need to it. QUnit will automatically
reset the innerHTML of the fixture
after each test to its original value. In case you’re using jQuery, it’s
useful to know that QUnit checks for its availability and will opt to use
$(el).html() instead, which will clean
up any jQuery event handlers too.