From f9d4fc4162dc272c74642e4cca0a09880b9ef66a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 21 Feb 2022 15:26:13 +0100 Subject: [PATCH] chore: run type tests using old version of TS (#12449) --- scripts/verifyOldTs.js | 149 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/scripts/verifyOldTs.js b/scripts/verifyOldTs.js index f8003d404e85..113febee9909 100644 --- a/scripts/verifyOldTs.js +++ b/scripts/verifyOldTs.js @@ -70,4 +70,153 @@ function smoketest() { } } +function typeTests() { + const cwd = path.resolve(__dirname, '../'); + 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', + ); + } + + 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', 'yarn.lock'], {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 '));