Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Make mocha work with ember-exam v3 #259

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions addon-test-support/-private/ember-exam-mocha-test-loader.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import getUrlParams from './get-url-params';
import splitTestModules from './split-test-modules';
import { TestLoader } from 'ember-mocha/test-loader';
import { describe, it } from 'mocha';

// get testLoader from 'ember-qunit/test-loader' once a new version get's released
import TestLoader from 'ember-cli-test-loader/test-support/index';

/**
* EmberExamMochaTestLoader extends ember-mocha/test-loader used by `ember test`, since it
* overrides moduleLoadFailure() to log a test failure when a module fails to load
* EmberExamMochaTestLoader extends ember-cli-test-loader used by `ember test`
* @class EmberExamMochaTestLoader
* @extends {TestLoader}
*/
Expand Down Expand Up @@ -43,6 +45,20 @@ export default class EmberExamMochaTestLoader extends TestLoader {
*/
unsee() {}

/**
* ensure a test failure is reported if a module cannot be loaded
*
* @param {string} moduleName
* @param {error} error
*/
moduleLoadFailure(moduleName, error) {
describe('TestLoader Failures', function() {
it(moduleName + ': could not be loaded', function() {
throw error;
});
});
}

/**
* Loads the test modules depending on the urlParam
*/
Expand Down
25 changes: 20 additions & 5 deletions addon-test-support/-private/get-test-loader.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
/* globals require, requirejs */
/* globals require */

const emberQunit = 'ember-qunit';
const emberMocha = 'ember-mocha';

/**
* Returns the test framework being used
*/
export function getTestFramework() {
if (require.has(emberQunit)){
return emberQunit;
} else if (require.has(emberMocha)){
return emberMocha;
}
return;
}

/**
* Returns ember-exam-qunit-test-loader or ember-exam-mocha-test-loader
*
* @export
* @returns {Object}
*/
export default function getTestLoader() {
if (requirejs.entries['ember-qunit/test-loader']) {
export function getTestLoader() {
if (require.has(emberQunit)){
const EmberExamQUnitTestLoader = require('./ember-exam-qunit-test-loader');
return EmberExamQUnitTestLoader['default'];
} else if (requirejs.entries['ember-mocha/test-loader']) {
} else if (require.has(emberMocha)){
const EmberExamMochaTestLoader = require('./ember-exam-mocha-test-loader');
return EmberExamMochaTestLoader['default'];
return EmberExamMochaTestLoader['default'];
}

throw new Error(
Expand Down
19 changes: 17 additions & 2 deletions addon-test-support/load.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import TestemOutput from './-private/patch-testem-output';
import getTestLoader from './-private/get-test-loader';
import { getTestLoader } from './-private/get-test-loader';

let loaded = false;

/**
* Equivalent to ember-qunit or ember-mocha's loadTest() except this does not create a new TestLoader instance
*
* @param {*} testLoader
*/
export function loadTests(testLoader) {
if (testLoader === undefined) {
throw new Error(
'A testLoader instance has not been created. You must call `loadEmberExam()` before calling `loadTest()`.'
);
}

testLoader.loadModules();
}

/**
* Setup EmberExamTestLoader to enable ember exam functionalities
*
* @returns {*} testLoader
*/
export default function loadEmberExam() {
export function loadEmberExam() {
if (loaded) {
// eslint-disable-next-line no-console
console.warn('Attempted to load Ember Exam more than once.');
Expand Down
21 changes: 3 additions & 18 deletions addon-test-support/start.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
/* globals require */

import loadEmberExam from 'ember-exam/test-support/load';

/**
* Equivalent to ember-qunit or ember-mocha's loadTest() except this does not create a new TestLoader instance
*
* @param {*} testLoader
*/
function loadTests(testLoader) {
if (testLoader === undefined) {
throw new Error(
'A testLoader instance has not been created. You must call `loadEmberExam()` before calling `loadTest()`.'
);
}

testLoader.loadModules();
}
import { getTestFramework } from './-private/get-test-loader';
import { loadEmberExam, loadTests } from 'ember-exam/test-support/load';

/**
* Ember-exam's own start function to set up EmberExamTestLoader, load tests and calls start() from
Expand All @@ -24,14 +10,13 @@ function loadTests(testLoader) {
* @param {*} qunitOptions
*/
export default function start(qunitOptions) {
const framework = require.has('ember-qunit') ? 'qunit' : 'mocha';
const modifiedOptions = qunitOptions || Object.create(null);
modifiedOptions.loadTests = false;

const testLoader = loadEmberExam();
loadTests(testLoader);

const emberTestFramework = require(`ember-${framework}`);
const emberTestFramework = require(getTestFramework());

if (emberTestFramework.start) {
emberTestFramework.start(modifiedOptions);
Expand Down