Skip to content

Commit d286015

Browse files
agilgur5ezolenko
andauthored
test: add parse-tsconfig spec (#397)
* 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` * fix windows test by normalizing Co-authored-by: Eugene Zolenko <[email protected]>
1 parent c1f3a35 commit d286015

File tree

5 files changed

+80
-38
lines changed

5 files changed

+80
-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-
await 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
await expect(genBundle("semantic.ts")).rejects.toThrow("Type 'string' is not assignable to type 'number'.");
4235
});

__tests__/parse-tsconfig.spec.ts

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

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)