Skip to content

Commit 18b6809

Browse files
committed
test(auto-install): make yarn tests resilient to Corepack by pre-creating local package.json with Yarn v1
1 parent 1696370 commit 18b6809

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/auto-install/test/yarn-bare.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@ const DIR = import.meta.dirname;
1212
const cwd = path.join(DIR, 'fixtures/yarn-bare');
1313
const file = path.join(cwd, 'output/bundle.js');
1414
const input = path.join(cwd, '../input.js');
15+
const pkgFile = path.join(cwd, 'package.json');
1516

1617
const PREV_CWD = process.cwd();
1718

1819
it('yarn, bare', async () => {
1920
process.chdir(cwd);
21+
// Ensure Yarn classic does not traverse to the repo root and read its
22+
// packageManager (pnpm). When no local package.json exists, Yarn v1 will
23+
// use the nearest parent package.json and error out. Creating a minimal
24+
// local package.json with an explicit Yarn version avoids the Corepack gate.
25+
if (!fs.existsSync(pkgFile)) {
26+
fs.writeFileSync(
27+
pkgFile,
28+
JSON.stringify(
29+
{ name: 'auto-install-yarn-bare-fixture', private: true, packageManager: '[email protected]' },
30+
null,
31+
2
32+
)
33+
);
34+
}
2035
const bundle = await rollup({
2136
input,
2237
// @ts-expect-error - rollup() ignores output here but tests kept it historically

packages/auto-install/test/yarn.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,25 @@ const DIR = import.meta.dirname;
1212
const cwd = path.join(DIR, 'fixtures/yarn');
1313
const file = path.join(cwd, 'output/bundle.js');
1414
const input = path.join(cwd, '../input.js');
15+
const pkgFile = path.join(cwd, 'package.json');
1516

1617
const PREV_CWD = process.cwd();
1718

1819
it('yarn', async () => {
1920
process.chdir(cwd);
21+
// Pre-create a local package.json with an explicit Yarn v1 requirement so
22+
// the Yarn shim (Corepack) doesn't traverse to the repo root and pick up
23+
// the root packageManager (pnpm), which would cause a usage error.
24+
if (!fs.existsSync(pkgFile)) {
25+
fs.writeFileSync(
26+
pkgFile,
27+
JSON.stringify(
28+
{ name: 'auto-install-yarn-fixture', private: true, packageManager: '[email protected]' },
29+
null,
30+
2
31+
)
32+
);
33+
}
2034
const bundle = await rollup({
2135
input,
2236
// @ts-expect-error - rollup() ignores output here but tests kept it historically

0 commit comments

Comments
 (0)