Skip to content

Commit

Permalink
fix: Use gts to Target Newer Versions of JavaScript (#164)
Browse files Browse the repository at this point in the history
* fix: Use Target `es2018` to Match `gts`

* style: lint

* feat: Allow custom `tsconfig` path + default to gts's

* docs: typo

* feat: Ensure ES2015 features are being used

* refactor: Extend provided `tsconfig.json`

* refactor: rename to `tsconfigPath`
  • Loading branch information
danielbankhead authored Oct 31, 2023
1 parent 6b92d3a commit 4c0b9a3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/pack-n-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export interface CodeSample {
interface TestOptions {
sample: CodeSample;
packageDir?: string;
/**
* Path to a `tsconfig.json` file
*/
tsconfigPath?: string;
}

interface Sample {
Expand Down Expand Up @@ -96,13 +100,18 @@ export async function pack(
return packageTarball;
}

/**
* `gts`'s `tsconfig.json`.
*/
const GTS_CONFIG_PATH = './node_modules/gts/tsconfig-google.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) {
Expand All @@ -113,15 +122,22 @@ 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 tsconfigPath = options.tsconfigPath || GTS_CONFIG_PATH;

const dependencies = sample.dependencies || [];
const devDependencies = sample.devDependencies || [];

if (sample.ts) {
devDependencies.push('typescript');
devDependencies.push('@types/node');

if (tsconfigPath === GTS_CONFIG_PATH) {
devDependencies.push('gts');
}
}

// Add dependencies, including tarball, to package.json.
Expand All @@ -138,15 +154,27 @@ export async function packNTest(options: TestOptions) {
{cwd: installDir}
);

// Poupulate test code.
// Populate 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', '--strict', 'index.ts'], {
cwd: installDir,
});
const testConfig = {
extends: tsconfigPath,
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});
}
}
}
10 changes: 10 additions & 0 deletions test/fixtures/pass/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
`,
},
Expand Down

0 comments on commit 4c0b9a3

Please sign in to comment.