diff --git a/.gitignore b/.gitignore index 93cd668..a036415 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /node_modules/ .log/ -/my-addon/ +my-addon/ diff --git a/.prettierignore b/.prettierignore index 0d5f7e7..84071d5 100644 --- a/.prettierignore +++ b/.prettierignore @@ -3,6 +3,7 @@ *.md files/ tests/fixtures/ +my-addon/ node_modules/ diff --git a/files/gitignore b/files/gitignore index cb26594..56a3f29 100644 --- a/files/gitignore +++ b/files/gitignore @@ -15,3 +15,7 @@ ember-cli-build.cjs node_modules/ .eslintcache .prettiercache + +# potentially containing secrets +.env +.env.*.local diff --git a/files/tests/test-helper.__ext__ b/files/tests/test-helper.__ext__ index ac6a374..f10b5ad 100644 --- a/files/tests/test-helper.__ext__ +++ b/files/tests/test-helper.__ext__ @@ -4,6 +4,8 @@ import * as QUnit from 'qunit'; import { setApplication } from '@ember/test-helpers'; import { setup } from 'qunit-dom'; import { start as qunitStart, setupEmberOnerrorValidation } from 'ember-qunit'; +<% if (typescript) { %>// @ts-expect-error @embroider/macros does not provide types +<% } %>import { getGlobalConfig } from '@embroider/macros/src/addon/runtime'; class Router extends EmberRouter { location = 'none'; @@ -21,6 +23,14 @@ class TestApp extends EmberApp { Router.map(function () {}); export function start() { + <% if (typescript) { %>// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call + <% } %>const theMacrosGlobal = getGlobalConfig(); + + <% if (typescript) { %>// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + <% } %>theMacrosGlobal['@embroider/macros'] ||= {}; + <% if (typescript) { %>// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + <% } %>theMacrosGlobal['@embroider/macros'].isTesting ||= true; + setApplication( TestApp.create({ autoboot: false, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 68060fd..587a640 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -37,8 +37,8 @@ importers: specifier: github:ember-cli/ember-cli#master version: https://codeload.github.com/ember-cli/ember-cli/tar.gz/8ea8eb59e8f32682c538f48f40657d708ad325cc(handlebars@4.7.8)(underscore@1.13.7) execa: - specifier: ^9.5.2 - version: 9.5.2 + specifier: ^9.6.0 + version: 9.6.0 fixturify: specifier: ^3.0.0 version: 3.0.0 @@ -1927,8 +1927,8 @@ packages: resolution: {integrity: sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==} engines: {node: '>=10'} - execa@9.5.2: - resolution: {integrity: sha512-EHlpxMCpHWSAh1dgS6bVeoLAXGnJNdR93aabr4QCGbzOM73o5XmRfM/e5FUqsw3aagP8S8XEWUWFAxnRBnAF0Q==} + execa@9.6.0: + resolution: {integrity: sha512-jpWzZ1ZhwUmeWRhS7Qv3mhpOhLfwI+uAX4e5fOcXqwMR7EcJ0pj2kV1CVzHVMX/LphnKWD3LObjZCoJ71lKpHw==} engines: {node: ^18.19.0 || >=20.5.0} exit@0.1.2: @@ -2371,8 +2371,8 @@ packages: resolution: {integrity: sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==} engines: {node: '>=10.17.0'} - human-signals@8.0.0: - resolution: {integrity: sha512-/1/GPCpDUCCYwlERiYjxoczfP0zfvZMU/OWgQPMya9AbAE24vseigFdhAMObpc8Q4lc/kjutPfUddDYyAmejnA==} + human-signals@8.0.1: + resolution: {integrity: sha512-eKCa6bwnJhvxj14kZk5NCPc6Hb6BdsU9DZcOnmQKSnO1VKrfV0zCvtttPZUsBvjmNDn8rpcJfpwSYnHBjc95MQ==} engines: {node: '>=18.18.0'} humanize-ms@1.2.1: @@ -6303,13 +6303,13 @@ snapshots: signal-exit: 3.0.7 strip-final-newline: 2.0.0 - execa@9.5.2: + execa@9.6.0: dependencies: '@sindresorhus/merge-streams': 4.0.0 cross-spawn: 7.0.6 figures: 6.1.0 get-stream: 9.0.1 - human-signals: 8.0.0 + human-signals: 8.0.1 is-plain-obj: 4.1.0 is-stream: 4.0.1 npm-run-path: 6.0.0 @@ -6940,7 +6940,7 @@ snapshots: human-signals@2.1.0: {} - human-signals@8.0.0: {} + human-signals@8.0.1: {} humanize-ms@1.2.1: dependencies: @@ -7915,7 +7915,7 @@ snapshots: assert-never: 1.4.0 chalk: 5.4.1 cli-highlight: 2.1.11 - execa: 9.5.2 + execa: 9.6.0 fs-extra: 11.3.0 github-changelog: 2.0.0 js-yaml: 4.1.0 diff --git a/tests/fixtures/build-mode-tests/debug-utils-test.js b/tests/fixtures/build-mode-tests/debug-utils-test.js new file mode 100644 index 0000000..a7d1dec --- /dev/null +++ b/tests/fixtures/build-mode-tests/debug-utils-test.js @@ -0,0 +1,33 @@ +import { test, module } from 'qunit'; +import { assert } from '@ember/debug'; +import { DEBUG } from '@glimmer/env'; +import { isDevelopingApp, isTesting } from '@embroider/macros'; + +module('debug utils remain in the build', function () { + test('assert', function(qAssert) { + // If we get the build mode wrong, e.g.: `NODE_ENV` != 'development' + // then the assert won't exist, causing qAssert to not detect a thrown Error + qAssert.throws(() => { + assert('should throw'); + }, /should throw/, `The error "should throw" is thrown`); + }); + + test('isTesting', function (assert) { + assert.strictEqual(isTesting(), true, `isTesting() === true`); + }); + + test('isDevelopingApp', function (assert) { + assert.strictEqual(isDevelopingApp(), true, `isDevelopingApp() === true`); + }); + + + module('not supported', function () { + test('DEBUG', function (assert) { + if (DEBUG) { + assert.step('DEBUG'); + } + + assert.verifySteps([]); + }); + }); +}); diff --git a/tests/package.json b/tests/package.json index e42c56c..a9f28e2 100644 --- a/tests/package.json +++ b/tests/package.json @@ -10,7 +10,7 @@ "@vitest/ui": "^0.18.0", "c8": "^7.11.3", "ember-cli": "github:ember-cli/ember-cli#master", - "execa": "^9.5.2", + "execa": "^9.6.0", "fixturify": "^3.0.0", "fs-extra": "^10.0.0", "globby": "^14.1.0", diff --git a/tests/smoke-tests/--typescript.test.ts b/tests/smoke-tests/--typescript.test.ts index 6bfc500..c5edfb0 100644 --- a/tests/smoke-tests/--typescript.test.ts +++ b/tests/smoke-tests/--typescript.test.ts @@ -5,12 +5,7 @@ import fixturify from 'fixturify'; import { execa } from 'execa'; import { beforeAll, beforeEach, describe, expect, it } from 'vitest'; -import { - assertGeneratedCorrectly, - dirContents, - filesMatching, - SUPPORTED_PACKAGE_MANAGERS, -} from '../helpers.js'; +import { assertGeneratedCorrectly, filesMatching, SUPPORTED_PACKAGE_MANAGERS } from '../helpers.js'; const blueprintPath = path.join(__dirname, '../..'); let localEmberCli = require.resolve('ember-cli/bin/ember'); @@ -92,56 +87,56 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { await commandSucceeds(`${packageManager} run build`); expect( - await filesMatching('src/**', addonDir), + (await filesMatching('src/**', addonDir)).sort(), `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)`, ).toMatchInlineSnapshot(` [ + "src/components/another-gts.gts", + "src/components/template-import.gts", "src/index.ts", "src/lint-test-gts.gts", "src/lint-test-ts.ts", - "src/template-registry.ts", - "src/components/another-gts.gts", - "src/components/template-import.gts", "src/services/example.ts", + "src/template-registry.ts", ] `); expect( - await filesMatching('{dist,declarations}/**/*', addonDir), + (await filesMatching('{dist,declarations}/**/*', addonDir)).sort(), `ensure we emit the correct files out of the box to the correct folders`, ).toMatchInlineSnapshot(` [ - "dist/index.js", - "dist/index.js.map", - "dist/lint-test-gts.js", - "dist/lint-test-gts.js.map", - "dist/lint-test-ts.js", - "dist/lint-test-ts.js.map", - "dist/template-registry.js", - "dist/template-registry.js.map", - "dist/components/another-gts.js", - "dist/components/another-gts.js.map", - "dist/components/template-import.js", - "dist/components/template-import.js.map", - "dist/services/example.js", - "dist/services/example.js.map", - "dist/_app_/components/another-gts.js", - "dist/_app_/components/template-import.js", - "dist/_app_/services/example.js", + "declarations/components/another-gts.d.ts", + "declarations/components/another-gts.d.ts.map", + "declarations/components/template-import.d.ts", + "declarations/components/template-import.d.ts.map", "declarations/index.d.ts", "declarations/index.d.ts.map", "declarations/lint-test-gts.d.ts", "declarations/lint-test-gts.d.ts.map", "declarations/lint-test-ts.d.ts", "declarations/lint-test-ts.d.ts.map", - "declarations/template-registry.d.ts", - "declarations/template-registry.d.ts.map", - "declarations/components/another-gts.d.ts", - "declarations/components/another-gts.d.ts.map", - "declarations/components/template-import.d.ts", - "declarations/components/template-import.d.ts.map", "declarations/services/example.d.ts", "declarations/services/example.d.ts.map", + "declarations/template-registry.d.ts", + "declarations/template-registry.d.ts.map", + "dist/_app_/components/another-gts.js", + "dist/_app_/components/template-import.js", + "dist/_app_/services/example.js", + "dist/components/another-gts.js", + "dist/components/another-gts.js.map", + "dist/components/template-import.js", + "dist/components/template-import.js.map", + "dist/index.js", + "dist/index.js.map", + "dist/lint-test-gts.js", + "dist/lint-test-gts.js.map", + "dist/lint-test-ts.js", + "dist/lint-test-ts.js.map", + "dist/services/example.js", + "dist/services/example.js.map", + "dist/template-registry.js", + "dist/template-registry.js.map", ] `); }); diff --git a/tests/smoke-tests/defaults.test.ts b/tests/smoke-tests/defaults.test.ts index 80f6571..e0d9f3f 100644 --- a/tests/smoke-tests/defaults.test.ts +++ b/tests/smoke-tests/defaults.test.ts @@ -38,8 +38,9 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { addonDir = join(tmpDir, addonName); await execa({ cwd: tmpDir, + extendEnv: false, })`${localEmberCli} addon ${addonName} -b ${blueprintPath} --skip-npm --skip-git --prefer-local true --${packageManager}`; - await execa({ cwd: addonDir })`${packageManager} install`; + await execa({ cwd: addonDir, extendEnv: false })`${packageManager} install`; }); it('is using the correct packager', async () => { @@ -99,13 +100,16 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { // Tests are additive, so when running them in order, we want to check linting // before we add files from fixtures it('lints with no fixtures all pass', async () => { - let { exitCode } = await execa({ cwd: addonDir })`pnpm lint`; + let { exitCode } = await execa({ cwd: addonDir, extendEnv: false })`pnpm lint`; expect(exitCode).toEqual(0); }); it('lint:fix with no fixtures', async () => { - let { exitCode } = await execa({ cwd: addonDir })`${packageManager} run lint:fix`; + let { exitCode } = await execa({ + cwd: addonDir, + extendEnv: false, + })`${packageManager} run lint:fix`; expect(exitCode).toEqual(0); }); @@ -124,16 +128,27 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { let testFixture = fixturify.readSync('./fixtures/rendering-tests'); fixturify.writeSync(join(addonDir, 'tests/rendering'), testFixture); + + fixturify.writeSync( + join(addonDir, 'tests/unit'), + fixturify.readSync('./fixtures/build-mode-tests'), + ); }); it('lint:fix', async () => { - let { exitCode } = await execa({ cwd: addonDir })`${packageManager} run lint:fix`; + let { exitCode } = await execa({ + cwd: addonDir, + extendEnv: false, + })`${packageManager} run lint:fix`; expect(exitCode).toEqual(0); }); it('build', async () => { - let buildResult = await execa({ cwd: addonDir })`${packageManager} run build`; + let buildResult = await execa({ + cwd: addonDir, + extendEnv: false, + })`${packageManager} run build`; expect(buildResult.exitCode).toEqual(0); @@ -146,13 +161,31 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { // It's important that we ensure that dist directory is empty for this test, because await fs.rm(join(addonDir, 'dist'), { recursive: true, force: true }); - let testResult = await execa({ cwd: addonDir })`${packageManager} run test`; + let safeEnv = { ...process.env }; + for (let key of Object.keys(safeEnv)) { + if (key.startsWith('EMBER_') || key.startsWith('VITE_') || key.startsWith('NODE_')) { + delete safeEnv[key]; + } + } + + let testResult = await execa({ + cwd: addonDir, + extendEnv: false, + // a modified env required with extendEnv, else we still inherit/merge the env of vitest + // which overrides our NODE_ENV from our .env.development file (read by vite) + // (because in-shell ENV vars override .env files) + env: safeEnv, + })`${packageManager} run test`; expect(testResult.exitCode).toEqual(0); + expect(testResult.stdout).includes('debug utils remain in the build: assert'); + expect(testResult.stdout).includes( + 'debug utils remain in the build > not supported: DEBUG', + ); expect(testResult.stdout).includes( - `# tests 2 -# pass 2 + `# tests 6 +# pass 6 # skip 0 # todo 0 # fail 0