qunit: Remove module() monkey-patch and use QUnit.hooks instead.

Follows-up I4200c94eff, which removed the legacy setup/teardown aliases.
What remains can be reduced to a simple global QUnit.hooks call.

This removes the last monkey-patch and thus makes us fully compliant
and supported by upstream. In particular, this means it is now also
possible to use `QUnit.module.only` and `QUnit.module.skip` during
development, which are short-cuts to apply the `test.only` and `test.skip`
logic to all tests in a given module. This wasn't possibly earlier
due to our monkey-patch implicitly deleting that API. (Though,
when I wrote that monkey-patch ten years ago, these APIs didn't exist!)

Ref <https://api.qunitjs.com/QUnit/hooks/>.
Ref <https://api.qunitjs.com/QUnit/module/>.

Bug: T250045
Change-Id: Ic317e9613bfb224da5a200a5db2e33cbac0031f4
This commit is contained in:
Timo Tijhof 2022-05-15 15:34:20 +01:00
parent 257b6e9dcf
commit b64181a8c0

View file

@ -24,9 +24,12 @@
fixture.id = 'qunit-fixture';
document.body.appendChild( fixture );
// SinonJS
// Integrate SinonJS with QUnit
//
// - Add a Sinon sandbox to the test context that is automatically
// restored at the end of each test.
// - Forward sinon assertions to QUnit.
//
// Glue code for nicer integration with QUnit
// Inspired by http://sinonjs.org/releases/sinon-qunit-1.0.0.js
sinon.assert.fail = function ( msg ) {
QUnit.assert.true( false, msg );
@ -42,61 +45,15 @@
useFakeTimers: false,
useFakeServer: false
};
// Extend QUnit.module with:
// - Add a Sinon sandbox to the test context.
( function () {
var nested;
var orgModule = QUnit.module;
QUnit.module = function ( name, localEnv, executeNow ) {
if ( nested ) {
// In a nested module, don't re-add our hooks, QUnit does that already.
return orgModule.apply( this, arguments );
}
if ( arguments.length === 2 && typeof localEnv === 'function' ) {
executeNow = localEnv;
localEnv = undefined;
}
var orgExecute;
if ( executeNow ) {
// Wrap executeNow() so that we can detect nested modules
orgExecute = executeNow;
executeNow = function () {
var ret;
nested = true;
ret = orgExecute.apply( this, arguments );
nested = false;
return ret;
};
}
localEnv = localEnv || {};
var orgBeforeEach = localEnv.beforeEach;
var orgAfterEach = localEnv.afterEach;
localEnv.beforeEach = function () {
// Sinon sandbox
var config = sinon.getConfig( sinon.config );
config.injectInto = this;
sinon.sandbox.create( config );
if ( orgBeforeEach ) {
return orgBeforeEach.apply( this, arguments );
}
};
localEnv.afterEach = function () {
var ret;
if ( orgAfterEach ) {
ret = orgAfterEach.apply( this, arguments );
}
this.sandbox.verifyAndRestore();
return ret;
};
return orgModule( name, localEnv, executeNow );
};
}() );
QUnit.hooks.beforeEach( function () {
// Sinon sandbox
var config = sinon.getConfig( sinon.config );
config.injectInto = this;
sinon.sandbox.create( config );
} );
QUnit.hooks.afterEach( function () {
this.sandbox.verifyAndRestore();
} );
/**
* Ensure mw.config and other `mw` singleton state is prestine for each test.
@ -156,7 +113,7 @@
ajaxRequests.push( { xhr: jqXHR, options: ajaxOptions } );
}
return function ( localEnv ) {
return function newMwEnvironment( localEnv ) {
localEnv = localEnv || {};
var orgBeforeEach = localEnv.beforeEach;
@ -579,14 +536,6 @@
);
} );
// Regression test for 'this.sandbox undefined' error, fixed by
// ensuring Sinon create/restore is not re-run on inner module.
QUnit.module( 'testrunner-nested-test', function () {
QUnit.test( 'example', function ( assert ) {
assert.true( true, 'nested modules supported' );
} );
} );
var beforeEachRan = false;
QUnit.module( 'testrunner-nested-hooks', {
beforeEach: function () {