Skip to content

Commit

Permalink
chore: reduce test flakiness (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 authored Sep 1, 2022
1 parent 92b52f6 commit c0df287
Show file tree
Hide file tree
Showing 75 changed files with 113 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ jobs:
- run: corepack yarn build # We need the stubs to run the tests

- run: corepack yarn test
env:
NOCK_ENV: replay
7 changes: 7 additions & 0 deletions tests/Enable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const engine = new Engine();

beforeEach(async () => {
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
});

describe(`EnableCommand`, () => {
Expand All @@ -22,6 +23,8 @@ describe(`EnableCommand`, () => {
try {
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${PATH}`;
await expect(runCli(cwd, [`enable`])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
} finally {
Expand All @@ -46,6 +49,8 @@ describe(`EnableCommand`, () => {
const corepackBin = await makeBin(cwd, `corepack` as Filename);

await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});

Expand All @@ -70,6 +75,8 @@ describe(`EnableCommand`, () => {
try {
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${PATH}`;
await expect(runCli(cwd, [`enable`, `yarn`])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
} finally {
Expand Down
4 changes: 3 additions & 1 deletion tests/_runCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export async function runCli(cwd: PortablePath, argv: Array<string>) {
const err: Array<Buffer> = [];

return new Promise((resolve, reject) => {
const child = spawn(process.execPath, [require.resolve(`corepack/dist/corepack.js`), ...argv], {
if (process.env.RUN_CLI_ID)
(process.env.RUN_CLI_ID as any)++;
const child = spawn(process.execPath, [`--no-warnings`, `-r`, require.resolve(`./recordRequests.js`), require.resolve(`corepack/dist/corepack.js`), ...argv], {
cwd: npath.fromPortablePath(cwd),
env: process.env,
stdio: `pipe`,
Expand Down
34 changes: 33 additions & 1 deletion tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ it(`should refuse to download a package manager if the hash doesn't match`, asyn

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 1,
stderr: ``,
stdout: /Mismatch hashes/,
});
});
Expand Down Expand Up @@ -52,6 +53,7 @@ for (const [name, version] of testedPackageManagers) {

await expect(runCli(cwd, [name, `--version`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
stdout: `${version.split(`+`, 1)[0]}\n`,
});
});
Expand All @@ -77,11 +79,13 @@ it(`should ignore the packageManager field when found within a node_modules vend

await expect(runCli(ppath.join(cwd, `node_modules/foo` as PortablePath), [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
stdout: `1.22.4\n`,
});

await expect(runCli(ppath.join(cwd, `node_modules/@foo/bar` as PortablePath), [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
stdout: `1.22.4\n`,
});
});
Expand All @@ -101,6 +105,7 @@ it(`should use the closest matching packageManager field`, async () => {

await expect(runCli(ppath.join(cwd, `foo` as PortablePath), [`npm`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
stdout: `6.14.2\n`,
});
});
Expand All @@ -127,6 +132,7 @@ it(`shouldn't allow using regular Yarn commands on npm-configured projects`, asy

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 1,
stderr: ``,
});
});
});
Expand All @@ -137,8 +143,9 @@ it(`should allow using transparent commands on npm-configured projects`, async (
packageManager: `[email protected]`,
});

await expect(runCli(cwd, [`yarn`, `dlx`, `[email protected]`, __filename])).resolves.toMatchObject({
await expect(runCli(cwd, [`yarn`, `dlx`, `--help`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});
});
});
Expand All @@ -147,6 +154,7 @@ it(`should transparently use the preconfigured version when there is no local pr
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});
});
});
Expand All @@ -163,16 +171,19 @@ it(`should use the pinned version when local projects don't list any spec`, asyn

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: `${config.definitions.yarn.default.split(`+`, 1)[0]}\n`,
stderr: ``,
exitCode: 0,
});

await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
stdout: `${config.definitions.pnpm.default.split(`+`, 1)[0]}\n`,
stderr: ``,
exitCode: 0,
});

await expect(runCli(cwd, [`npm`, `--version`])).resolves.toMatchObject({
stdout: `${config.definitions.npm.default.split(`+`, 1)[0]}\n`,
stderr: ``,
exitCode: 0,
});
});
Expand All @@ -182,6 +193,7 @@ it(`should allow updating the pinned version using the "prepare" command`, async
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`prepare`, `--activate`, `[email protected]`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});

await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
Expand All @@ -190,6 +202,7 @@ it(`should allow updating the pinned version using the "prepare" command`, async

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: `1.0.0\n`,
stderr: ``,
exitCode: 0,
});
});
Expand All @@ -203,6 +216,7 @@ it(`should allow to call "prepare" without arguments within a configured project

await expect(runCli(cwd, [`prepare`, `--activate`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});

// Disable the network to make sure we don't succeed by accident
Expand All @@ -211,6 +225,7 @@ it(`should allow to call "prepare" without arguments within a configured project
try {
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: `1.0.0\n`,
stderr: ``,
exitCode: 0,
});
} finally {
Expand Down Expand Up @@ -255,23 +270,27 @@ it(`should allow to call "prepare" with --all to prepare all package managers`,

await expect(runCli(cwd, [`prepare`, `--all`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});

process.env.COREPACK_ENABLE_NETWORK = `0`;

try {
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: `${config.definitions.yarn.default.split(`+`, 1)[0]}\n`,
stderr: ``,
exitCode: 0,
});

await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
stdout: `${config.definitions.pnpm.default.split(`+`, 1)[0]}\n`,
stderr: ``,
exitCode: 0,
});

await expect(runCli(cwd, [`npm`, `--version`])).resolves.toMatchObject({
stdout: `${config.definitions.npm.default.split(`+`, 1)[0]}\n`,
stderr: ``,
exitCode: 0,
});
} finally {
Expand All @@ -291,6 +310,7 @@ it(`should support disabling the network accesses from the environment`, async (

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: expect.stringContaining(`Network access disabled by the environment`),
stderr: ``,
exitCode: 1,
});
});
Expand All @@ -303,6 +323,7 @@ it(`should support hydrating package managers from cached archives`, async () =>
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`prepare`, `[email protected]`, `-o`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});

// Use a new cache
Expand All @@ -314,6 +335,7 @@ it(`should support hydrating package managers from cached archives`, async () =>
try {
await expect(runCli(cwd, [`hydrate`, `corepack.tgz`])).resolves.toMatchObject({
stdout: `Hydrating [email protected]...\nAll done!\n`,
stderr: ``,
exitCode: 0,
});

Expand All @@ -323,6 +345,7 @@ it(`should support hydrating package managers from cached archives`, async () =>

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: `2.2.2\n`,
stderr: ``,
exitCode: 0,
});
} finally {
Expand All @@ -335,6 +358,7 @@ it(`should support hydrating multiple package managers from cached archives`, as
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`prepare`, `[email protected]`, `[email protected]`, `-o`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});

// Use a new cache
Expand All @@ -346,6 +370,7 @@ it(`should support hydrating multiple package managers from cached archives`, as
try {
await expect(runCli(cwd, [`hydrate`, `corepack.tgz`])).resolves.toMatchObject({
stdout: `Hydrating [email protected]...\nHydrating [email protected]...\nAll done!\n`,
stderr: ``,
exitCode: 0,
});

Expand All @@ -355,6 +380,7 @@ it(`should support hydrating multiple package managers from cached archives`, as

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: `2.2.2\n`,
stderr: ``,
exitCode: 0,
});

Expand All @@ -364,6 +390,7 @@ it(`should support hydrating multiple package managers from cached archives`, as

await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
stdout: `5.8.0\n`,
stderr: ``,
exitCode: 0,
});
} finally {
Expand All @@ -380,11 +407,13 @@ it(`should support running package managers with bin array`, async () => {

await expect(runCli(cwd, [`yarnpkg`, `--version`])).resolves.toMatchObject({
stdout: `2.2.2\n`,
stderr: ``,
exitCode: 0,
});

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: `2.2.2\n`,
stderr: ``,
exitCode: 0,
});
});
Expand All @@ -403,14 +432,17 @@ it(`should handle parallel installs`, async () => {
])).resolves.toMatchObject([
{
stdout: `2.2.2\n`,
stderr: ``,
exitCode: 0,
},
{
stdout: `2.2.2\n`,
stderr: ``,
exitCode: 0,
},
{
stdout: `2.2.2\n`,
stderr: ``,
exitCode: 0,
},
]);
Expand Down
Loading

0 comments on commit c0df287

Please sign in to comment.