Skip to content

Commit

Permalink
test: add parse-tsconfig spec
Browse files Browse the repository at this point in the history
- test passing tsconfig, buggy tsconfig, non-existent tsconfig, and not a tsconfig
- clean: "failed to read" error will never happen as we already checked for existence of the file earlier
  - so remove the `undefined` check and instead use a non-null assertion (plus a comment explaining it)

- refactor: move the integration test for tsconfig error into this unit test instead
  - faster / more efficient / more precise

- refactor: split out a `makeOptions` func that creates default plugin options to use in tests
  - similar to `makeStubbedContext`
  • Loading branch information
agilgur5 committed Jul 22, 2022
1 parent e5e3ccd commit 53b11df
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 38 deletions.
32 changes: 32 additions & 0 deletions __tests__/fixtures/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as ts from "typescript";

import { setTypescriptModule } from "../../src/tsproxy";
import { IOptions } from "../../src/ioptions";

setTypescriptModule(ts);

export function makeOptions(cacheDir: string, cwd: string): IOptions {
return {
include: ["*.ts+(|x)", "**/*.ts+(|x)"],
exclude: ["*.d.ts", "**/*.d.ts"],
check: false,
verbosity: 5,
clean: false,
cacheRoot: cacheDir,
cwd,
abortOnError: false,
rollupCommonJSResolveHack: false,
typescript: ts,
objectHashIgnoreUnknownHack: false,
tsconfigOverride: null,
useTsconfigDeclarationDir: false,
tsconfigDefaults: null,
sourceMapCallback: (id: string, map: string): void => {
console.log(id + map);
},
transformers: [(ls: ts.LanguageService) => {
console.log(ls);
return {};
}],
};
}
29 changes: 2 additions & 27 deletions __tests__/get-options-overrides.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import * as ts from "typescript";
import { normalizePath as normalize } from "@rollup/pluginutils";
import { remove } from "fs-extra";

import { makeOptions } from "./fixtures/options";
import { makeStubbedContext } from "./fixtures/context";
import { setTypescriptModule } from "../src/tsproxy";
import { IOptions } from "../src/ioptions";
import { getOptionsOverrides, createFilter } from "../src/get-options-overrides";

setTypescriptModule(ts);

const local = (x: string) => normalize(path.resolve(__dirname, x));
const cacheDir = local("__temp/get-options-overrides");

Expand All @@ -19,29 +16,7 @@ const filtPath = (relPath: string) => normalize(`${process.cwd()}/${relPath}`);

afterAll(() => remove(cacheDir));

const defaultConfig: IOptions = {
include: ["*.ts+(|x)", "**/*.ts+(|x)"],
exclude: ["*.d.ts", "**/*.d.ts"],
check: false,
verbosity: 5,
clean: false,
cacheRoot: cacheDir,
cwd: local(""),
abortOnError: false,
rollupCommonJSResolveHack: false,
typescript: ts,
objectHashIgnoreUnknownHack: false,
tsconfigOverride: null,
useTsconfigDeclarationDir: false,
tsconfigDefaults: null,
sourceMapCallback: (id: string, map: string): void => {
console.log(id + map);
},
transformers: [(ls: ts.LanguageService) => {
console.log(ls);
return {};
}],
};
const defaultConfig = makeOptions(cacheDir, local(""));

const forcedOptions: ts.CompilerOptions = {
allowNonTsExtensions: true,
Expand Down
7 changes: 0 additions & 7 deletions __tests__/integration/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ async function genBundle(relInput: string, extraOpts?: RPT2Options, onwarn?: Moc
});
}

test("integration - tsconfig errors", async () => {
// TODO: move to parse-tsconfig unit tests?
expect(genBundle("semantic.ts", {
tsconfig: "non-existent-tsconfig",
})).rejects.toThrow("rpt2: failed to open 'non-existent-tsconfig'");
});

test("integration - semantic error", async () => {
expect(genBundle("semantic.ts")).rejects.toThrow("Type 'string' is not assignable to type 'number'.");
});
Expand Down
44 changes: 44 additions & 0 deletions __tests__/parse-tsconfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { test, expect } from "@jest/globals";
import * as path from "path";

import { makeOptions } from "./fixtures/options";
import { makeStubbedContext } from "./fixtures/context";
import { parseTsConfig } from "../src/parse-tsconfig";

const local = (x: string) => path.resolve(__dirname, x);

const defaultOpts = makeOptions("", "");
const stubbedContext = makeStubbedContext({});

test("parseTsConfig", () => {
expect(() => parseTsConfig(stubbedContext, defaultOpts)).not.toThrow();
});

test("parseTsConfig - tsconfig errors", () => {
const data = { error: "" };

// should not throw when the tsconfig is buggy, but should still print an error (below)
expect(() => parseTsConfig(makeStubbedContext(data), {
...defaultOpts,
tsconfigOverride: {
include: "should-be-an-array",
},
})).not.toThrow();
expect(data.error).toMatch("Compiler option 'include' requires a value of type Array");
});

test("parseTsConfig - failed to open", () => {
expect(() => parseTsConfig(stubbedContext, {
...defaultOpts,
tsconfig: "non-existent-tsconfig",
})).toThrow("rpt2: failed to open 'non-existent-tsconfig'");
});

test("parseTsConfig - failed to parse", () => {
const notTsConfigPath = local("fixtures/options.ts"); // a TS file should fail to parse

expect(() => parseTsConfig(stubbedContext, {
...defaultOpts,
tsconfig: notTsConfigPath,
})).toThrow(`rpt2: failed to parse '${notTsConfigPath}'`);
});
5 changes: 1 addition & 4 deletions src/parse-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ export function parseTsConfig(context: IContext, pluginOptions: IOptions)
let pretty = true;
if (fileName)
{
const text = tsModule.sys.readFile(fileName);
if (text === undefined)
throw new Error(`rpt2: failed to read '${fileName}'`);

const text = tsModule.sys.readFile(fileName)!; // readFile only returns undefined when the file doesn't exist, which we already checked above
const result = tsModule.parseConfigFileTextToJson(fileName, text);
pretty = result.config?.pretty ?? pretty;

Expand Down

0 comments on commit 53b11df

Please sign in to comment.