Skip to content

Commit

Permalink
chore: run type tests using old version of TS (#12449)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 21, 2022
1 parent 293f38a commit f9d4fc4
Showing 1 changed file with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions scripts/verifyOldTs.js
Expand Up @@ -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 '));

0 comments on commit f9d4fc4

Please sign in to comment.