Skip to content

Commit 6eb50a8

Browse files
committed
Add build mode tests, use setTetsing for tests
1 parent 26f4dd5 commit 6eb50a8

11 files changed

Lines changed: 142 additions & 52 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ ember-cli-build.cjs
1515
node_modules/
1616
.eslintcache
1717
.prettiercache
18+
19+
# potentially containing secrets
20+
.env
21+
.env.*.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: 34 additions & 31 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

@@ -92,56 +95,56 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) {
9295
await commandSucceeds(`${packageManager} run build`);
9396

9497
expect(
95-
await filesMatching('src/**', addonDir),
98+
(await filesMatching('src/**', addonDir)).sort(),
9699
`ensure we don't pollute the src dir with declarations and emit the js and .d.ts to the correct folders -- this should be the same as the input files (no change from the fixture + default files)`,
97100
).toMatchInlineSnapshot(`
98101
[
102+
"src/components/another-gts.gts",
103+
"src/components/template-import.gts",
99104
"src/index.ts",
100105
"src/lint-test-gts.gts",
101106
"src/lint-test-ts.ts",
102-
"src/template-registry.ts",
103-
"src/components/another-gts.gts",
104-
"src/components/template-import.gts",
105107
"src/services/example.ts",
108+
"src/template-registry.ts",
106109
]
107110
`);
108111

109112
expect(
110-
await filesMatching('{dist,declarations}/**/*', addonDir),
113+
(await filesMatching('{dist,declarations}/**/*', addonDir)).sort(),
111114
`ensure we emit the correct files out of the box to the correct folders`,
112115
).toMatchInlineSnapshot(`
113116
[
114-
"dist/index.js",
115-
"dist/index.js.map",
116-
"dist/lint-test-gts.js",
117-
"dist/lint-test-gts.js.map",
118-
"dist/lint-test-ts.js",
119-
"dist/lint-test-ts.js.map",
120-
"dist/template-registry.js",
121-
"dist/template-registry.js.map",
122-
"dist/components/another-gts.js",
123-
"dist/components/another-gts.js.map",
124-
"dist/components/template-import.js",
125-
"dist/components/template-import.js.map",
126-
"dist/services/example.js",
127-
"dist/services/example.js.map",
128-
"dist/_app_/components/another-gts.js",
129-
"dist/_app_/components/template-import.js",
130-
"dist/_app_/services/example.js",
117+
"declarations/components/another-gts.d.ts",
118+
"declarations/components/another-gts.d.ts.map",
119+
"declarations/components/template-import.d.ts",
120+
"declarations/components/template-import.d.ts.map",
131121
"declarations/index.d.ts",
132122
"declarations/index.d.ts.map",
133123
"declarations/lint-test-gts.d.ts",
134124
"declarations/lint-test-gts.d.ts.map",
135125
"declarations/lint-test-ts.d.ts",
136126
"declarations/lint-test-ts.d.ts.map",
137-
"declarations/template-registry.d.ts",
138-
"declarations/template-registry.d.ts.map",
139-
"declarations/components/another-gts.d.ts",
140-
"declarations/components/another-gts.d.ts.map",
141-
"declarations/components/template-import.d.ts",
142-
"declarations/components/template-import.d.ts.map",
143127
"declarations/services/example.d.ts",
144128
"declarations/services/example.d.ts.map",
129+
"declarations/template-registry.d.ts",
130+
"declarations/template-registry.d.ts.map",
131+
"dist/_app_/components/another-gts.js",
132+
"dist/_app_/components/template-import.js",
133+
"dist/_app_/services/example.js",
134+
"dist/components/another-gts.js",
135+
"dist/components/another-gts.js.map",
136+
"dist/components/template-import.js",
137+
"dist/components/template-import.js.map",
138+
"dist/index.js",
139+
"dist/index.js.map",
140+
"dist/lint-test-gts.js",
141+
"dist/lint-test-gts.js.map",
142+
"dist/lint-test-ts.js",
143+
"dist/lint-test-ts.js.map",
144+
"dist/services/example.js",
145+
"dist/services/example.js.map",
146+
"dist/template-registry.js",
147+
"dist/template-registry.js.map",
145148
]
146149
`);
147150
});

0 commit comments

Comments
 (0)