Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use gts to Target Newer Versions of JavaScript #164

Merged
merged 7 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading