From 600b5a473ddb35e753ee7a877dcb6a9b57523c3f Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Fri, 27 Oct 2023 10:58:46 -0700 Subject: [PATCH 1/7] fix: Use Target `es2018` to Match `gts` --- src/pack-n-test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pack-n-test.ts b/src/pack-n-test.ts index ebaa24c..5a56e2b 100644 --- a/src/pack-n-test.ts +++ b/src/pack-n-test.ts @@ -144,7 +144,7 @@ export async function packNTest(options: TestOptions) { if (sample.ts) { // TODO: maybe make it flexible for users to pass in typescript config. - await execa('npx', ['tsc', '--strict', 'index.ts'], { + await execa('npx', ['tsc', '--target', 'es2018', '--strict', 'index.ts'], { cwd: installDir, }); } From aea01c2fa5e5bd301822ef1b39c5e20bf27ac720 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Fri, 27 Oct 2023 11:04:32 -0700 Subject: [PATCH 2/7] style: lint --- src/pack-n-test.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/pack-n-test.ts b/src/pack-n-test.ts index 5a56e2b..54178f7 100644 --- a/src/pack-n-test.ts +++ b/src/pack-n-test.ts @@ -144,9 +144,13 @@ export async function packNTest(options: TestOptions) { if (sample.ts) { // TODO: maybe make it flexible for users to pass in typescript config. - await execa('npx', ['tsc', '--target', 'es2018', '--strict', 'index.ts'], { - cwd: installDir, - }); + await execa( + 'npx', + ['tsc', '--target', 'es2018', '--strict', 'index.ts'], + { + cwd: installDir, + } + ); } } } From 32b2638342c353bd181136ffa7b16cf2c79c0269 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Fri, 27 Oct 2023 11:49:02 -0700 Subject: [PATCH 3/7] feat: Allow custom `tsconfig` path + default to gts's --- src/pack-n-test.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/pack-n-test.ts b/src/pack-n-test.ts index 54178f7..8aa747f 100644 --- a/src/pack-n-test.ts +++ b/src/pack-n-test.ts @@ -48,6 +48,10 @@ export interface CodeSample { interface TestOptions { sample: CodeSample; packageDir?: string; + /** + * Path to a `tsconfig.json` file + */ + tsconfig?: string; } interface Sample { @@ -96,13 +100,18 @@ export async function pack( return packageTarball; } +/** + * `gts`'s `tsconfig.json`. + */ +const GTS_CONFIG_PATH = './node_modules/gts/tsconfig.json'; + export async function packNTest(options: TestOptions) { const moduleUnderTest = options.packageDir || process.cwd(); const installDir = tmp.dirSync().name; try { const tarball = await pack(moduleUnderTest, installDir); - await prepareTarget(tarball, options.sample); + await prepareTarget(tarball); const filename = getExecFilename(options.sample); await execa('node', [filename], {cwd: installDir}); } catch (err) { @@ -113,15 +122,21 @@ export async function packNTest(options: TestOptions) { } return; - async function prepareTarget(tarball: string, sample: CodeSample) { + async function prepareTarget(tarball: string) { // Generate a package.json. await execa('npm', ['init', '-y'], {cwd: installDir}); + const sample = options.sample; + const tsconfig = options.tsconfig || GTS_CONFIG_PATH; const dependencies = sample.dependencies || []; const devDependencies = sample.devDependencies || []; if (sample.ts) { devDependencies.push('typescript'); + + if (tsconfig === GTS_CONFIG_PATH) { + devDependencies.push('gts'); + } } // Add dependencies, including tarball, to package.json. @@ -138,15 +153,14 @@ export async function packNTest(options: TestOptions) { {cwd: installDir} ); - // Poupulate test code. + // Popupulate test code. const {code, filename} = getSample(sample); await writeFile(path.join(installDir, filename), code, 'utf-8'); if (sample.ts) { - // TODO: maybe make it flexible for users to pass in typescript config. await execa( 'npx', - ['tsc', '--target', 'es2018', '--strict', 'index.ts'], + ['tsc', '--project', tsconfig, '--strict', 'index.ts'], { cwd: installDir, } From 7e2ec55dd6ed0f278b5bbe7c6fe500581a0d4e05 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Fri, 27 Oct 2023 11:50:12 -0700 Subject: [PATCH 4/7] docs: typo --- src/pack-n-test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pack-n-test.ts b/src/pack-n-test.ts index 8aa747f..06b86ae 100644 --- a/src/pack-n-test.ts +++ b/src/pack-n-test.ts @@ -153,7 +153,7 @@ export async function packNTest(options: TestOptions) { {cwd: installDir} ); - // Popupulate test code. + // Populate test code. const {code, filename} = getSample(sample); await writeFile(path.join(installDir, filename), code, 'utf-8'); From 9ba353c63dfcf29109fe128c5dc5a70a4a64546a Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Mon, 30 Oct 2023 16:41:57 -0700 Subject: [PATCH 5/7] feat: Ensure ES2015 features are being used --- test/fixtures/pass/test/test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/fixtures/pass/test/test.ts b/test/fixtures/pass/test/test.ts index 306cfc2..2dd02fb 100644 --- a/test/fixtures/pass/test/test.ts +++ b/test/fixtures/pass/test/test.ts @@ -22,6 +22,16 @@ describe('passing tests', () => { description: 'basic passing sample', ts: ` import {doStuff} from 'pass'; + + export class Example { + // ES 2015+ feature + #value = 0; + + getValue () { + return this.#value; + } + } + doStuff().then(console.log); `, }, From 27f89a235cda11d47cdfd9143e1364daa24683e7 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Mon, 30 Oct 2023 16:42:58 -0700 Subject: [PATCH 6/7] refactor: Extend provided `tsconfig.json` --- src/pack-n-test.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/pack-n-test.ts b/src/pack-n-test.ts index 06b86ae..9e0e743 100644 --- a/src/pack-n-test.ts +++ b/src/pack-n-test.ts @@ -103,7 +103,7 @@ export async function pack( /** * `gts`'s `tsconfig.json`. */ -const GTS_CONFIG_PATH = './node_modules/gts/tsconfig.json'; +const GTS_CONFIG_PATH = './node_modules/gts/tsconfig-google.json'; export async function packNTest(options: TestOptions) { const moduleUnderTest = options.packageDir || process.cwd(); @@ -133,6 +133,7 @@ export async function packNTest(options: TestOptions) { if (sample.ts) { devDependencies.push('typescript'); + devDependencies.push('@types/node'); if (tsconfig === GTS_CONFIG_PATH) { devDependencies.push('gts'); @@ -158,13 +159,22 @@ export async function packNTest(options: TestOptions) { await writeFile(path.join(installDir, filename), code, 'utf-8'); if (sample.ts) { - await execa( - 'npx', - ['tsc', '--project', tsconfig, '--strict', 'index.ts'], - { - cwd: installDir, - } + const testConfig = { + extends: tsconfig, + files: ['index.ts'], + compilerOptions: { + rootDir: '.', + resolveJsonModule: true, + }, + }; + + // this is the config `tsc` will use for compilation locally. + await writeFile( + path.join(installDir, 'tsconfig.json'), + JSON.stringify(testConfig) ); + + await execa('npx', ['tsc'], {cwd: installDir}); } } } From af42ac5445433f1a33dea25612f61662a471a6ba Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Mon, 30 Oct 2023 17:41:07 -0700 Subject: [PATCH 7/7] refactor: rename to `tsconfigPath` --- src/pack-n-test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pack-n-test.ts b/src/pack-n-test.ts index 9e0e743..64f9203 100644 --- a/src/pack-n-test.ts +++ b/src/pack-n-test.ts @@ -51,7 +51,7 @@ interface TestOptions { /** * Path to a `tsconfig.json` file */ - tsconfig?: string; + tsconfigPath?: string; } interface Sample { @@ -126,7 +126,7 @@ export async function packNTest(options: TestOptions) { // Generate a package.json. await execa('npm', ['init', '-y'], {cwd: installDir}); const sample = options.sample; - const tsconfig = options.tsconfig || GTS_CONFIG_PATH; + const tsconfigPath = options.tsconfigPath || GTS_CONFIG_PATH; const dependencies = sample.dependencies || []; const devDependencies = sample.devDependencies || []; @@ -135,7 +135,7 @@ export async function packNTest(options: TestOptions) { devDependencies.push('typescript'); devDependencies.push('@types/node'); - if (tsconfig === GTS_CONFIG_PATH) { + if (tsconfigPath === GTS_CONFIG_PATH) { devDependencies.push('gts'); } } @@ -160,7 +160,7 @@ export async function packNTest(options: TestOptions) { if (sample.ts) { const testConfig = { - extends: tsconfig, + extends: tsconfigPath, files: ['index.ts'], compilerOptions: { rootDir: '.',