From f9c6aac02adb421f849187b9b1ae20851b9097df Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Sat, 15 Jul 2023 11:02:03 -0400 Subject: [PATCH] Add type-tests --- files/__addonLocation__/package.json | 4 +++- files/__addonLocation__/tsconfig.json | 2 +- files/__addonLocation__/type-tests/index.test.ts | 4 ++++ files/__addonLocation__/type-tests/tsconfig.json | 6 ++++++ index.js | 2 ++ .../addon-only-ts/type-tests/index.test.ts | 4 ++++ tests/smoke-tests/--typescript.test.ts | 14 ++++++++++++-- tests/smoke-tests/defaults.test.ts | 7 ++++++- tests/vitest.config.ts | 1 + 9 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 files/__addonLocation__/type-tests/index.test.ts create mode 100644 files/__addonLocation__/type-tests/tsconfig.json create mode 100644 tests/fixtures/addon-only-ts/type-tests/index.test.ts diff --git a/files/__addonLocation__/package.json b/files/__addonLocation__/package.json index 03e50c64..148028cf 100644 --- a/files/__addonLocation__/package.json +++ b/files/__addonLocation__/package.json @@ -25,6 +25,7 @@ "lint:hbs:fix": "ember-template-lint . --fix --no-error-on-unmatched-pattern", "lint:js:fix": "eslint . --fix",<% if (typescript) { %> "lint:types": "glint", + "lint:types-tests": "glint --project type-tests", "start": "concurrently 'npm:start:*'", "start:js": "rollup --config --watch --no-watch.clearScreen", "start:types": "glint --declaration --watch",<% } else { %> @@ -73,7 +74,8 @@ "eslint-config-prettier": "^8.3.0", "eslint-plugin-ember": "^11.6.0", "eslint-plugin-n": "^16.0.0", - "eslint-plugin-prettier": "^4.0.0", + "eslint-plugin-prettier": "^4.0.0",<% if (typescript) { %> + "expect-type": "^0.16.0",<% } %> "prettier": "^2.5.1", "rollup": "^3.21.8"<% if (!isExistingMonorepo) { %>, "rollup-plugin-copy": "^3.4.0"<% } %><% if (typescript) { %>, diff --git a/files/__addonLocation__/tsconfig.json b/files/__addonLocation__/tsconfig.json index 2198eb8f..7752b2b1 100644 --- a/files/__addonLocation__/tsconfig.json +++ b/files/__addonLocation__/tsconfig.json @@ -8,6 +8,6 @@ "environment": "ember-loose" }, "compilerOptions": { - "declarationDir": "declarations" + "declarationDir": "declarations", } } diff --git a/files/__addonLocation__/type-tests/index.test.ts b/files/__addonLocation__/type-tests/index.test.ts new file mode 100644 index 00000000..306df4c6 --- /dev/null +++ b/files/__addonLocation__/type-tests/index.test.ts @@ -0,0 +1,4 @@ +import { expectTypeOf } from 'expect-type'; + +// Replace this with your real type tests +expectTypeOf(1).toEqualTypeOf(); diff --git a/files/__addonLocation__/type-tests/tsconfig.json b/files/__addonLocation__/type-tests/tsconfig.json new file mode 100644 index 00000000..253caeb2 --- /dev/null +++ b/files/__addonLocation__/type-tests/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "@tsconfig/ember/tsconfig.json", + "glint": { + "environment": "ember-loose" + }, +} diff --git a/index.js b/index.js index b47a5929..d08b8966 100644 --- a/index.js +++ b/index.js @@ -310,6 +310,8 @@ module.exports = { files = files.filter( (filename) => !filename.match(/.*\.ts$/) && !ignoredFiles.includes(filename) ); + + files = files.filter((filename) => !filename.includes('type-tests')); } if (this.isExistingMonorepo) { diff --git a/tests/fixtures/addon-only-ts/type-tests/index.test.ts b/tests/fixtures/addon-only-ts/type-tests/index.test.ts new file mode 100644 index 00000000..306df4c6 --- /dev/null +++ b/tests/fixtures/addon-only-ts/type-tests/index.test.ts @@ -0,0 +1,4 @@ +import { expectTypeOf } from 'expect-type'; + +// Replace this with your real type tests +expectTypeOf(1).toEqualTypeOf(); diff --git a/tests/smoke-tests/--typescript.test.ts b/tests/smoke-tests/--typescript.test.ts index 592c0141..8f54829c 100644 --- a/tests/smoke-tests/--typescript.test.ts +++ b/tests/smoke-tests/--typescript.test.ts @@ -13,6 +13,7 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { describe(`--typescript with ${packageManager}`, () => { let distDir = ''; let declarationsDir = ''; + let typeTestsDir = ''; let helper = new AddonHelper({ packageManager, args: ['--typescript'], @@ -25,6 +26,7 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { distDir = path.join(helper.addonFolder, 'dist'); declarationsDir = path.join(helper.addonFolder, 'declarations'); + typeTestsDir = path.join(helper.addonFolder, 'type-tests'); }); afterAll(async () => { @@ -34,15 +36,23 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { it('was generated correctly', async () => { await helper.build(); - assertGeneratedCorrectly({ projectRoot: helper.projectRoot }); + await assertGeneratedCorrectly({ projectRoot: helper.projectRoot }); + + let contents = await dirContents(helper.addonFolder); + + expect(contents).to.include('type-tests'); + expect(contents).to.include('tsconfig.json'); + + expect(await dirContents(typeTestsDir)).to.deep.equal(['index.test.ts', 'tsconfig.json']); }); // Tests are additive, so when running them in order, we want to check linting // before we add files from fixtures it('lints all pass', async () => { - let { exitCode } = await helper.run('lint'); + let { exitCode, stdout } = await helper.run('lint'); expect(exitCode).toEqual(0); + expect(stdout).to.include('lint:types-tests'); }); it('build and test', async () => { diff --git a/tests/smoke-tests/defaults.test.ts b/tests/smoke-tests/defaults.test.ts index d8986718..c6835567 100644 --- a/tests/smoke-tests/defaults.test.ts +++ b/tests/smoke-tests/defaults.test.ts @@ -65,7 +65,12 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { }); it('was generated correctly', async () => { - assertGeneratedCorrectly({ projectRoot: helper.projectRoot }); + await assertGeneratedCorrectly({ projectRoot: helper.projectRoot }); + + let contents = await dirContents(helper.addonFolder); + + expect(contents).to.not.include('type-tests'); + expect(contents).to.not.include('tsconfig.json'); }); // Tests are additive, so when running them in order, we want to check linting diff --git a/tests/vitest.config.ts b/tests/vitest.config.ts index f0f80f2d..45f2ae03 100644 --- a/tests/vitest.config.ts +++ b/tests/vitest.config.ts @@ -6,5 +6,6 @@ export default defineConfig({ test: { testTimeout: 60 * ONE_SECOND, hookTimeout: 150 * ONE_SECOND, + exclude: ['fixtures/**/*'], }, });