From 2c9928868f341c2b347277f5c54a0567d3fca8fb Mon Sep 17 00:00:00 2001 From: Tobias Oetzel Date: Mon, 10 Oct 2016 14:04:59 +0200 Subject: [PATCH] Core: Support a predefined QUnit.config If a global QUnit.config object is present before QUnit is loaded, the default config values will be extended. The use case is to configure QUnit before it is loaded. Closes #1059 --- Gruntfile.js | 1 + src/core/config.js | 11 ++++++++++- src/export.js | 4 +++- test/preconfigured.html | 21 +++++++++++++++++++++ test/preconfigured.js | 21 +++++++++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 test/preconfigured.html create mode 100644 test/preconfigured.js diff --git a/Gruntfile.js b/Gruntfile.js index 6f632861d..c66342ddd 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -99,6 +99,7 @@ grunt.initConfig( { "test/only.html", "test/seed.html", "test/overload.html", + "test/preconfigured.html", "test/regex-filter.html", "test/regex-exclude-filter.html", "test/string-filter.html" diff --git a/src/core/config.js b/src/core/config.js index 5ecd0a097..6b7131030 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -1,4 +1,5 @@ -import { sessionStorage } from "../globals"; +import { window, sessionStorage } from "../globals"; +import { extend } from "./utilities"; /** * Config object: Maintain internal state @@ -56,6 +57,14 @@ const config = { storage: sessionStorage }; +// take a predefined QUnit.config and extend the defaults +var 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 ); +} + // Push a loose unnamed module to the modules collection config.modules.push( config.currentModule ); diff --git a/src/export.js b/src/export.js index c885f69ab..4089cfbdf 100644 --- a/src/export.js +++ b/src/export.js @@ -47,7 +47,9 @@ Object.defineProperty( QUnit, "reset", { } ); if ( defined.document ) { - if ( window.QUnit ) { + + // QUnit may be defined when it is preconfigured but then only QUnit and QUnit.config may be defined. + if ( window.QUnit && window.QUnit.version ) { throw new Error( "QUnit has already been defined." ); } diff --git a/test/preconfigured.html b/test/preconfigured.html new file mode 100644 index 000000000..0205b3f02 --- /dev/null +++ b/test/preconfigured.html @@ -0,0 +1,21 @@ + + + + + QUnit Preconfigured Test Suite + + + + + + +
+ + diff --git a/test/preconfigured.js b/test/preconfigured.js new file mode 100644 index 000000000..243a1a47a --- /dev/null +++ b/test/preconfigured.js @@ -0,0 +1,21 @@ +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.ok( QUnit.config.scrolltop, "The scrolltop default is true" ); + } ); + + QUnit.test( "Qunit.config.reorder default was overwritten", function( assert ) { + assert.notOk( QUnit.config.reorder, "reorder was overwritten" ); + } ); + + QUnit.start(); + }, 100 ); +} ); + +QUnit.module( "QUnit.preconfigured" ); +QUnit.test( "Autostart is false", function( assert ) { + assert.notOk( QUnit.config.autostart, "The global autostart setting is applied" ); +} );