From 0befe2aafe913704db958c472ed6f2a37ec8caaf Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 28 Mar 2022 21:58:41 +0100 Subject: [PATCH] Core: Add support for preconfig to all environments This was previously limited to the browser environment because it looked for QUnit.config on the (optional) `window` global, rather than looking directly at the global object itself. Simplify the code by looking at globalThis instead of (effectively) globalThis.window, and as bonus this now also works in SpiderMonkey, Node.js (e.g. custom use without QUnit CLI), and other environments. --- docs/config/index.md | 24 ++++++++++++++++++++---- src/core/config.js | 15 ++++++++------- test/preconfigured.js | 25 +++++++++---------------- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index bb02c636b..33dd7d8da 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -11,14 +11,30 @@ General configuration options for QUnit. ## Preconfiguring QUnit -If you load QUnit asynchronously or otherwise need to configure QUnit before it is loaded, you can define the global variable `QUnit` with a `config` property. Any other properties of the QUnit object will be ignored. The config values specified here will be carried over to the real `QUnit.config` object. +If you load QUnit asynchronously or otherwise need to configure QUnit before it is loaded, you can predefine the configuration by creating a global variable `QUnit` with a `config` property. + +The config values specified here will be carried over to the real `QUnit.config` object. Any other properties of this object will be ignored. ```js -// Before QUnit is loaded -window.QUnit = { +// Implicit global +// Supported everywhere, including old browsers. (But not ES strict mode.) +QUnit = { config: { autostart: false, - noglobals: true + maxDepth: 12 } }; + +// Browser global +// For all browsers (including strict mode and old browsers) +window.QUnit = { /* .. */ }; + +// Isomorphic global +// For modern browsers, SpiderMonkey, and Node.js (incl. strict mode). +globalThis.QUnit = { /* .. */ }; ``` + +### Changelog + +| UNRELEASED | Preconfig support added for SpiderMonkey and other environments.
Previously, it was limited to the browser environment. +| [QUnit 2.1.0](https://github.com/qunitjs/qunit/releases/tag/2.1.0) | Preconfig feature introduced. diff --git a/src/core/config.js b/src/core/config.js index 0570177cc..1e095451a 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -1,4 +1,4 @@ -import { window, localSessionStorage } from '../globals'; +import { globalThis, localSessionStorage } from '../globals'; import { extend } from './utilities'; /** @@ -103,12 +103,13 @@ const config = { storage: localSessionStorage }; -// take a predefined QUnit.config and extend the defaults -const globalConfig = window && window.QUnit && window.QUnit.config; - -// only extend the global config if there is no QUnit overload -if (window && window.QUnit && !window.QUnit.version) { - extend(config, globalConfig); +// Apply a predefined QUnit.config object +// +// Ignore QUnit.config if it is a QUnit distribution instead of preconfig. +// That means QUnit was loaded twice! (Use the same approach as export.js) +const preConfig = globalThis && globalThis.QUnit && !globalThis.QUnit.version && globalThis.QUnit.config; +if (preConfig) { + extend(config, preConfig); } // Push a loose unnamed module to the modules collection diff --git a/test/preconfigured.js b/test/preconfigured.js index 096d8ff1c..cdec93768 100644 --- a/test/preconfigured.js +++ b/test/preconfigured.js @@ -1,21 +1,14 @@ /* eslint-env browser */ -window.addEventListener('load', function () { - // make sure QUnit has started if autostart would be true - setTimeout(function () { - QUnit.module('QUnit.preconfigured.asyncTests'); - QUnit.test('QUnit.config should have an expected default', function (assert) { - assert.true(QUnit.config.scrolltop, 'The scrolltop default is true'); - }); +QUnit.module('QUnit.config [preconfigured]'); - QUnit.test('Qunit.config.reorder default was overwritten', function (assert) { - assert.false(QUnit.config.reorder, 'reorder was overwritten'); - }); - - QUnit.start(); - }, 100); +QUnit.test('config', function (assert) { + assert.strictEqual(QUnit.config.maxDepth, 5, 'maxDepth default'); + assert.false(QUnit.config.autostart, 'autostart override'); + assert.false(QUnit.config.reorder, 'reorder override'); }); -QUnit.module('QUnit.preconfigured'); -QUnit.test('Autostart is false', function (assert) { - assert.false(QUnit.config.autostart, 'The global autostart setting is applied'); +window.addEventListener('load', function () { + setTimeout(function () { + QUnit.start(); + }, 1); });