Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: run type tests using old version of TS #12449

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions scripts/verifyOldTs.js
Original file line number Diff line number Diff line change
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 '));