wct-mocha
Version:
Client-side library for testing web-components with Mocha.
71 lines • 3.31 kB
JavaScript
/**
* @license
* Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
Object.defineProperty(exports, "__esModule", { value: true });
var interfaceExtensions = [];
/**
* Registers an extension that extends the global `Mocha` implementation
* with new helper methods. These helper methods will be added to the `window`
* when tests run for both BDD and TDD interfaces.
*/
function extendInterfaces(helperName, helperFactory) {
interfaceExtensions.push(function () {
var Mocha = window.Mocha;
// For all Mocha interfaces (probably just TDD and BDD):
Object.keys(Mocha.interfaces).forEach(function (interfaceName) {
if (interfaceName !== 'tdd' && interfaceName !== 'bdd') {
return;
}
// This is the original callback that defines the interface (TDD or
// BDD):
var originalInterface = Mocha.interfaces[interfaceName];
// This is the name of the "teardown" or "afterEach" property for the
// current interface:
var teardownProperty = interfaceName === 'tdd' ? 'teardown' : 'afterEach';
// The original callback is monkey patched with a new one that appends
// to the global context however we want it to:
Mocha.interfaces[interfaceName] = function (suite) {
// Call back to the original callback so that we get the base
// interface:
originalInterface.apply(this, arguments);
// Register a listener so that we can further extend the base
// interface:
suite.on('pre-require', function (context, _file, _mocha) {
// Capture a bound reference to the teardown function as a
// convenience:
var teardown = context[teardownProperty].bind(context);
// Add our new helper to the testing context. The helper is
// generated by a factory method that receives the context,
// the teardown function and the interface name and returns
// the new method to be added to that context:
// tslint:disable-next-line:no-any
context[helperName] =
helperFactory(context, teardown, interfaceName);
});
};
});
});
}
exports.extendInterfaces = extendInterfaces;
/**
* Applies any registered interface extensions. The extensions will be applied
* as many times as this function is called, so don't call it more than once.
*/
function applyExtensions() {
interfaceExtensions.forEach(function (applyExtension) {
applyExtension();
});
}
exports.applyExtensions = applyExtensions;
//# sourceMappingURL=extend.js.map
;