Skip to content

Commit

Permalink
Core: Support a predefined QUnit.config
Browse files Browse the repository at this point in the history
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 qunitjs#1059
  • Loading branch information
TobiasOetzel committed Dec 4, 2016
1 parent eb642e7 commit 5ae7399
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sessionStorage } from "../globals";
import { window, sessionStorage } from "../globals";
import { extend } from "./utilities";

/**
* Config object: Maintain internal state
Expand Down Expand Up @@ -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 );

Expand Down
4 changes: 3 additions & 1 deletion src/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -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." );
}

Expand Down
21 changes: 21 additions & 0 deletions test/preconfigured.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QUnit Preconfigured Test Suite</title>
<link rel="stylesheet" href="../dist/qunit.css">
<script>
window.QUnit = {
config: {
autostart: false,
reorder: false
}
};
</script>
<script src="../dist/qunit.js"></script>
<script src="preconfigured.js"></script>
</head>
<body>
<div id="qunit"></div>
</body>
</html>
21 changes: 21 additions & 0 deletions test/preconfigured.js
Original file line number Diff line number Diff line change
@@ -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" );
} );

0 comments on commit 5ae7399

Please sign in to comment.