Skip to content

Commit 53b11df

Browse files
committed
test: add parse-tsconfig spec
- 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`
1 parent e5e3ccd commit 53b11df

File tree

5 files changed

+79
-38
lines changed

5 files changed

+79
-38
lines changed

__tests__/fixtures/options.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as ts from "typescript";
2+
3+
import { setTypescriptModule } from "../../src/tsproxy";
4+
import { IOptions } from "../../src/ioptions";
5+
6+
setTypescriptModule(ts);
7+
8+
export function makeOptions(cacheDir: string, cwd: string): IOptions {
9+
return {
10+
include: ["*.ts+(|x)", "**/*.ts+(|x)"],
11+
exclude: ["*.d.ts", "**/*.d.ts"],
12+
check: false,
13+
verbosity: 5,
14+
clean: false,
15+
cacheRoot: cacheDir,
16+
cwd,
17+
abortOnError: false,
18+
rollupCommonJSResolveHack: false,
19+
typescript: ts,
20+
objectHashIgnoreUnknownHack: false,
21+
tsconfigOverride: null,
22+
useTsconfigDeclarationDir: false,
23+
tsconfigDefaults: null,
24+
sourceMapCallback: (id: string, map: string): void => {
25+
console.log(id + map);
26+
},
27+
transformers: [(ls: ts.LanguageService) => {
28+
console.log(ls);
29+
return {};
30+
}],
31+
};
32+
}

__tests__/get-options-overrides.spec.ts

+2-27
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import * as ts from "typescript";
44
import { normalizePath as normalize } from "@rollup/pluginutils";
55
import { remove } from "fs-extra";
66

7+
import { makeOptions } from "./fixtures/options";
78
import { makeStubbedContext } from "./fixtures/context";
8-
import { setTypescriptModule } from "../src/tsproxy";
9-
import { IOptions } from "../src/ioptions";
109
import { getOptionsOverrides, createFilter } from "../src/get-options-overrides";
1110

12-
setTypescriptModule(ts);
13-
1411
const local = (x: string) => normalize(path.resolve(__dirname, x));
1512
const cacheDir = local("__temp/get-options-overrides");
1613

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

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

22-
const defaultConfig: IOptions = {
23-
include: ["*.ts+(|x)", "**/*.ts+(|x)"],
24-
exclude: ["*.d.ts", "**/*.d.ts"],
25-
check: false,
26-
verbosity: 5,
27-
clean: false,
28-
cacheRoot: cacheDir,
29-
cwd: local(""),
30-
abortOnError: false,
31-
rollupCommonJSResolveHack: false,
32-
typescript: ts,
33-
objectHashIgnoreUnknownHack: false,
34-
tsconfigOverride: null,
35-
useTsconfigDeclarationDir: false,
36-
tsconfigDefaults: null,
37-
sourceMapCallback: (id: string, map: string): void => {
38-
console.log(id + map);
39-
},
40-
transformers: [(ls: ts.LanguageService) => {
41-
console.log(ls);
42-
return {};
43-
}],
44-
};
19+
const defaultConfig = makeOptions(cacheDir, local(""));
4520

4621
const forcedOptions: ts.CompilerOptions = {
4722
allowNonTsExtensions: true,

__tests__/integration/errors.spec.ts

-7
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ async function genBundle(relInput: string, extraOpts?: RPT2Options, onwarn?: Moc
3030
});
3131
}
3232

33-
test("integration - tsconfig errors", async () => {
34-
// TODO: move to parse-tsconfig unit tests?
35-
expect(genBundle("semantic.ts", {
36-
tsconfig: "non-existent-tsconfig",
37-
})).rejects.toThrow("rpt2: failed to open 'non-existent-tsconfig'");
38-
});
39-
4033
test("integration - semantic error", async () => {
4134
expect(genBundle("semantic.ts")).rejects.toThrow("Type 'string' is not assignable to type 'number'.");
4235
});

__tests__/parse-tsconfig.spec.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { test, expect } from "@jest/globals";
2+
import * as path from "path";
3+
4+
import { makeOptions } from "./fixtures/options";
5+
import { makeStubbedContext } from "./fixtures/context";
6+
import { parseTsConfig } from "../src/parse-tsconfig";
7+
8+
const local = (x: string) => path.resolve(__dirname, x);
9+
10+
const defaultOpts = makeOptions("", "");
11+
const stubbedContext = makeStubbedContext({});
12+
13+
test("parseTsConfig", () => {
14+
expect(() => parseTsConfig(stubbedContext, defaultOpts)).not.toThrow();
15+
});
16+
17+
test("parseTsConfig - tsconfig errors", () => {
18+
const data = { error: "" };
19+
20+
// should not throw when the tsconfig is buggy, but should still print an error (below)
21+
expect(() => parseTsConfig(makeStubbedContext(data), {
22+
...defaultOpts,
23+
tsconfigOverride: {
24+
include: "should-be-an-array",
25+
},
26+
})).not.toThrow();
27+
expect(data.error).toMatch("Compiler option 'include' requires a value of type Array");
28+
});
29+
30+
test("parseTsConfig - failed to open", () => {
31+
expect(() => parseTsConfig(stubbedContext, {
32+
...defaultOpts,
33+
tsconfig: "non-existent-tsconfig",
34+
})).toThrow("rpt2: failed to open 'non-existent-tsconfig'");
35+
});
36+
37+
test("parseTsConfig - failed to parse", () => {
38+
const notTsConfigPath = local("fixtures/options.ts"); // a TS file should fail to parse
39+
40+
expect(() => parseTsConfig(stubbedContext, {
41+
...defaultOpts,
42+
tsconfig: notTsConfigPath,
43+
})).toThrow(`rpt2: failed to parse '${notTsConfigPath}'`);
44+
});

src/parse-tsconfig.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ export function parseTsConfig(context: IContext, pluginOptions: IOptions)
2323
let pretty = true;
2424
if (fileName)
2525
{
26-
const text = tsModule.sys.readFile(fileName);
27-
if (text === undefined)
28-
throw new Error(`rpt2: failed to read '${fileName}'`);
29-
26+
const text = tsModule.sys.readFile(fileName)!; // readFile only returns undefined when the file doesn't exist, which we already checked above
3027
const result = tsModule.parseConfigFileTextToJson(fileName, text);
3128
pretty = result.config?.pretty ?? pretty;
3229

0 commit comments

Comments
 (0)