diff --git a/packages/compat/src/v1-app.ts b/packages/compat/src/v1-app.ts index 107e52d37..80d7643c7 100644 --- a/packages/compat/src/v1-app.ts +++ b/packages/compat/src/v1-app.ts @@ -149,6 +149,13 @@ export default class V1App { return new V1Config(this.configTree, this.app.env); } + @Memoize() + get testConfig(): V1Config | undefined { + if (this.shouldBuildTests) { + return new V1Config(this.configTree, 'test'); + } + } + get autoRun(): boolean { return this.app.options.autoRun; } @@ -659,7 +666,7 @@ export default class V1App { let appTree = this.appTree; let testsTree = this.testsTree; let lintTree = this.lintTree; - let config = new WriteV1Config(this.config, this.storeConfigInMeta); + let config = new WriteV1Config(this.config, this.storeConfigInMeta, this.testConfig); let patterns = this.configReplacePatterns; let configReplaced = new this.configReplace(config, this.configTree, { configPath: join('environments', `${this.app.env}.json`), diff --git a/packages/compat/src/v1-config.ts b/packages/compat/src/v1-config.ts index fe20f27eb..62d5ae3ac 100644 --- a/packages/compat/src/v1-config.ts +++ b/packages/compat/src/v1-config.ts @@ -30,8 +30,8 @@ export class V1Config extends Plugin { export class WriteV1Config extends Plugin { private lastContents: string | undefined; - constructor(private inputTree: V1Config, private storeConfigInMeta: boolean) { - super([inputTree], { + constructor(private inputTree: V1Config, private storeConfigInMeta: boolean, private testInputTree?: V1Config) { + super([inputTree, testInputTree as V1Config].filter(Boolean), { persistentOutput: true, needsCache: false, }); @@ -42,7 +42,20 @@ export class WriteV1Config extends Plugin { if (this.storeConfigInMeta) { contents = metaLoader(); } else { - contents = `export default ${JSON.stringify(this.inputTree.readConfig())};`; + if (this.testInputTree) { + contents = ` + import { isTesting } from '@embroider/macros'; + let env; + if (isTesting()) { + env = ${JSON.stringify(this.testInputTree.readConfig())}; + } else { + env = ${JSON.stringify(this.inputTree.readConfig())}; + } + export default env; + `; + } else { + contents = `export default ${JSON.stringify(this.inputTree.readConfig())};`; + } } if (!this.lastContents || this.lastContents !== contents) { outputFileSync(filename, contents); diff --git a/tests/scenarios/app-config-environment-test.ts b/tests/scenarios/app-config-environment-test.ts new file mode 100644 index 000000000..bbf76ca7e --- /dev/null +++ b/tests/scenarios/app-config-environment-test.ts @@ -0,0 +1,89 @@ +import merge from 'lodash/merge'; +import { appScenarios } from './scenarios'; +import { PreparedApp } from 'scenario-tester'; +import QUnit from 'qunit'; + +const { module: Qmodule, test } = QUnit; + +appScenarios + .only('release') + .map('app-config-environment', project => { + merge(project.files, { + 'ember-cli-build.js': ` + 'use strict'; + + const EmberApp = require('ember-cli/lib/broccoli/ember-app'); + const { maybeEmbroider } = require('@embroider/test-setup'); + + module.exports = function (defaults) { + let app = new EmberApp(defaults, { + tests: true, + storeConfigInMeta: false, + }); + return maybeEmbroider(app, {}); + }; + `, + config: { + 'environment.js': `module.exports = function(environment) { + // DEFAULT config/environment.js + let ENV = { + modulePrefix: 'my-app', + environment, + rootURL: '/', + locationType: 'history', + EmberENV: { + EXTEND_PROTOTYPES: false, + FEATURES: {}, + }, + APP: {}, + }; + + if (environment === 'test') { + ENV.locationType = 'none'; + ENV.APP.LOG_ACTIVE_GENERATION = false; + ENV.APP.LOG_VIEW_LOOKUPS = false; + ENV.APP.rootElement = '#ember-testing'; + ENV.APP.autoboot = false; + + // CUSTOM + ENV.someCustomField = true; + }; + + return ENV; + };`, + }, + tests: { + unit: { + 'store-config-in-meta-test.js': ` + import { module, test } from 'qunit'; + import ENV from 'app-template/config/environment'; + + module('Unit | storeConfigInMeta set to false', function (hooks) { + test('it has loaded the correct config values', async function (assert) { + assert.equal(ENV.someCustomField, true); + }); + });`, + }, + }, + }); + }) + .forEachScenario(scenario => { + Qmodule(scenario.name, function (hooks) { + let app: PreparedApp; + hooks.before(async () => { + app = await scenario.prepare(); + }); + + test(`ember test ran against dev build with custom unit test`, async function (assert) { + // here we build the app with environment set to dev so that we can use + // the build output directory as the input path to an `ember test` run + // later. This difference in environment is important because it's the + // only way for us to test ember-cli-build.js' `tests: true` behavior, + // and is equivalent to visiting the app's /tests page + let devBuildResult = await app.execute(`pnpm build --environment=development`); + assert.equal(devBuildResult.exitCode, 0, devBuildResult.output); + let testRunResult = await app.execute(`pnpm test:ember --path dist`); + assert.equal(testRunResult.exitCode, 0, testRunResult.output); + }); + }); + });