-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathverifyOldTs.mjs
231 lines (197 loc) · 6.03 KB
/
verifyOldTs.mjs
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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {createRequire} from 'module';
import * as path from 'path';
import {fileURLToPath} from 'url';
import chalk from 'chalk';
import execa from 'execa';
import fs from 'graceful-fs';
import stripJsonComments from 'strip-json-comments';
import tempy from 'tempy';
const require = createRequire(import.meta.url);
const baseTsConfig = JSON.parse(
stripJsonComments(
fs.readFileSync(require.resolve('../tsconfig.json'), 'utf8'),
),
);
/* eslint-disable sort-keys */
const tsConfig = {
extends: baseTsConfig.extends,
compilerOptions: {
esModuleInterop: false,
module: 'commonjs',
moduleResolution: 'node',
noEmit: true,
},
};
/* eslint-enable */
const tsVersion = '5.0';
function smoketest() {
const jestDirectory = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../packages/jest',
);
const cwd = tempy.directory();
try {
fs.writeFileSync(
path.join(cwd, '.yarnrc.yml'),
'nodeLinker: node-modules\n',
);
execa.sync('yarn', ['init', '--yes'], {cwd, stdio: 'inherit'});
execa.sync(
'yarn',
// TODO: do not set version of @tsconfig/node14 after we upgrade to a version of TS that supports `"moduleResolution": "node16"` (https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/)
['add', `typescript@~${tsVersion}`, '@tsconfig/node14@1'],
{cwd, stdio: 'inherit'},
);
fs.writeFileSync(
path.join(cwd, 'tsconfig.json'),
JSON.stringify(tsConfig, null, 2),
);
fs.writeFileSync(
path.join(cwd, 'index.ts'),
`import jest = require('${jestDirectory}');`,
);
execa.sync('yarn', ['tsc', '--project', '.'], {cwd, stdio: 'inherit'});
console.log(
chalk.inverse.green(
` Successfully compiled Jest with TypeScript ${tsVersion} `,
),
);
} finally {
fs.rmSync(cwd, {force: true, recursive: true});
}
}
function typeTests() {
const cwd = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../');
const rootPackageJson = require('../package.json');
const currentTsdTypescriptVersion =
rootPackageJson.devDependencies['@tsd/typescript'];
const currentTypescriptVersion =
rootPackageJson.devDependencies['typescript'];
const apiExtractorTypescriptVersion =
require('@microsoft/api-extractor/package.json').dependencies['typescript'];
try {
const {stdout: statusStdout} = execa.sync(
'git',
['status', '--porcelain'],
{cwd},
);
if (statusStdout.length > 0) {
throw new Error(
'Repo is not clean - cannot run type tests with old typescript version',
);
}
// TODO: do not set version of @tsconfig/node14 after we upgrade to a version of TS that supports `"moduleResolution": "node16"` (https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/)
execa.sync('yarn', ['add', '@tsconfig/node14@1'], {cwd});
execa.sync(
'yarn',
[
'set',
'resolution',
`@tsd/typescript@npm:${currentTsdTypescriptVersion}`,
tsVersion,
],
{cwd},
);
verifyInstalledTsdTypescript();
execa.sync(
'yarn',
[
'set',
'resolution',
`typescript@npm:${currentTypescriptVersion}`,
tsVersion,
],
{cwd},
);
execa.sync(
'yarn',
[
'set',
'resolution',
`typescript@npm:${apiExtractorTypescriptVersion}`,
tsVersion,
],
{cwd},
);
execa.sync('yarn', ['set', 'resolution', 'typescript@npm:*', tsVersion], {
cwd,
});
verifyInstalledTypescript();
execa.sync('yarn', ['test-types'], {cwd, stdio: 'inherit'});
} finally {
execa.sync('git', ['checkout', 'package.json', 'yarn.lock'], {cwd});
execa.sync('yarn', ['install'], {cwd});
}
function verifyInstalledTsdTypescript() {
const tsdEntries = listInstalledVersion('@tsd/typescript');
if (tsdEntries.length !== 1) {
throw new Error(
`More than one version of @tsd/typescript found: ${tsdEntries.join(
', ',
)}`,
);
}
const tsdVersion = tsdEntries[0].match(/@npm:(\d+\.\d+)\.\d+/);
if (!tsdVersion) {
throw new Error('Unable to verify installed version of @tsd/typescript');
}
if (tsdVersion[1] !== tsVersion) {
throw new Error(
`Installed TSD version is not ${tsVersion}, is ${tsdVersion[1]}`,
);
}
}
function verifyInstalledTypescript() {
const typescriptEntries = listInstalledVersion('typescript');
if (typescriptEntries.length !== 1) {
throw new Error(
`More than one version of typescript found: ${typescriptEntries.join(
', ',
)}`,
);
}
const tsdVersion = typescriptEntries[0].match(/@npm%3A(\d+\.\d+)\.\d+/);
if (!tsdVersion) {
throw new Error('Unable to verify installed version of typescript');
}
if (tsdVersion[1] !== tsVersion) {
throw new Error(
`Installed TSD version is not ${tsVersion}, is ${tsdVersion[1]}`,
);
}
}
function listInstalledVersion(module) {
const {stdout: tsdWhyOutput} = execa.sync(
'yarn',
['why', module, '--json'],
{cwd},
);
const locators = tsdWhyOutput
.split('\n')
.map(JSON.parse)
.map(entry => {
const entries = Object.entries(entry.children);
if (entries.length !== 1) {
throw new Error(
`More than one entry found in ${JSON.stringify(entry, null, 2)}`,
);
}
return entries[0][1].locator;
});
return Array.from(new Set(locators));
}
}
console.log(chalk.inverse(` Running smoketest using TypeScript@${tsVersion} `));
smoketest();
console.log(chalk.inverse.green(' Successfully ran smoketest '));
console.log(
chalk.inverse(` Running type tests using TypeScript@${tsVersion} `),
);
typeTests();
console.log(chalk.inverse.green(' Successfully ran type tests '));