diff --git a/index.js b/index.js index 0d0ff93f..d9b7cc9c 100644 --- a/index.js +++ b/index.js @@ -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); } diff --git a/lib/helpers.js b/lib/helpers.js index df48172d..6671c9d9 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -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() {} }; @@ -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 = { @@ -104,4 +142,4 @@ module.exports = { reloadServer, createServer, makeFastbootTestingConfig -}; \ No newline at end of file +}; diff --git a/tests/dummy/app/pods/examples/environment/controller.js b/tests/dummy/app/pods/examples/environment/controller.js new file mode 100644 index 00000000..4c09b9c3 --- /dev/null +++ b/tests/dummy/app/pods/examples/environment/controller.js @@ -0,0 +1,5 @@ +import Controller from '@ember/controller'; +import config from '../../../config/environment'; +export default Controller.extend({ + env: config.environment, +}); diff --git a/tests/dummy/app/pods/examples/environment/template.hbs b/tests/dummy/app/pods/examples/environment/template.hbs new file mode 100644 index 00000000..7ea6770f --- /dev/null +++ b/tests/dummy/app/pods/examples/environment/template.hbs @@ -0,0 +1 @@ +