Skip to content

Commit

Permalink
Pass options to middleware
Browse files Browse the repository at this point in the history
Useful if the middleware is expensive and should be optionally started
depending on ember-cli options.

For example `ember serve` and `ember test` use a `path` flag to specify
an existing ember build to use, without rebuilding. `ember-cli-typescript`
will add typechecking middleware regardless of ember using an existing
build, the typechecking middleware is not required. This can slow
down the initial request to express server significantly. Passing the
ember options to middleware will allow middleware to optionally enable
different features depending on options
  • Loading branch information
mrloop committed May 10, 2020
1 parent d37a61d commit 5972bba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/tasks/test.js
Expand Up @@ -27,12 +27,14 @@ class TestTask extends Task {
});
}

addonMiddlewares() {
addonMiddlewares(options) {
this.project.initializeAddons();

return this.project.addons.reduce((addons, addon) => {
if (addon.testemMiddleware) {
addons.push(addon.testemMiddleware.bind(addon));
addons.push(function () {
addon.testemMiddleware(...arguments, options);
});
}

return addons;
Expand All @@ -54,7 +56,7 @@ class TestTask extends Task {
port: options.port,
debug: options.testemDebug,
reporter: options.reporter,
middleware: this.addonMiddlewares(),
middleware: this.addonMiddlewares(options),
launch: options.launch,
file: options.configFile,
/* eslint-disable camelcase */
Expand All @@ -67,7 +69,6 @@ class TestTask extends Task {
transformed.key = options.sslKey;
transformed.cert = options.sslCert;
}

return transformed;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/tasks/testem-config/testem-dummy.json
@@ -0,0 +1,9 @@
{
"framework": "qunit",
"launch": "Dummy",
"launchers": {
"Dummy" : {
"command": "exit"
}
}
}
30 changes: 30 additions & 0 deletions tests/unit/tasks/test-test.js
Expand Up @@ -4,9 +4,39 @@ const expect = require('chai').expect;
const TestTask = require('../../../lib/tasks/test');
const MockProject = require('../../helpers/mock-project');

function sleep(timeout) {
return new Promise((resolve) => setTimeout(resolve, timeout));
}

describe('test task test', function () {
let subject;

it('call testem middleware with options', async function () {
let testemMiddlewareOptions;
let project = new MockProject();

project.initializeAddons = function () {};
project.addons = [
{
testemMiddleware(_, options) {
testemMiddlewareOptions = options;
},
},
];

let options = {
reporter: 'xunit',
configFile: 'tests/fixtures/tasks/testem-config/testem-dummy.json',
path: 'dist',
ssl: false,
};

subject = new TestTask({ project });
await subject.run(options);
expect(testemMiddlewareOptions).to.deep.equal(options);
expect(testemMiddlewareOptions.path).to.equal('dist');
});

it('transforms options for testem configuration', function () {
subject = new TestTask({
project: new MockProject(),
Expand Down

0 comments on commit 5972bba

Please sign in to comment.