Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit cf5c5a5

Browse files
author
Robert Jackson
committed
Replace autostart behavior with hook to start tests.
Prior to this change, tests automatically started once DOMContentLoaded fires. This means that there is no obvious mechanism to hook into the system after boot, but before tests start (e.g. to load lazy engine assets). This is a somewhat breaking change, apps will have to update their `tests/test-helper.js` to properly kick off the tests. The change is pretty trivial: ```js // tests/test-helper.js import { start } from 'ember-cli-qunit'; // ...snip current content... start(); ```
1 parent fb8127a commit cf5c5a5

5 files changed

Lines changed: 139 additions & 113 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import Ember from 'ember';
2+
import QUnit from 'qunit';
3+
import { QUnitAdapter } from 'ember-qunit';
4+
import AbstractTestLoader, {
5+
addModuleExcludeMatcher,
6+
addModuleIncludeMatcher
7+
} from 'ember-cli-test-loader/test-support/index';
8+
9+
10+
addModuleExcludeMatcher(function(moduleName) {
11+
return QUnit.urlParams.nolint &&
12+
moduleName.match(/\.(jshint|lint-test)$/);
13+
});
14+
15+
addModuleIncludeMatcher(function(moduleName) {
16+
return moduleName.match(/\.jshint$/);
17+
});
18+
19+
let moduleLoadFailures = [];
20+
21+
QUnit.done(function() {
22+
if (moduleLoadFailures.length) {
23+
throw new Error('\n' + moduleLoadFailures.join('\n'));
24+
}
25+
});
26+
27+
export class TestLoader extends AbstractTestLoader {
28+
moduleLoadFailure(moduleName, error) {
29+
moduleLoadFailures.push(error);
30+
31+
QUnit.module('TestLoader Failures');
32+
QUnit.test(moduleName + ': could not be loaded', function() {
33+
throw error;
34+
});
35+
}
36+
}
37+
/**
38+
Uses current URL configuration to setup the test container.
39+
40+
* If `?nocontainer` is set, the test container will be hidden.
41+
* If `?dockcontainer` or `?devmode` are set the test container will be
42+
absolutely positioned.
43+
* If `?devmode` is set, the test container will be made full screen.
44+
45+
@method setupTestContainer
46+
*/
47+
export function setupTestContainer() {
48+
let testContainer = document.getElementById('ember-testing-container');
49+
if (!testContainer) { return; }
50+
51+
let params = QUnit.urlParams;
52+
53+
let containerVisibility = params.nocontainer ? 'hidden' : 'visible';
54+
let containerPosition = (params.dockcontainer || params.devmode) ? 'absolute' : 'relative';
55+
56+
if (params.devmode) {
57+
testContainer.className = ' full-screen';
58+
}
59+
60+
testContainer.style.visibility = containerVisibility;
61+
testContainer.style.position = containerPosition;
62+
}
63+
64+
/**
65+
Load tests following the default patterns:
66+
67+
* The module name ends with `-test`
68+
* The module name ends with `.jshint`
69+
70+
Excludes tests that match the following
71+
patterns when `?nolint` URL param is set:
72+
73+
* The module name ends with `.jshint`
74+
* The module name ends with `-lint-test`
75+
76+
@method loadTests
77+
*/
78+
export function loadTests() {
79+
new TestLoader().loadModules();
80+
}
81+
82+
/**
83+
Instruct QUnit to start the tests.
84+
@method startTests
85+
*/
86+
export function startTests() {
87+
QUnit.start();
88+
}
89+
90+
/**
91+
Sets up the `Ember.Test` adapter for usage with QUnit 2.x.
92+
93+
@method setupTestAdapter
94+
*/
95+
export function setupTestAdapter() {
96+
Ember.Test.adapter = QUnitAdapter.create();
97+
}
98+
99+
/**
100+
@method start
101+
@param {Object} [options] Options to be used for enabling/disabling behaviors
102+
@param {Boolean} [options.loadTests] If `false` tests will not be loaded automatically.
103+
@param {Boolean} [options.setupTestContainer] If `false` the test container will not
104+
be setup based on `devmode`, `dockcontainer`, or `nocontainer` URL params.
105+
@param {Boolean} [options.startTests] If `false` tests will not be automatically started
106+
(you must run `QUnit.start()` to kick them off).
107+
@param {Boolean} [options.setupTestAdapter] If `false` the default Ember.Test adapter will
108+
not be updated.
109+
*/
110+
export function start(options = { }) {
111+
if (options.loadTests !== false) {
112+
loadTests();
113+
}
114+
115+
if (options.setupTestContainer !== false) {
116+
setupTestContainer();
117+
}
118+
119+
if (options.setupTestAdapter !== false) {
120+
setupTestAdapter();
121+
}
122+
123+
if (options.startTests !== false) {
124+
startTests();
125+
}
126+
}

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,16 @@ module.exports = {
4444
return path.join(__dirname, 'blueprints');
4545
},
4646

47-
treeForAddonTestSupport: function() {
47+
// intentionally not calling _super here
48+
// to avoid these trees being namespaced into
49+
// `ember-cli-qunit/test-support/`
50+
treeForAddonTestSupport: function(onDiskAddonTestSupportTree) {
4851
var MergeTrees = require('broccoli-merge-trees');
49-
var tree = new MergeTrees(this._getDependencyTrees());
52+
var trees = [].concat(
53+
this._getDependencyTrees(),
54+
onDiskAddonTestSupportTree
55+
);
56+
var tree = new MergeTrees(trees);
5057

5158
if (this._shouldPreprocessAddonTestSupport) {
5259
return this.preprocessJs(tree, {
@@ -88,7 +95,6 @@ module.exports = {
8895
'vendor/qunit/qunit.css',
8996
'vendor/qunit-notifications/index.js',
9097
'vendor/ember-cli-qunit/qunit-configuration.js',
91-
'vendor/ember-cli-qunit/test-loader.js'
9298
];
9399

94100
var addonOptions = target.options['ember-cli-qunit'];

tests/test-helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ import resolver from './helpers/resolver';
22
import {
33
setResolver
44
} from 'ember-qunit';
5+
import { start } from 'ember-cli-qunit';
56

67
setResolver(resolver);
8+
start();
Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
/* globals jQuery, QUnit */
1+
/* globals QUnit */
22

33
(function() {
4+
QUnit.config.autostart = false;
45
QUnit.config.urlConfig.push({ id: 'nocontainer', label: 'Hide container'});
56
QUnit.config.urlConfig.push({ id: 'nolint', label: 'Disable Linting'});
67
QUnit.config.urlConfig.push({ id: 'dockcontainer', label: 'Dock container'});
@@ -16,34 +17,4 @@
1617
}
1718
});
1819
}
19-
20-
function ready(fn) {
21-
if (typeof jQuery === 'function') {
22-
jQuery(document).ready(fn);
23-
return;
24-
}
25-
26-
if (document.readyState != 'loading'){
27-
fn();
28-
} else {
29-
document.addEventListener('DOMContentLoaded', fn);
30-
}
31-
}
32-
33-
ready(function() {
34-
var testContainer = document.getElementById('ember-testing-container');
35-
if (!testContainer) { return; }
36-
37-
var params = QUnit.urlParams;
38-
39-
var containerVisibility = params.nocontainer ? 'hidden' : 'visible';
40-
var containerPosition = (params.dockcontainer || params.devmode) ? 'absolute' : 'relative';
41-
42-
if (params.devmode) {
43-
testContainer.className = ' full-screen';
44-
}
45-
46-
testContainer.style.visibility = containerVisibility;
47-
testContainer.style.position = containerPosition;
48-
});
4920
})();

vendor/ember-cli-qunit/test-loader.js

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)