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: force fastboot environment to be set to test unless specified #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = {

if (this.fastboot) {
let options = makeFastbootTestingConfig({ distPath }, pkg);
reloadServer(this.fastboot, distPath, options);
reloadServer(this.fastboot, distPath, pkg, options);
} else {
this.fastboot = createServer(distPath, pkg);
}
Expand Down
58 changes: 48 additions & 10 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,64 @@ function createFastbootTest(app, callback) {
callback({ req, res, options, urlToVisit });
});
}

function createFastbootEcho(app) {
app.post('/fastboot-testing/echo', bodyParser.text(), (req, res) => {
res.send(req.body);
});
}

function reloadServer(fastboot, distPath, options) {
fastboot.reload({ distPath });
function reloadServer(fastboot, distPath, pkg, options) {
let config = makeFastbootTestingConfig({ distPath }, pkg);
setEnvironment(config.environment, () => fastboot.reload({ distPath }));
options.setupFastboot(fastboot);
}

function createServer(distPath, pkg) {
let options = makeFastbootTestingConfig({ distPath }, pkg);
let fastboot = new FastBoot(options);
let options = makeFastbootTestingConfig({ distPath }, pkg);
let fastboot = setEnvironment(options.environment, () => new FastBoot(options))
options.setupFastboot(fastboot);

return fastboot;
}

function makeFastbootTestingConfig(config, pkg) {
function setEnvironment(environment, callback = function() {}) {
let oldConfig;
if (process.env.APP_CONFIG) {
oldConfig = process.env.APP_CONFIG;
}
// Get app's desired environment settings and set this ENV var
// which tells the Ember App when being built in fastboot to use
// these settings and not the defaults (which would be whatever
// environment this was being run from)
// https://github.com/embermap/ember-cli-fastboot-testing/issues/32

if (environment) {
process.env.APP_CONFIG = JSON.stringify(environment);
}

let fastboot = callback()

if (oldConfig) {
process.env.APP_CONFIG = oldConfig;
}
else {
delete process.env.APP_CONFIG
}

return fastboot;
}

function readEnvironment(pkg, name) {
let configPath = 'config';
if (pkg['ember-addon'] && pkg['ember-addon']['configPath']) {
configPath = pkg['ember-addon']['configPath'];
}
let appEnvironment = require(path.resolve(configPath, 'environment.js'));
return appEnvironment(name);
}

function makeFastbootTestingConfig(config, pkg, useEnvironment) {
let defaults = {
setupFastboot() {}
};
Expand All @@ -88,12 +125,13 @@ function makeFastbootTestingConfig(config, pkg) {
}

let fastbootTestConfigPath = path.resolve(configPath, 'fastboot-testing.js');

let customized = fs.existsSync(fastbootTestConfigPath) ?
let overrides = fs.existsSync(fastbootTestConfigPath) ?
require(fastbootTestConfigPath) :
{};

return Object.assign({}, config, defaults, customized);
overrides.environment = readEnvironment(pkg, overrides.useEnvironment || 'test');

return Object.assign({}, config, defaults, overrides);
}

module.exports = {
Expand All @@ -104,4 +142,4 @@ module.exports = {
reloadServer,
createServer,
makeFastbootTestingConfig
};
};
5 changes: 5 additions & 0 deletions tests/dummy/app/pods/examples/environment/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Controller from '@ember/controller';
import config from '../../../config/environment';
export default Controller.extend({
env: config.environment,
});
1 change: 1 addition & 0 deletions tests/dummy/app/pods/examples/environment/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>{{env}}</h1>
2 changes: 2 additions & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Router.map(function() {
});

this.route('examples', function() {
this.route('environment');

this.route('redirects', function() {
this.route('transition-to');
this.route('replace-with');
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = function(environment) {
// ENV.APP.LOG_VIEW_LOOKUPS = true;
}

if (environment === 'test') {
if (environment === 'test' || environment == 'specified-by-user') {
// Testem prefers this...
ENV.locationType = 'none';

Expand Down
1 change: 1 addition & 0 deletions tests/dummy/config/fastboot-testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
sandboxGlobals: {
SampleGlobal: `TestSampleGlobal`
},
useEnvironment: 'specified-by-user',
setupFastboot: fastbootInstance => {
// the modified SampleGlobal will be available in window.SampleGlobal in model of route
fastbootInstance._app.sandbox.sandbox.SampleGlobal = 'Modified TestSampleGlobal';
Expand Down
11 changes: 11 additions & 0 deletions tests/fastboot/environment-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setup, visit } from 'ember-cli-fastboot-testing/test-support';

module('Fastboot | environment', function(hooks) {
setup(hooks);

test('it renders the correct h1 title', async function(assert) {
await visit('/examples/environment');
assert.dom('h1').includesText('specified-by-user');
});
});