Skip to content

Commit

Permalink
Merge pull request #1182 from emberjs/isolate-test-loading
Browse files Browse the repository at this point in the history
Require explicit calls to loadTests and setupEmberOnerrorValidation
  • Loading branch information
ef4 authored Dec 18, 2024
2 parents da520e0 + e4a9efd commit 18d5ca9
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 61 deletions.
14 changes: 11 additions & 3 deletions addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@
"types": "./types/index.d.ts",
"default": "./dist/index.js"
},
"./*": "./dist/*.js",
"./*": {
"types": "./types/*.d.ts",
"default": "./dist/*.js"
},
"./addon-main.js": "./addon-main.cjs"
},
"files": [
"dist",
"types",
"addon-main.cjs"
],
"types": "types/index.d.ts",
"typesVersions": {
"*": {
"*": [
"types/*"
]
}
},
"scripts": {
"build": "rollup --config",
"lint": "concurrently 'npm:lint:*(!fix)' --names 'lint:'",
Expand All @@ -43,7 +52,6 @@
"dependencies": {
"@embroider/addon-shim": "^1.8.6",
"@embroider/macros": "^1.13.1",
"ember-cli-test-loader": "^3.1.0",
"qunit-theme-ember": "^1.0.0"
},
"devDependencies": {
Expand Down
13 changes: 0 additions & 13 deletions addon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ if (macroCondition(getOwnConfig()?.theme === 'ember')) {
}

export { default as QUnitAdapter, nonTestDoneCallback } from './adapter';
export { loadTests } from './test-loader';

import './qunit-configuration';

Expand All @@ -33,7 +32,6 @@ if (typeof Testem !== 'undefined') {

import { _backburner } from '@ember/runloop';
import { resetOnerror, getTestMetadata } from '@ember/test-helpers';
import { loadTests } from './test-loader';
import Ember from 'ember';
import * as QUnit from 'qunit';
import QUnitAdapter from './adapter';
Expand Down Expand Up @@ -180,7 +178,6 @@ export function setupTestIsolationValidation(delay) {
/**
@method start
@param {Object} [options] Options to be used for enabling/disabling behaviors
@param {Boolean} [options.loadTests] If `false` tests will not be loaded automatically.
@param {Boolean} [options.setupTestContainer] If `false` the test container will not
be setup based on `devmode`, `dockcontainer`, or `nocontainer` URL params.
@param {Boolean} [options.startTests] If `false` tests will not be automatically started
Expand All @@ -190,8 +187,6 @@ export function setupTestIsolationValidation(delay) {
@param {Boolean} [options.setupEmberTesting] `false` opts out of the
default behavior of setting `Ember.testing` to `true` before all tests and
back to `false` after each test will.
@param {Boolean} [options.setupEmberOnerrorValidation] If `false` validation
of `Ember.onerror` will be disabled.
@param {Boolean} [options.setupTestIsolationValidation] If `false` test isolation validation
will be disabled.
@param {Number} [options.testIsolationValidationDelay] When using
Expand All @@ -200,10 +195,6 @@ export function setupTestIsolationValidation(delay) {
async to have been completed. The default value is 50.
*/
export function start(options = {}) {
if (options.loadTests !== false) {
loadTests();
}

if (options.setupTestContainer !== false) {
setupTestContainer();
}
Expand All @@ -216,10 +207,6 @@ export function start(options = {}) {
setupEmberTesting();
}

if (options.setupEmberOnerrorValidation !== false) {
setupEmberOnerrorValidation();
}

if (
typeof options.setupTestIsolationValidation !== 'undefined' &&
options.setupTestIsolationValidation !== false
Expand Down
93 changes: 76 additions & 17 deletions addon/src/test-loader.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,81 @@
/* globals requirejs, require */

import * as QUnit from 'qunit';
import AbstractTestLoader, {
addModuleIncludeMatcher,
} from 'ember-cli-test-loader/test-support/index';

addModuleIncludeMatcher(function (moduleName) {
return moduleName.match(/\.jshint$/);
});
class TestLoader {
static load() {
new TestLoader().loadModules();
}

constructor() {
this._didLogMissingUnsee = false;
}

shouldLoadModule(moduleName) {
return moduleName.match(/[-_]test$/);
}

listModules() {
return Object.keys(requirejs.entries);
}

listTestModules() {
let moduleNames = this.listModules();
let testModules = [];
let moduleName;

for (let i = 0; i < moduleNames.length; i++) {
moduleName = moduleNames[i];

if (this.shouldLoadModule(moduleName)) {
testModules.push(moduleName);
}
}

return testModules;
}

loadModules() {
let testModules = this.listTestModules();
let testModule;

for (let i = 0; i < testModules.length; i++) {
testModule = testModules[i];
this.require(testModule);
this.unsee(testModule);
}
}

require(moduleName) {
try {
require(moduleName);
} catch (e) {
this.moduleLoadFailure(moduleName, e);
}
}

unsee(moduleName) {
if (typeof require.unsee === 'function') {
require.unsee(moduleName);
} else if (!this._didLogMissingUnsee) {
this._didLogMissingUnsee = true;
if (typeof console !== 'undefined') {
console.warn(
'unable to require.unsee, please upgrade loader.js to >= v3.3.0'
);
}
}
}

moduleLoadFailure(moduleName, error) {
moduleLoadFailures.push(error);

QUnit.module('TestLoader Failures');
QUnit.test(moduleName + ': could not be loaded', function () {
throw error;
});
}
}

let moduleLoadFailures = [];

Expand All @@ -26,17 +96,6 @@ QUnit.done(function () {
}
});

export class TestLoader extends AbstractTestLoader {
moduleLoadFailure(moduleName, error) {
moduleLoadFailures.push(error);

QUnit.module('TestLoader Failures');
QUnit.test(moduleName + ': could not be loaded', function () {
throw error;
});
}
}

/**
Load tests following the default patterns:
Expand Down
22 changes: 7 additions & 15 deletions addon/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ export class QUnitAdapter extends EmberTestAdapter {}
export { module, test, skip, only, todo } from 'qunit';

interface QUnitStartOptions {
/**
* If `false` tests will not be loaded automatically.
*/
loadTests?: boolean | undefined;

/**
* If `false` the test container will not be setup based on `devmode`,
* `dockcontainer`, or `nocontainer` URL params.
Expand All @@ -116,17 +111,14 @@ interface QUnitStartOptions {
*/
setupEmberTesting?: boolean | undefined;

/**
* If `false` validation of `Ember.onerror` will be disabled.
*/
setupEmberOnerrorValidation?: boolean | undefined;

/**
* If `false` test isolation validation will be disabled.
*/
setupTestIsolationValidation?: boolean | undefined;
}

export function setupEmberOnerrorValidation(): void;

export function start(options?: QUnitStartOptions): void;

// SAFETY: all of the `TC extends TestContext` generics below are just wildly,
Expand Down Expand Up @@ -281,10 +273,10 @@ declare global {

interface EachFunction {
<TC extends TestContext, T>(
name: string,
dataset: T[],
callback: (this: TC, assert: Assert, data: T) => void | Promise<unknown>
): void;
}
name: string,
dataset: T[],
callback: (this: TC, assert: Assert, data: T) => void | Promise<unknown>
): void;
}
}
}
1 change: 1 addition & 0 deletions addon/types/test-loader.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function loadTests(): void;
7 changes: 0 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion test-app/tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import config from 'test-app/config/environment';
import * as QUnit from 'qunit';
import { setApplication } from '@ember/test-helpers';
import { setup } from 'qunit-dom';
import { start } from 'ember-qunit';
import { loadTests } from 'ember-qunit/test-loader';
import { start, setupEmberOnerrorValidation } from 'ember-qunit';

setApplication(Application.create(config.APP));

setup(QUnit.assert);

setupEmberOnerrorValidation();
loadTests();
start();
7 changes: 2 additions & 5 deletions test-app/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export class QUnitAdapter extends EmberTestAdapter {}

export { module, test, skip, only, todo } from 'qunit';

export function setupEmberOnerrorValidation(): void;

interface QUnitStartOptions {
/**
* If `false` tests will not be loaded automatically.
Expand Down Expand Up @@ -116,11 +118,6 @@ interface QUnitStartOptions {
*/
setupEmberTesting?: boolean | undefined;

/**
* If `false` validation of `Ember.onerror` will be disabled.
*/
setupEmberOnerrorValidation?: boolean | undefined;

/**
* If `false` test isolation validation will be disabled.
*/
Expand Down

0 comments on commit 18d5ca9

Please sign in to comment.