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

WriteV1Config: fix /tests support #1412

Merged
merged 5 commits into from
Jun 7, 2023
Merged
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
9 changes: 8 additions & 1 deletion packages/compat/src/v1-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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`),
Expand Down
19 changes: 16 additions & 3 deletions packages/compat/src/v1-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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);
Expand Down
89 changes: 89 additions & 0 deletions tests/scenarios/app-config-environment-test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});