Skip to content

Commit 1d7b058

Browse files
committed
Refactor tests to use safeExecaEnv for environment management
1 parent f9dbcfe commit 1d7b058

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

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/smoke-tests/--typescript.test.ts

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

8-
import { assertGeneratedCorrectly, filesMatching, SUPPORTED_PACKAGE_MANAGERS } from '../helpers.js';
8+
import { assertGeneratedCorrectly, filesMatching, SUPPORTED_PACKAGE_MANAGERS, safeExecaEnv } from '../helpers.js';
99
const blueprintPath = path.join(__dirname, '../..');
1010
let localEmberCli = require.resolve('ember-cli/bin/ember');
1111

@@ -20,6 +20,8 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) {
2020
cwd: addonDir,
2121
shell: true,
2222
preferLocal: true,
23+
extendEnv: false,
24+
env: safeExecaEnv(),
2325
// Allows us to not fail yet when the command fails
2426
// but we'd still fail appropriately with the exitCode check below.
2527
// When we fail, we want to check for git diffs for debugging purposes.
@@ -45,13 +47,14 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) {
4547
addonDir = join(tmpDir, addonName);
4648
await execa({
4749
cwd: tmpDir,
50+
extendEnv: false,
4851
})`${localEmberCli} addon ${addonName} -b ${blueprintPath} --skip-npm --prefer-local true --${packageManager} --typescript`;
4952
// Have to use --force because NPM is *stricter* when you use tags in package.json
5053
// than pnpm (in that tags don't match any specified stable version)
5154
if (packageManager === 'npm') {
52-
await execa({ cwd: addonDir })`npm install --force`;
55+
await execa({ cwd: addonDir, extendEnv: false })`npm install --force`;
5356
} else if (packageManager === 'pnpm') {
54-
await execa({ cwd: addonDir })`pnpm install`;
57+
await execa({ cwd: addonDir, extendEnv: false })`pnpm install`;
5558
}
5659
});
5760

tests/smoke-tests/defaults.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
dirContents,
1515
matchesFixture,
1616
packageJsonAt,
17+
safeExecaEnv,
1718
SUPPORTED_PACKAGE_MANAGERS,
1819
} from '../helpers.js';
1920
import { existsSync } from 'node:fs';
@@ -161,20 +162,13 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) {
161162
// It's important that we ensure that dist directory is empty for this test, because
162163
await fs.rm(join(addonDir, 'dist'), { recursive: true, force: true });
163164

164-
let safeEnv = { ...process.env };
165-
for (let key of Object.keys(safeEnv)) {
166-
if (key.startsWith('EMBER_') || key.startsWith('VITE_') || key.startsWith('NODE_')) {
167-
delete safeEnv[key];
168-
}
169-
}
170-
171165
let testResult = await execa({
172166
cwd: addonDir,
173167
extendEnv: false,
174168
// a modified env required with extendEnv, else we still inherit/merge the env of vitest
175169
// which overrides our NODE_ENV from our .env.development file (read by vite)
176170
// (because in-shell ENV vars override .env files)
177-
env: safeEnv,
171+
env: safeExecaEnv(),
178172
})`${packageManager} run test`;
179173

180174
expect(testResult.exitCode).toEqual(0);

0 commit comments

Comments
 (0)