Created
August 28, 2015 15:49
-
-
Save trentmwillis/b23306cccda878a567fa to your computer and use it in GitHub Desktop.
Adds `before` and `after` hooks to QUnit to enable one-time setup and teardown steps. Similar to Mocha.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add the `before` hook | |
QUnit.moduleStart(function() { | |
// QUnit is weird in that once a module starts, it is considered the `previousModule` and not the `currentModule`. | |
let previousModule = QUnit.config.previousModule; | |
let testEnvironment = previousModule && previousModule.testEnvironment; | |
let before = testEnvironment && testEnvironment.before; | |
// If the module has a `before` function, call it with the `testEnvironment` as the context. | |
if (before && typeof before === 'function') { | |
before.call(testEnvironment); | |
} | |
}); | |
// Add the `after` hook | |
QUnit.moduleDone(function() { | |
// QUnit is weird in that once a module starts, it is considered the `previousModule` and not the `currentModule`. | |
let previousModule = QUnit.config.previousModule; | |
let testEnvironment = previousModule && previousModule.testEnvironment; | |
let after = testEnvironment && testEnvironment.after; | |
// If the module has a `after` function, call it with the `testEnvironment` as the context. | |
if (after && typeof after === 'function') { | |
after.call(testEnvironment); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment