Skip to content

Commit babd66c

Browse files
committed
test: simplify the typings compat tests
1 parent cf84548 commit babd66c

File tree

1 file changed

+89
-93
lines changed

1 file changed

+89
-93
lines changed

test/unit/typings-compatibility-test.ts

Lines changed: 89 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async function run(
6161
cwd: string,
6262
errorAsString: boolean,
6363
): Promise<string | Error | undefined> {
64-
return new Promise(resolve => {
64+
return new Promise((resolve, reject) => {
6565
exec(cmd, {cwd}, (error, stdout, stderr) => {
6666
if (error) {
6767
resolve(errorAsString ? `${stdout}\n${stderr}` : error)
@@ -84,108 +84,104 @@ function getItLabelDetails(tsVer: TestDef): string {
8484
)}`
8585
}
8686

87-
describe("compatibility of typings for typescript versions", function () {
88-
let execCmd: "pnpm" | "npm" | "yarn" = "npm"
87+
describe("compatibility of typings for typescript versions", async function () {
88+
let execCmd: string
8989

9090
before(async function () {
9191
this.timeout(10000)
9292
if (/^true$/.test(process.env.EXCLUDE_TYPINGS_COMPAT_TESTS as string)) {
9393
this.skip()
94-
} else {
95-
// detect package manager (pnpm, npm, yarn) for installing typescript versions
96-
const packageManagers = ["pnpm", "yarn", "npm"] as typeof execCmd[]
97-
98-
const versionResults = await Promise.all(
99-
packageManagers.map(pm =>
100-
run(`${pm} --version`, tscTestBasePath, false),
101-
),
102-
)
94+
}
95+
execCmd = await getPackageManager()
96+
})
10397

104-
const packageManagerIndex = versionResults.findIndex(
105-
versionResult => typeof versionResult === "string",
98+
for (const tsVer of tsVersions) {
99+
// must increase timeout for allowing `npm install`'ing the version of
100+
// the typescript package to complete
101+
this.timeout(30000)
102+
103+
const tscTargetPath = path.resolve(tscTestBasePath, `ts-${tsVer.version}`)
104+
105+
it(`it should compile successfully with typescript version ${
106+
tsVer.version
107+
// eslint-disable-next-line no-loop-func
108+
}, tsc ${getItLabelDetails(tsVer)}`, async function () {
109+
await prepareTestPackage(tscTargetPath, tsVer, execCmd)
110+
111+
const cmd = ["npm", "pnpm"].includes(execCmd) ? `${execCmd} run` : execCmd
112+
const errMsg = (await run(`${cmd} test`, tscTargetPath, true)) as
113+
| string
114+
| undefined
115+
assert.isUndefined(errMsg, errMsg)
116+
})
117+
118+
afterEach(async () => {
119+
await remove(tscTargetPath)
120+
})
121+
}
122+
})
123+
124+
async function prepareTestPackage(
125+
tscTargetPath: string,
126+
tsVer: TestDef,
127+
execCmd: string,
128+
) {
129+
await emptyDir(tscTargetPath)
130+
131+
await Promise.all([
132+
(async () => {
133+
const tsConfig = await readJson(
134+
path.resolve(templateSrcPath, "tsconfig.json"),
106135
)
107136

108-
if (packageManagerIndex === -1) {
109-
throw new Error(
110-
"Cannot run typings compatibility test, because pnpm, npm, and yarn are not available.",
137+
tsConfig.compilerOptions.target = tsVer.minTarget
138+
if (tsVer.requiredLibs) {
139+
tsConfig.compilerOptions.lib = addLibs(
140+
tsVer.requiredLibs,
141+
tsConfig.compilerOptions.lib as string[],
111142
)
112143
}
144+
return writeJson(path.resolve(tscTargetPath, "tsconfig.json"), tsConfig)
145+
})(),
146+
(async () => {
147+
const pkgJson = await readJson(
148+
path.resolve(templateSrcPath, "package.json"),
149+
)
113150

114-
// eslint-disable-next-line require-atomic-updates
115-
execCmd = packageManagers[packageManagerIndex]
116-
}
117-
})
151+
pkgJson.name = `test-typings-ts-${tsVer.version}`
152+
pkgJson.devDependencies.typescript = `${tsVer.version}`
153+
return writeJson(path.resolve(tscTargetPath, "package.json"), pkgJson)
154+
})(),
155+
(async () => {
156+
const content = await srcStr
157+
return writeFile(
158+
path.resolve(tscTargetPath, "typings-test.ts"),
159+
content,
160+
"utf8",
161+
)
162+
})(),
163+
])
118164

119-
for (const tsVer of tsVersions) {
120-
// eslint-disable-next-line no-loop-func
121-
describe(`when used in a project with typescript version ${tsVer.version}`, function () {
122-
// must increase timeout for allowing `npm install`'ing the version of
123-
// the typescript package to complete
124-
this.timeout(30000)
125-
126-
const tscTargetPath = path.resolve(tscTestBasePath, `ts-${tsVer.version}`)
127-
128-
beforeEach(async () => {
129-
await emptyDir(tscTargetPath)
130-
131-
await Promise.all([
132-
(async () => {
133-
const tsConfig = await readJson(
134-
path.resolve(templateSrcPath, "tsconfig.json"),
135-
)
136-
137-
tsConfig.compilerOptions.target = tsVer.minTarget
138-
if (tsVer.requiredLibs) {
139-
tsConfig.compilerOptions.lib = addLibs(
140-
tsVer.requiredLibs,
141-
tsConfig.compilerOptions.lib as string[],
142-
)
143-
}
144-
return writeJson(
145-
path.resolve(tscTargetPath, "tsconfig.json"),
146-
tsConfig,
147-
)
148-
})(),
149-
(async () => {
150-
const pkgJson = await readJson(
151-
path.resolve(templateSrcPath, "package.json"),
152-
)
153-
154-
pkgJson.name = `test-typings-ts-${tsVer.version}`
155-
pkgJson.devDependencies.typescript = `${tsVer.version}`
156-
return writeJson(
157-
path.resolve(tscTargetPath, "package.json"),
158-
pkgJson,
159-
)
160-
})(),
161-
(async () => {
162-
const content = await srcStr
163-
return writeFile(
164-
path.resolve(tscTargetPath, "typings-test.ts"),
165-
content,
166-
"utf8",
167-
)
168-
})(),
169-
])
170-
171-
await run(`${execCmd} install`, tscTargetPath, false)
172-
})
173-
174-
afterEach(async () => {
175-
await remove(tscTargetPath)
176-
})
177-
178-
it(`it should compile successfully with tsc ${getItLabelDetails(
179-
tsVer,
180-
)}`, async function () {
181-
const cmd = ["npm", "pnpm"].includes(execCmd)
182-
? `${execCmd} run`
183-
: execCmd
184-
const errMsg = (await run(`${cmd} test`, tscTargetPath, true)) as
185-
| string
186-
| undefined
187-
assert.isUndefined(errMsg, errMsg)
188-
})
189-
})
165+
await run(`${execCmd} install`, tscTargetPath, false)
166+
}
167+
168+
/// detect package manager (pnpm, npm, yarn) for installing typescript versions
169+
async function getPackageManager() {
170+
const packageManagers = ["pnpm", "yarn", "npm"]
171+
172+
const versionResults = await Promise.all(
173+
packageManagers.map(pm => run(`${pm} --version`, tscTestBasePath, false)),
174+
)
175+
176+
const packageManagerIndex = versionResults.findIndex(
177+
versionResult => typeof versionResult === "string",
178+
)
179+
180+
if (packageManagerIndex === -1) {
181+
throw new Error(
182+
"Cannot run typings compatibility test, because pnpm, npm, and yarn are not available.",
183+
)
190184
}
191-
})
185+
186+
return packageManagers[packageManagerIndex]
187+
}

0 commit comments

Comments
 (0)