Module loading utilities for dynamically loading test modules, particularly useful in AMD environments where test files need to be discovered and loaded at runtime.
Convenience function to automatically load all test modules.
/**
* Load tests following the default patterns (module name ends with '-test')
* Creates a TestLoader instance and calls loadModules()
*/
function loadTests(): void;Usage Example:
// tests/test-helper.js - For AMD environments
import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';
import { loadTests } from 'ember-qunit/test-loader';
setApplication(Application.create(config.APP));
loadTests(); // Load all test modules before starting
start();Main class for discovering and loading test modules with customizable patterns.
/**
* Handles discovery and loading of test modules in AMD environments
*/
class TestLoader {
/** Static convenience method to create instance and load modules */
static load(): void;
/**
* Determines if a module should be loaded as a test module
* @param moduleName - The name of the module to check
* @returns true if module name ends with '-test'
*/
shouldLoadModule(moduleName: string): boolean;
/**
* Lists all available modules from requirejs.entries
* @returns Array of all module names
*/
listModules(): string[];
/**
* Lists only modules that should be loaded as tests
* @returns Array of test module names
*/
listTestModules(): string[];
/**
* Loads all test modules, calling require() and unsee() on each
*/
loadModules(): void;
/**
* Requires a single module with error handling
* @param moduleName - Name of module to require
*/
require(moduleName: string): void;
/**
* Unseens a module if require.unsee is available
* @param moduleName - Name of module to unsee
*/
unsee(moduleName: string): void;
/**
* Handles module load failures by creating QUnit test failures
* @param moduleName - Name of the module that failed to load
* @param error - The error that occurred during loading
*/
moduleLoadFailure(moduleName: string, error: Error): void;
}Usage Examples:
// Basic usage
import { TestLoader } from 'ember-qunit/test-loader';
TestLoader.load(); // Load all test modules// Custom usage
import { TestLoader } from 'ember-qunit/test-loader';
const loader = new TestLoader();
// Check specific modules
if (loader.shouldLoadModule('my-component-test')) {
console.log('This is a test module');
}
// Get all test modules
const testModules = loader.listTestModules();
console.log('Found test modules:', testModules);
// Load modules manually
loader.loadModules();// Custom test loading pattern
class CustomTestLoader extends TestLoader {
shouldLoadModule(moduleName) {
// Custom pattern: load modules ending with '-spec' or '-test'
return moduleName.match(/[-_](test|spec)$/);
}
}
const customLoader = new CustomTestLoader();
customLoader.loadModules();The TestLoader follows this process:
requirejs.entries for all available modulesshouldLoadModule() to identify test modules (default: ends with -test)require() on each test moduleunsee() on each module to prevent caching issuesWhen modules fail to load, TestLoader automatically creates QUnit test failures:
// Failed module loads create tests like this:
QUnit.module('TestLoader Failures');
QUnit.test('my-broken-test: could not be loaded', function() {
throw loadError; // The original error
});Multiple module load failures are collected and re-thrown at the end of the test run via QUnit.done() callback.
loadTests()import.meta.glob() for automatic test discovery// AMD setup (requires loadTests)
import { loadTests } from 'ember-qunit/test-loader';
loadTests();
start();
// ESM setup (automatic discovery)
start(); // No loadTests() needed