Skip to content

Instantly share code, notes, and snippets.

@trentmwillis
Created August 28, 2015 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trentmwillis/b23306cccda878a567fa to your computer and use it in GitHub Desktop.
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.
// 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