Skip to content

Commit 12f1c31

Browse files
authored
chore(tests): use separate envs for each tests (#371)
1 parent fe6a307 commit 12f1c31

7 files changed

Lines changed: 166 additions & 209 deletions

File tree

tests/Disable.test.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {runCli} from './_runCli';
1212
const engine = new Engine();
1313

1414
beforeEach(async () => {
15+
// `process.env` is reset after each tests in setupTests.js.
1516
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
1617
});
1718

@@ -26,15 +27,10 @@ describe(`DisableCommand`, () => {
2627
for (const variant of getBinaryNames(binName))
2728
await makeBin(cwd, variant as Filename, {ignorePlatform: true});
2829

29-
const PATH = process.env.PATH;
30-
try {
31-
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${PATH}`;
32-
await expect(runCli(cwd, [`disable`])).resolves.toMatchObject({
33-
exitCode: 0,
34-
});
35-
} finally {
36-
process.env.PATH = PATH;
37-
}
30+
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
31+
await expect(runCli(cwd, [`disable`])).resolves.toMatchObject({
32+
exitCode: 0,
33+
});
3834

3935
const sortedEntries = xfs.readdirPromise(cwd).then(entries => {
4036
return entries.sort();
@@ -84,15 +80,10 @@ describe(`DisableCommand`, () => {
8480
const dontRemoveBin = await makeBin(cwd, `dont-remove` as Filename);
8581
binNames.add(ppath.basename(dontRemoveBin));
8682

87-
const PATH = process.env.PATH;
88-
try {
89-
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${PATH}`;
90-
await expect(runCli(cwd, [`disable`, `yarn`])).resolves.toMatchObject({
91-
exitCode: 0,
92-
});
93-
} finally {
94-
process.env.PATH = PATH;
95-
}
83+
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
84+
await expect(runCli(cwd, [`disable`, `yarn`])).resolves.toMatchObject({
85+
exitCode: 0,
86+
});
9687

9788
for (const variant of getBinaryNames(`yarn`))
9889
binNames.delete(variant);

tests/Enable.test.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {runCli} from './
1212
const engine = new Engine();
1313

1414
beforeEach(async () => {
15+
// `process.env` is reset after each tests in setupTests.js.
1516
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
1617
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
1718
});
@@ -21,17 +22,12 @@ describe(`EnableCommand`, () => {
2122
await xfs.mktempPromise(async cwd => {
2223
const corepackBin = await makeBin(cwd, `corepack` as Filename);
2324

24-
const PATH = process.env.PATH;
25-
try {
26-
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${PATH}`;
27-
await expect(runCli(cwd, [`enable`])).resolves.toMatchObject({
28-
stdout: ``,
29-
stderr: ``,
30-
exitCode: 0,
31-
});
32-
} finally {
33-
process.env.PATH = PATH;
34-
}
25+
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
26+
await expect(runCli(cwd, [`enable`])).resolves.toMatchObject({
27+
stdout: ``,
28+
stderr: ``,
29+
exitCode: 0,
30+
});
3531

3632
const sortedEntries = xfs.readdirPromise(cwd).then(entries => {
3733
return entries.sort();
@@ -73,17 +69,12 @@ describe(`EnableCommand`, () => {
7369
await xfs.mktempPromise(async cwd => {
7470
const corepackBin = await makeBin(cwd, `corepack` as Filename);
7571

76-
const PATH = process.env.PATH;
77-
try {
78-
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${PATH}`;
79-
await expect(runCli(cwd, [`enable`, `yarn`])).resolves.toMatchObject({
80-
stdout: ``,
81-
stderr: ``,
82-
exitCode: 0,
83-
});
84-
} finally {
85-
process.env.PATH = PATH;
86-
}
72+
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
73+
await expect(runCli(cwd, [`enable`, `yarn`])).resolves.toMatchObject({
74+
stdout: ``,
75+
stderr: ``,
76+
exitCode: 0,
77+
});
8778

8879
const sortedEntries = xfs.readdirPromise(cwd).then(entries => {
8980
return entries.sort();

tests/Up.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import process from 'node:process';
55
import {runCli} from './_runCli';
66

77
beforeEach(async () => {
8+
// `process.env` is reset after each tests in setupTests.js.
89
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
910
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
1011
});

tests/Use.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import process from 'node:process';
55
import {runCli} from './_runCli';
66

77
beforeEach(async () => {
8+
// `process.env` is reset after each tests in setupTests.js.
89
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
910
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
1011
});

0 commit comments

Comments
 (0)