Skip to content

Commit

Permalink
Core: Add support for preconfig to all environments
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Krinkle committed Mar 28, 2022
1 parent 0f6ec7c commit 0befe2a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
24 changes: 20 additions & 4 deletions docs/config/index.md
Expand Up @@ -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.<br/>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.
15 changes: 8 additions & 7 deletions src/core/config.js
@@ -1,4 +1,4 @@
import { window, localSessionStorage } from '../globals';
import { globalThis, localSessionStorage } from '../globals';
import { extend } from './utilities';

/**
Expand Down Expand Up @@ -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
Expand Down
25 changes: 9 additions & 16 deletions 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);
});

0 comments on commit 0befe2a

Please sign in to comment.