From f0a67e584cff79021b82952f55ee046e0858f648 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 | 3 +- files/__addonLocation__/tsconfig.json | 3 +- .../type-tests/index.test.ts | 4 ++ index.js | 2 + .../addon-only-ts/type-tests/index.test.ts | 4 ++ tests/smoke-tests/--typescript.test.ts | 42 +++++++++++++++++-- tests/smoke-tests/defaults.test.ts | 9 +++- tests/tsconfig.json | 3 +- tests/vitest.config.ts | 1 + 9 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 files/__addonLocation__/type-tests/index.test.ts 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 c5b390b2..a24f0b1a 100644 --- a/files/__addonLocation__/package.json +++ b/files/__addonLocation__/package.json @@ -74,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..63cf81dc 100644 --- a/files/__addonLocation__/tsconfig.json +++ b/files/__addonLocation__/tsconfig.json @@ -2,7 +2,8 @@ "extends": "@tsconfig/ember/tsconfig.json", "include": [ "src/**/*", - "unpublished-development-types/**/*" + "unpublished-development-types/**/*", + "type-tests/**/*" ], "glint": { "environment": "ember-loose" 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/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 57328eab..9a6c6273 100644 --- a/tests/smoke-tests/--typescript.test.ts +++ b/tests/smoke-tests/--typescript.test.ts @@ -2,7 +2,7 @@ import fs from 'node:fs/promises'; import path from 'node:path'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; -import { assertGeneratedCorrectly } from '../assertions.js'; +import { assertGeneratedCorrectly, matchesFixture } from '../assertions.js'; import { createAddon, createTmp, @@ -18,6 +18,8 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { let tmpDir = ''; let distDir = ''; let declarationsDir = ''; + let typeTests = ''; + let addonDir = ''; beforeAll(async () => { tmpDir = await createTmp(); @@ -28,6 +30,8 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { }); cwd = path.join(tmpDir, name); + addonDir = path.join(cwd, name); + typeTests = path.join(cwd, name, 'type-tests'); distDir = path.join(cwd, name, 'dist'); declarationsDir = path.join(cwd, name, 'declarations'); @@ -35,13 +39,43 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { }); afterAll(async () => { - await fs.rm(tmpDir, { recursive: true, force: true }); + // await fs.rm(tmpDir, { recursive: true, force: true }); }); it('was generated correctly', async () => { await runScript({ cwd, script: 'build', packageManager: 'pnpm' }); - assertGeneratedCorrectly({ projectRoot: cwd }); + await assertGeneratedCorrectly({ projectRoot: cwd }); + + let addonContents = await dirContents(addonDir); + + expect(addonContents).to.deep.equal([ + '.eslintignore', + '.eslintrc.cjs', + '.gitignore', + '.prettierignore', + '.prettierrc.js', + '.template-lintrc.cjs', + 'LICENSE.md', + 'README.md', + 'addon-main.cjs', + 'babel.config.json', + 'declarations', + 'dist', + 'node_modules', + 'package.json', + 'rollup.config.mjs', + 'src', + 'tsconfig.json', + 'type-tests', + 'unpublished-development-types', + ]); + + await matchesFixture(path.join(typeTests, 'index.test.ts'), { + cwd, + scenario: 'addon-only-ts', + file: 'type-tests/index.test.ts', + }); }); it('builds the addon', async () => { @@ -49,6 +83,8 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { expect(exitCode).toEqual(0); + console.log({ distDir, declarationsDir }); + let distContents = await dirContents(distDir); let declarationsContents = await dirContents(declarationsDir); diff --git a/tests/smoke-tests/defaults.test.ts b/tests/smoke-tests/defaults.test.ts index 063fc9ba..30a5a860 100644 --- a/tests/smoke-tests/defaults.test.ts +++ b/tests/smoke-tests/defaults.test.ts @@ -18,6 +18,7 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { let cwd = ''; let tmpDir = ''; let distDir = ''; + let addonDir = ''; beforeAll(async () => { tmpDir = await createTmp(); @@ -30,6 +31,7 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { }); cwd = path.join(tmpDir, name); + addonDir = path.join(cwd, name); distDir = path.join(cwd, name, 'dist'); await install({ cwd, packageManager }); @@ -79,7 +81,12 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { }); it('was generated correctly', async () => { - assertGeneratedCorrectly({ projectRoot: cwd }); + await assertGeneratedCorrectly({ projectRoot: cwd }); + + let contents = await dirContents(addonDir); + + expect(contents).to.not.include('type-tests'); + expect(contents).to.not.include('tsconfig.json'); }); it('builds the addon', async () => { diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 076f2a3e..13c97cd2 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -7,5 +7,6 @@ // Vite's internal types aren't relevant to us "skipLibCheck": true - } + }, + "exclude": ["fixtures/"] } 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/**/*'], }, });