Skip to content

Commit

Permalink
Use embroider macros to support staticAddonTestSupportTrees
Browse files Browse the repository at this point in the history
  • Loading branch information
thoov committed Sep 29, 2020
1 parent d493bdc commit d8f07ea
Show file tree
Hide file tree
Showing 13 changed files with 2,121 additions and 927 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ jobs:
- env: EMBER_TRY_SCENARIO=ember-canary TEST_FRAMEWORK=ember-mocha
- env: EMBER_TRY_SCENARIO=ember-default-with-jquery TEST_FRAMEWORK=ember-qunit
- env: EMBER_TRY_SCENARIO=ember-default-with-jquery TEST_FRAMEWORK=ember-mocha
- env: EMBER_TRY_SCENARIO=embroider TEST_FRAMEWORK=ember-qunit
- env: EMBER_TRY_SCENARIO=embroider TEST_FRAMEWORK=ember-mocha
- env: EMBER_TRY_SCENARIO=embroider-optimized TEST_FRAMEWORK=ember-qunit
- env: EMBER_TRY_SCENARIO=embroider-optimized TEST_FRAMEWORK=ember-mocha

before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash
Expand Down
10 changes: 5 additions & 5 deletions addon-test-support/-private/get-test-loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals require, requirejs */
import { dependencySatisfies, macroCondition, importSync } from '@embroider/macros';

/**
* Returns ember-exam-qunit-test-loader or ember-exam-mocha-test-loader
Expand All @@ -8,11 +8,11 @@
* @return {Object}
*/
export default function getTestLoader() {
if (requirejs.entries['ember-qunit/test-loader']) {
const EmberExamQUnitTestLoader = require('./ember-exam-qunit-test-loader');
if (macroCondition(dependencySatisfies('ember-qunit', '*'))){
const EmberExamQUnitTestLoader = importSync('./ember-exam-qunit-test-loader');
return EmberExamQUnitTestLoader['default'];
} else if (requirejs.entries['ember-mocha/test-loader']) {
const EmberExamMochaTestLoader = require('./ember-exam-mocha-test-loader');
} else if (macroCondition(dependencySatisfies('ember-mocha', '*'))){
const EmberExamMochaTestLoader = importSync('./ember-exam-mocha-test-loader');
return EmberExamMochaTestLoader['default'];
}

Expand Down
11 changes: 7 additions & 4 deletions addon-test-support/start.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* globals require */

import loadEmberExam from 'ember-exam/test-support/load';
import { dependencySatisfies, macroCondition, importSync } from '@embroider/macros';

/**
* Equivalent to ember-qunit or ember-mocha's loadTest() except this does not create a new TestLoader instance
Expand All @@ -26,14 +25,18 @@ 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}`);
let emberTestFramework;
if (macroCondition(dependencySatisfies('ember-qunit', '*'))){
emberTestFramework = importSync('ember-qunit');
} else if (macroCondition(dependencySatisfies('ember-mocha', '*'))) {
emberTestFramework = importSync('ember-mocha');
}

if (emberTestFramework.start) {
emberTestFramework.start(modifiedOptions);
Expand Down
22 changes: 21 additions & 1 deletion config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,27 @@ module.exports = function() {
'@ember/jquery': '^0.5.1'
}
}
}
},
{
name: 'embroider',
npm: {
devDependencies: {
'@embroider/core': '*',
'@embroider/webpack': '*',
'@embroider/compat': '*',
},
},
},
{
name: 'embroider-optimized',
npm: {
devDependencies: {
'@embroider/core': '*',
'@embroider/webpack': '*',
'@embroider/compat': '*',
},
}
},
]
};
});
Expand Down
20 changes: 19 additions & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,23 @@ module.exports = function(defaults) {
behave. You most likely want to be modifying `./index.js` or app's build file
*/

return app.toTree();
// Use embroider if it's present (it can get added by ember-try)
if ('@embroider/core' in app.dependencies()) {
/* eslint-disable node/no-missing-require, node/no-extraneous-require */
const { Webpack } = require('@embroider/webpack');
const { compatBuild } = require('@embroider/compat');
/* eslint-enable node/no-missing-require, node/no-extraneous-require */
let config = {};
if (process.env.EMBER_TRY_SCENARIO === 'embroider-optimized') {
config = {
staticAddonTrees: true,
staticAddonTestSupportTrees: true,
staticHelpers: true,
staticComponents: true,
}
}
return compatBuild(app, Webpack, config);
} else {
return app.toTree();
}
};
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"coverage": "nyc report --reporter=text-lcov | codeclimate-test-reporter"
},
"dependencies": {
"@embroider/macros": "^0.25.0",
"chalk": "^4.1.0",
"cli-table3": "^0.6.0",
"debug": "^4.1.0",
Expand All @@ -42,7 +43,7 @@
"@ember/optional-features": "^1.1.0",
"codeclimate-test-reporter": "^0.5.0",
"ember-cli": "^3.19.0",
"ember-cli-addon-docs": "^0.6.15",
"ember-cli-addon-docs": "^0.10.0",
"ember-cli-addon-docs-yuidoc": "^0.2.4",
"ember-cli-dependency-checker": "^3.0.0",
"ember-cli-deploy": "^1.0.2",
Expand Down Expand Up @@ -74,6 +75,18 @@
"sinon": "^9.0.3",
"testdouble": "^3.16.1"
},
"peerDependencies": {
"ember-mocha": "*",
"ember-qunit": "*"
},
"peerDependenciesMeta": {
"ember-qunit": {
"optional": true
},
"ember-mocha": {
"optional": true
}
},
"engines": {
"node": ">= 10.*"
},
Expand Down
3 changes: 2 additions & 1 deletion tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import { setResolver } from '@ember/test-helpers';
import resolver from './helpers/resolver';
import start from 'ember-exam/test-support/start';
import { macroCondition, dependencySatisfies } from '@embroider/macros';

const oppositeFramework = !require.has('ember-qunit') ? 'qunit' : 'mocha';
const oppositeFramework = macroCondition(dependencySatisfies('ember-qunit', '*')) ? 'mocha': 'qunit';

Object.keys(require.entries).forEach((entry) => {
if (entry.indexOf(oppositeFramework) !== -1) {
Expand Down
37 changes: 20 additions & 17 deletions tests/unit/mocha/filter-test-modules-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ describe('Unit | filter-test-modules', () => {
});

describe('modulePath', function() {
let modules = []

beforeEach(function() {
this.modules = [
modules = [
'foo-test',
'foo-test.jshint',
'bar-test',
Expand All @@ -34,38 +36,38 @@ describe('Unit | filter-test-modules', () => {
});

afterEach(function() {
this.modules = [];
modules = [];
});

it('should return a list of jshint tests', () => {
expect(filterTestModules(this.modules, 'jshint')).to.deep.equal([
expect(filterTestModules(modules, 'jshint')).to.deep.equal([
'foo-test.jshint',
'bar-test.jshint'
]);
});

it('should return an empty list when there is no match', () => {
expect(() => {
filterTestModules(this.modules, 'no-match');
filterTestModules(modules, 'no-match');
}).to.throw(/No tests matched with the filter:/);
});

it('should return a list of tests matched with a regular expression', () => {
expect(filterTestModules(this.modules, '/jshint/')).to.deep.equal([
expect(filterTestModules(modules, '/jshint/')).to.deep.equal([
'foo-test.jshint',
'bar-test.jshint'
]);
});

it('should return a list of tests matched with a regular expression that excluses jshint', () => {
expect(filterTestModules(this.modules, '!/jshint/')).to.deep.equal([
expect(filterTestModules(modules, '!/jshint/')).to.deep.equal([
'foo-test',
'bar-test'
]);
});

it('should return a list of tests matches with a list of string options', () => {
expect(filterTestModules(this.modules, 'foo, bar')).to.deep.equal([
expect(filterTestModules(modules, 'foo, bar')).to.deep.equal([
'foo-test',
'foo-test.jshint',
'bar-test',
Expand All @@ -74,67 +76,68 @@ describe('Unit | filter-test-modules', () => {
});

it('should return a list of unique tests matches when options are repeated', () => {
expect(filterTestModules(this.modules, 'foo, foo')).to.deep.equal([
expect(filterTestModules(modules, 'foo, foo')).to.deep.equal([
'foo-test',
'foo-test.jshint'
]);
});
});

describe('filePath', function() {
let modules = [];
beforeEach(function() {
this.modules = [
modules = [
'dummy/tests/integration/foo-test',
'dummy/tests/unit/foo-test',
'dummy/tests/unit/bar-test'
];
});

afterEach(function() {
this.modules = [];
modules = [];
});

it('should return a test module matches with full test file path', () => {
expect(filterTestModules(this.modules, null, 'app/tests/integration/foo-test.js')).to.deep.equal([
expect(filterTestModules(modules, null, 'app/tests/integration/foo-test.js')).to.deep.equal([
'dummy/tests/integration/foo-test'
]);
});

it('should return a test module matches with relative test file path', () => {
expect(filterTestModules(this.modules, null, '/tests/unit/foo-test')).to.deep.equal([
expect(filterTestModules(modules, null, '/tests/unit/foo-test')).to.deep.equal([
'dummy/tests/unit/foo-test'
]);
});

it('should return a test module matched with test file path with wildcard', () => {
expect(filterTestModules(this.modules, null, '/unit/*')).to.deep.equal([
expect(filterTestModules(modules, null, '/unit/*')).to.deep.equal([
'dummy/tests/unit/foo-test',
'dummy/tests/unit/bar-test'
]);
});

it('should return a test module matched with test file path with wildcard', () => {
expect(filterTestModules(this.modules, null, '/tests/*/foo*')).to.deep.equal([
expect(filterTestModules(modules, null, '/tests/*/foo*')).to.deep.equal([
'dummy/tests/integration/foo-test',
'dummy/tests/unit/foo-test'
]);
});

it('should return a list of tests matched with a regular expression', () => {
expect(() => {
filterTestModules(this.modules, null, 'no-match');
filterTestModules(modules, null, 'no-match');
}).to.throw(/No tests matched with the filter:/);
});

it('should return a list of tests matches with a list of string options', () => {
expect(filterTestModules(this.modules, null, '/tests/integration/*, dummy/tests/unit/foo-test')).to.deep.equal([
expect(filterTestModules(modules, null, '/tests/integration/*, dummy/tests/unit/foo-test')).to.deep.equal([
'dummy/tests/integration/foo-test',
'dummy/tests/unit/foo-test'
]);
});

it('should return a list of unique tests matches when options are repeated', () => {
expect(filterTestModules(this.modules, null, 'app/tests/unit/bar-test.js, /tests/unit/*')).to.deep.equal([
expect(filterTestModules(modules, null, 'app/tests/unit/bar-test.js, /tests/unit/*')).to.deep.equal([
'dummy/tests/unit/bar-test',
'dummy/tests/unit/foo-test'
]);
Expand Down
59 changes: 25 additions & 34 deletions tests/unit/mocha/multiple-ember-tests-test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
import { describeModule, it } from 'ember-mocha';
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupTest } from 'ember-mocha';

describeModule(
'component:herp-derp',
'#1: Module-For With Multiple Tests',
function() {
it('#1', function() {
expect(true).to.be.ok;
});
it('#2', function() {
expect(true).to.be.ok;
});
it('#3', function() {
expect(true).to.be.ok;
});
it('#4', function() {
expect(true).to.be.ok;
});
it('#5', function() {
expect(true).to.be.ok;
});
it('#6', function() {
expect(true).to.be.ok;
});
it('#7', function() {
expect(true).to.be.ok;
});
it('#8', function() {
expect(true).to.be.ok;
});
it('#9', function() {
expect(true).to.be.ok;
});
}
);
describe('#1: Multiple Ember Tests', function() {
setupTest();

it('#1', function() {
expect(true).to.be.ok;
});

it('#2', function() {
expect(true).to.be.ok;
});

it('#3', function() {
expect(true).to.be.ok;
});

it('#4', function() {
expect(true).to.be.ok;
});

it('#5', function() {
expect(true).to.be.ok;
});
});
Loading

0 comments on commit d8f07ea

Please sign in to comment.