-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathget-options-overrides.spec.ts
164 lines (135 loc) · 4.95 KB
/
get-options-overrides.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { afterAll, test, expect } from "@jest/globals";
import * as path from "path";
import * as ts from "typescript";
import { normalizePath as normalize } from "@rollup/pluginutils";
import { remove } from "fs-extra";
import { makeOptions } from "./fixtures/options";
import { makeContext } from "./fixtures/context";
import { getOptionsOverrides, createFilter } from "../src/get-options-overrides";
const local = (x: string) => normalize(path.resolve(__dirname, x));
const cacheDir = local("__temp/get-options-overrides");
// filter expects an absolute path and resolves include/exclude to process.cwd() by default: https://github.com/ezolenko/rollup-plugin-typescript2/pull/321#discussion_r873077874
const filtPath = (relPath: string) => normalize(`${process.cwd()}/${relPath}`);
afterAll(() => remove(cacheDir));
const defaultConfig = makeOptions(cacheDir, local(""));
const forcedOptions: ts.CompilerOptions = {
allowNonTsExtensions: true,
importHelpers: true,
inlineSourceMap: false,
noEmit: false,
noEmitOnError: false,
noEmitHelpers: false,
noResolve: false,
outDir: `${cacheDir}/placeholder`,
};
const defaultPreParsedTsConfig: ts.ParsedCommandLine = {
options: {},
fileNames: [],
errors: [],
};
test("getOptionsOverrides", () => {
const config = { ...defaultConfig };
expect(getOptionsOverrides(config)).toStrictEqual(forcedOptions);
});
test("getOptionsOverrides - preParsedTsConfig", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = { ...defaultPreParsedTsConfig };
expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({
...forcedOptions,
declarationDir: undefined,
module: ts.ModuleKind.ES2015,
sourceRoot: undefined,
moduleResolution: ts.ModuleResolutionKind.Node10,
});
});
test("getOptionsOverrides - preParsedTsConfig with options.module", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = {
...defaultPreParsedTsConfig,
options: {
module: ts.ModuleKind.AMD,
},
};
expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({
...forcedOptions,
declarationDir: undefined,
sourceRoot: undefined,
moduleResolution: ts.ModuleResolutionKind.Node10,
});
});
test("getOptionsOverrides - with declaration", () => {
const config = { ...defaultConfig, useTsconfigDeclarationDir: true };
const preParsedTsConfig = { ...defaultPreParsedTsConfig };
expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({
...forcedOptions,
module: ts.ModuleKind.ES2015,
sourceRoot: undefined,
moduleResolution: ts.ModuleResolutionKind.Node10,
});
});
test("getOptionsOverrides - with sourceMap", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = {
...defaultPreParsedTsConfig,
options: {
sourceMap: true,
},
};
expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({
...forcedOptions,
declarationDir: undefined,
module: ts.ModuleKind.ES2015,
moduleResolution: ts.ModuleResolutionKind.Node10,
});
});
test("createFilter", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = { ...defaultPreParsedTsConfig };
const filter = createFilter(makeContext(), config, preParsedTsConfig);
expect(filter(filtPath("src/test.ts"))).toBe(true);
expect(filter(filtPath("src/test.js"))).toBe(false);
expect(filter(filtPath("src/test.d.ts"))).toBe(false);
});
test("createFilter - context.debug", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = { ...defaultPreParsedTsConfig };
const context = makeContext();
createFilter(context, config, preParsedTsConfig);
expect(context.debug).toHaveBeenCalledTimes(2);
});
test("createFilter - rootDirs", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = {
...defaultPreParsedTsConfig,
options: {
rootDirs: ["src", "lib"]
},
};
const filter = createFilter(makeContext(), config, preParsedTsConfig);
expect(filter(filtPath("src/test.ts"))).toBe(true);
expect(filter(filtPath("src/test.js"))).toBe(false);
expect(filter(filtPath("src/test.d.ts"))).toBe(false);
expect(filter(filtPath("lib/test.ts"))).toBe(true);
expect(filter(filtPath("lib/test.js"))).toBe(false);
expect(filter(filtPath("lib/test.d.ts"))).toBe(false);
expect(filter(filtPath("not-src/test.ts"))).toBe(false);
});
test("createFilter - projectReferences", () => {
// test string include and also don't match with "**"
const config = { ...defaultConfig, include: "*.ts+(|x)" };
const preParsedTsConfig = {
...defaultPreParsedTsConfig,
projectReferences: [
{ path: "src" },
{ path: "lib" },
],
};
const filter = createFilter(makeContext(), config, preParsedTsConfig);
expect(filter(filtPath("src/test.ts"))).toBe(true);
expect(filter(filtPath("src/test.js"))).toBe(false);
expect(filter(filtPath("src/test.d.ts"))).toBe(false);
expect(filter(filtPath("lib/test.ts"))).toBe(true);
expect(filter(filtPath("lib/test.js"))).toBe(false);
expect(filter(filtPath("lib/test.d.ts"))).toBe(false);
expect(filter(filtPath("not-src/test.ts"))).toBe(false);
});