-
Notifications
You must be signed in to change notification settings - Fork 781
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: Always define globalThis.QUnit
== Background == In QUnit 2.x, we always set the QUnit global in browser contexts, but outside browsers, we only set the global if CJS/AMD wasn't detected. Except in the QUnit CLI, where we do set the global anyway, because that's how most QUnit tests are written, incliuding for Node.js. So really the only case where the QUnit global is missing is custom test runners that: * run in Node.js, * and require/import qunit.js directly, * and don't export it as a global. I'm aware a growing proportion of developers import 'qunit' directly in each test file for improved type support. That's a great way to avoid needing to rely on globals. But, it's not a requirement, and is not always an option, especially for simple no-build-step and browser-facing projects. == Why == 1. Improve portability between test runners. Remove the last edge case where the QUnit global can be undefined. Make it QUnit's responsiblity to define this reliably, as indeed it almost always already does. Remove this as undocumented requirement for specific test runners to patch up on their end. In light of Karma deprecation, and emergence of more general purpose TAP runners, I'd like to remove this as factor that might make/break QUnit support in one of those. 2. Prepare for native ESM build. We don't natively support ESM exports yet, but once we do this will become a problem. To prevent split-brain problems with mixed use (e.g. in test registry and other state) standardise internally on which ever globalThis.QUnit was defined first, and then reliably export that to any importers. Ref #1551.
- Loading branch information
Showing
8 changed files
with
71 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,60 @@ | ||
import QUnit from './core'; | ||
/* global module, exports */ | ||
|
||
import core from './core'; | ||
import { initBrowser } from './browser/browser-runner'; | ||
import { window, document } from './globals'; | ||
import { globalThis, window, document } from './globals'; | ||
|
||
/** | ||
* Available exports: | ||
* | ||
* globalThis: | ||
* - browser (globalThis === window) | ||
* - Web Worker (globalThis === self) | ||
* - Node.js | ||
* - SpiderMonkey (mozjs) | ||
* - Rhino 7.14+ | ||
* - any other embedded JS engine | ||
* | ||
* CommonJS module.exports (commonjs2): | ||
* - Node.js | ||
* | ||
* CommonJS exports (commonjs, https://wiki.commonjs.org/wiki/Modules): | ||
* - Rhino | ||
*/ | ||
|
||
// If a real QUnit global was already defined, then replace our reference | ||
// with that one, and export that instead. Skip initBrowser() to avoid | ||
// doubling the user interface. | ||
// | ||
// Since QUnit 3.0, we no longer throw an error if QUnit is loaded twice. | ||
// This enables mixed use of ESM import and CJS require() in a project, | ||
// without split-brain problems for defining tests, setting config, registering | ||
// reporters, etc. | ||
// | ||
// Note that a placeholder QUnit global may exist for preconfiguration. | ||
// Such placeholder is recognised by not having QUnit.version (it should | ||
// only contain QUnit.config), and will be upgraded to the real QUnit. | ||
let QUnit; | ||
if (globalThis.QUnit && globalThis.QUnit.version) { | ||
QUnit = globalThis.QUnit; | ||
} else { | ||
QUnit = core; | ||
globalThis.QUnit = QUnit; | ||
|
||
if (window && document) { | ||
initBrowser(QUnit, window, document); | ||
} | ||
} | ||
|
||
// For Node.js | ||
if (typeof module !== 'undefined' && module && module.exports) { | ||
module.exports = QUnit; | ||
|
||
// For consistency with CommonJS environments' exports | ||
module.exports.QUnit = QUnit; | ||
} | ||
|
||
if (window && document) { | ||
initBrowser(QUnit, window, document); | ||
// For CommonJS with exports, but without module.exports, like Rhino | ||
if (typeof exports !== 'undefined' && exports) { | ||
exports.QUnit = QUnit; | ||
} |
This file contains 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
This file contains 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