Skip to content

Commit 93ae9d0

Browse files
Merge pull request #66 from ember-cli/nvp/add-build-mode-tests
Add build mode tests (ensuring isDevelopingApp and isTesting are appropriately true when they need to be (as well as assert-stripping))
2 parents 4f5ae5f + b36a28e commit 93ae9d0

12 files changed

Lines changed: 114 additions & 25 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/node_modules/
22
.log/
3-
/my-addon/
3+
my-addon/

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*.md
44
files/
55
tests/fixtures/
6+
my-addon/
67

78
node_modules/
89

files/gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ ember-cli-build.cjs
1515
node_modules/
1616
.eslintcache
1717
.prettiercache
18+
19+
# potentially containing secrets
20+
*.local

files/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"@embroider/addon-dev": "^8.1.0",
4747
"@embroider/compat": "^4.1.0",
4848
"@embroider/core": "^4.1.0",
49-
"@embroider/macros": "^1.18.0",
49+
"@embroider/macros": "^1.20.1",
5050
"@embroider/vite": "^1.1.5",
5151
"@eslint/js": "^9.17.0",
5252
"@glimmer/component": "^2.0.0<% if (typescript) { %>",

files/tests/test-helper.__ext__

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as QUnit from 'qunit';
44
import { setApplication } from '@ember/test-helpers';
55
import { setup } from 'qunit-dom';
66
import { start as qunitStart, setupEmberOnerrorValidation } from 'ember-qunit';
7+
import { setTesting } from '@embroider/macros';
78

89
class Router extends EmberRouter {
910
location = 'none';
@@ -21,6 +22,7 @@ class TestApp extends EmberApp {
2122
Router.map(function () {});
2223

2324
export function start() {
25+
setTesting(true);
2426
setApplication(
2527
TestApp.create({
2628
autoboot: false,

pnpm-lock.yaml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { test, module } from 'qunit';
2+
import { assert } from '@ember/debug';
3+
import { DEBUG } from '@glimmer/env';
4+
import { isDevelopingApp, isTesting } from '@embroider/macros';
5+
6+
module('debug utils remain in the build', function () {
7+
test('assert', function(qAssert) {
8+
// If we get the build mode wrong, e.g.: `NODE_ENV` != 'development'
9+
// then the assert won't exist, causing qAssert to not detect a thrown Error
10+
qAssert.throws(() => {
11+
assert('should throw');
12+
}, /should throw/, `The error "should throw" is thrown`);
13+
});
14+
15+
test('isTesting', function (assert) {
16+
assert.strictEqual(isTesting(), true, `isTesting() === true`);
17+
});
18+
19+
test('isDevelopingApp', function (assert) {
20+
assert.strictEqual(isDevelopingApp(), true, `isDevelopingApp() === true`);
21+
});
22+
23+
24+
module('not supported', function () {
25+
test('DEBUG', function (assert) {
26+
if (DEBUG) {
27+
assert.step('DEBUG');
28+
}
29+
30+
assert.verifySteps([]);
31+
});
32+
});
33+
});

tests/helpers/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ export async function createTmp() {
4848
return tmpDirPath;
4949
}
5050

51+
/**
52+
* Returns a copy of the current process env with EMBER_, VITE_, and NODE_
53+
* prefixed vars removed.
54+
*
55+
* Useful when spawning child processes with `extendEnv: false` so that
56+
* vitest's NODE_ENV=test (and similar) doesn't leak into the child and
57+
* interfere with the build-time / runtime macro mode detection.
58+
*/
59+
export function safeExecaEnv(): Record<string, string | undefined> {
60+
let env = { ...process.env };
61+
62+
for (let key of Object.keys(env)) {
63+
if (key.startsWith('EMBER_') || key.startsWith('VITE_') || key.startsWith('NODE_')) {
64+
delete env[key];
65+
}
66+
}
67+
68+
return env;
69+
}
70+
5171
/**
5272
* Abstraction for install, as the blueprint supports multiple package managers
5373
*/

tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"@vitest/ui": "^0.18.0",
1111
"c8": "^7.11.3",
1212
"ember-cli": "github:ember-cli/ember-cli#master",
13-
"execa": "^9.5.2",
13+
"execa": "^9.6.0",
1414
"fixturify": "^3.0.0",
1515
"fs-extra": "^10.0.0",
1616
"globby": "^14.1.0",

tests/smoke-tests/--typescript.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { beforeAll, beforeEach, describe, expect, it } from 'vitest';
77

88
import {
99
assertGeneratedCorrectly,
10-
dirContents,
1110
filesMatching,
1211
SUPPORTED_PACKAGE_MANAGERS,
12+
safeExecaEnv,
1313
} from '../helpers.js';
1414
const blueprintPath = path.join(__dirname, '../..');
1515
let localEmberCli = require.resolve('ember-cli/bin/ember');
@@ -25,6 +25,8 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) {
2525
cwd: addonDir,
2626
shell: true,
2727
preferLocal: true,
28+
extendEnv: false,
29+
env: safeExecaEnv(),
2830
// Allows us to not fail yet when the command fails
2931
// but we'd still fail appropriately with the exitCode check below.
3032
// When we fail, we want to check for git diffs for debugging purposes.
@@ -50,13 +52,14 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) {
5052
addonDir = join(tmpDir, addonName);
5153
await execa({
5254
cwd: tmpDir,
55+
extendEnv: false,
5356
})`${localEmberCli} addon ${addonName} -b ${blueprintPath} --skip-npm --prefer-local true --${packageManager} --typescript`;
5457
// Have to use --force because NPM is *stricter* when you use tags in package.json
5558
// than pnpm (in that tags don't match any specified stable version)
5659
if (packageManager === 'npm') {
57-
await execa({ cwd: addonDir })`npm install --force`;
60+
await execa({ cwd: addonDir, extendEnv: false })`npm install --force`;
5861
} else if (packageManager === 'pnpm') {
59-
await execa({ cwd: addonDir })`pnpm install`;
62+
await execa({ cwd: addonDir, extendEnv: false })`pnpm install`;
6063
}
6164
});
6265

0 commit comments

Comments
 (0)