/
compiler.test.ts
239 lines (213 loc) · 6.76 KB
/
compiler.test.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import {
ensureDirSync,
mkdtempSync,
removeSync,
writeFileSync,
readFileSync,
readJsonSync,
} from 'fs-extra';
import { tmpdir } from 'os';
import { join } from 'path';
import { Compiler } from '../lib/compiler';
import { ProjectInfo } from '../lib/project-info';
describe(Compiler, () => {
describe('generated tsconfig', () => {
test('default is tsconfig.json', () => {
const sourceDir = mkdtempSync(
join(tmpdir(), 'jsii-compiler-watch-mode-'),
);
const compiler = new Compiler({
projectInfo: _makeProjectInfo(sourceDir, 'index.d.ts'),
});
compiler.emit();
expect(readJsonSync(join(sourceDir, 'tsconfig.json'), 'utf-8')).toEqual(
expectedTypeScriptConfig(),
);
});
test('file name can be customized', () => {
const sourceDir = mkdtempSync(
join(tmpdir(), 'jsii-compiler-watch-mode-'),
);
const compiler = new Compiler({
projectInfo: _makeProjectInfo(sourceDir, 'index.d.ts'),
generateTypeScriptConfig: 'tsconfig.jsii.json',
});
compiler.emit();
expect(
readJsonSync(join(sourceDir, 'tsconfig.jsii.json'), 'utf-8'),
).toEqual(expectedTypeScriptConfig());
});
});
test('"watch" mode', async () => {
// This can be a little slow, allowing 15 seconds maximum here (default is 5 seconds)
jest.setTimeout(15_000);
const sourceDir = mkdtempSync(join(tmpdir(), 'jsii-compiler-watch-mode-'));
try {
writeFileSync(join(sourceDir, 'index.ts'), 'export class MarkerA {}');
// Intentionally using lower case name - it should be case-insensitive
writeFileSync(join(sourceDir, 'readme.md'), '# Test Package');
const compiler = new Compiler({
projectInfo: _makeProjectInfo(sourceDir, 'index.d.ts'),
failOnWarnings: true,
projectReferences: false,
});
let firstCompilation = true;
let onWatchClosed: () => void;
let onWatchFailed: (err: unknown) => void;
const watchClosed = new Promise<void>((ok, ko) => {
onWatchClosed = ok;
onWatchFailed = ko;
});
const watch = await compiler.watch({
nonBlocking: true,
// Ignore diagnostics reporting (not to pollute test console output)
reportDiagnostics: () => null,
// Ignore watch status reporting (not to pollute test console output)
reportWatchStatus: () => null,
// Verify everything goes according to plan
compilationComplete: (emitResult) => {
try {
expect(emitResult.emitSkipped).toBeFalsy();
const output = readFileSync(join(sourceDir, '.jsii'), {
encoding: 'utf-8',
});
if (firstCompilation) {
firstCompilation = false;
expect(output).toContain('"MarkerA"');
writeFileSync(
join(sourceDir, 'index.ts'),
'export class MarkerB {}',
);
return;
}
expect(output).toContain('"MarkerB"');
watch.close();
// Tell the test suite we're done here!
onWatchClosed();
} catch (e) {
watch.close();
onWatchFailed(e);
}
},
});
await watchClosed;
} finally {
removeSync(sourceDir);
}
});
test('rootDir is added to assembly', () => {
const outDir = 'jsii-outdir';
const rootDir = 'jsii-rootdir';
const sourceDir = mkdtempSync(join(tmpdir(), 'jsii-tmpdir'));
ensureDirSync(join(sourceDir, rootDir));
try {
writeFileSync(
join(sourceDir, rootDir, 'index.ts'),
'export class MarkerA {}',
);
// Intentionally using lower case name - it should be case-insensitive
writeFileSync(join(sourceDir, rootDir, 'readme.md'), '# Test Package');
const compiler = new Compiler({
projectInfo: {
..._makeProjectInfo(sourceDir, join(outDir, 'index.d.ts')),
tsc: {
outDir,
rootDir,
},
},
failOnWarnings: true,
projectReferences: false,
});
compiler.emit();
const assembly = readJsonSync(join(sourceDir, '.jsii'), 'utf-8');
expect(assembly.metadata).toEqual(
expect.objectContaining({
tscRootDir: rootDir,
}),
);
} finally {
removeSync(sourceDir);
}
});
test('emits declaration map when feature is enabled', () => {
const sourceDir = mkdtempSync(join(tmpdir(), 'jsii-tmpdir'));
try {
writeFileSync(join(sourceDir, 'index.ts'), 'export class MarkerA {}');
const compiler = new Compiler({
projectInfo: {
..._makeProjectInfo(sourceDir, 'index.d.ts'),
tsc: {
declarationMap: true,
},
},
generateTypeScriptConfig: 'tsconfig.jsii.json',
});
compiler.emit();
expect(() => {
readFileSync(join(sourceDir, 'index.d.ts.map'), 'utf-8');
}).not.toThrow();
} finally {
removeSync(sourceDir);
}
});
});
function _makeProjectInfo(sourceDir: string, types: string): ProjectInfo {
return {
projectRoot: sourceDir,
packageJson: undefined,
types,
main: types.replace(/(?:\.d)?\.ts(x?)/, '.js$1'),
name: 'jsii', // That's what package.json would tell if we look up...
version: '0.0.1',
jsiiVersionFormat: 'short',
license: 'Apache-2.0',
author: { name: 'John Doe', roles: ['author'] },
repository: { type: 'git', url: 'https://github.com/aws/jsii.git' },
dependencies: {},
peerDependencies: {},
dependencyClosure: [],
bundleDependencies: {},
targets: {},
excludeTypescript: [],
tsc: {
// NOTE: these are the default values jsii uses when none are provided in package.json.
inlineSourceMap: true,
inlineSources: true,
},
};
}
function expectedTypeScriptConfig() {
return {
_generated_by_jsii_:
'Generated by jsii - safe to delete, and ideally should be in .gitignore',
compilerOptions: {
alwaysStrict: true,
charset: 'utf8',
composite: false,
declaration: true,
experimentalDecorators: true,
incremental: true,
inlineSourceMap: true,
inlineSources: true,
lib: ['es2019'],
module: 'CommonJS',
newLine: 'lf',
noEmitOnError: true,
noFallthroughCasesInSwitch: true,
noImplicitAny: true,
noImplicitReturns: true,
noImplicitThis: true,
noUnusedLocals: true,
noUnusedParameters: true,
resolveJsonModule: true,
strict: true,
strictNullChecks: true,
strictPropertyInitialization: true,
stripInternal: false,
target: 'ES2019',
tsBuildInfoFile: 'tsconfig.tsbuildinfo',
},
exclude: ['node_modules'],
include: [join('**', '*.ts')],
};
}