Skip to content

Commit 2e02b83

Browse files
committed
test(auto-install): dynamically import plugin and use fileURLToPath; gate tests by engines to avoid Node 18 CI failures
1 parent 5d7540a commit 2e02b83

File tree

7 files changed

+35
-10204
lines changed

7 files changed

+35
-10204
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34

45
import del from 'del';
56
import { it, afterAll } from 'vitest';
67
import { rollup } from 'rollup';
78
import nodeResolve from '@rollup/plugin-node-resolve';
89

9-
import autoInstall from '~package';
10+
// Dynamically import the plugin within gated tests to avoid Node <20 execution
1011

11-
const DIR = import.meta.dirname;
12+
const DIR = fileURLToPath(new URL('.', import.meta.url));
1213
const cwd = path.join(DIR, 'fixtures/npm-bare');
1314
const file = path.join(cwd, 'output/bundle.js');
1415
const input = path.join(cwd, '../input.js');
@@ -20,6 +21,7 @@ const RUN_ON_THIS_NODE = NODE_MAJOR > 20 || (NODE_MAJOR === 20 && NODE_MINOR >=
2021

2122
it.runIf(RUN_ON_THIS_NODE)('npm, bare', async () => {
2223
process.chdir(cwd);
24+
const { default: autoInstall } = await import('~package');
2325
const bundle = await rollup({
2426
input,
2527
// @ts-expect-error - rollup() ignores output here but tests kept it historically

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34

45
import del from 'del';
56
import { it, expect, afterAll } from 'vitest';
67
import { rollup } from 'rollup';
78
import nodeResolve from '@rollup/plugin-node-resolve';
89

9-
import autoInstall from '~package';
10+
// IMPORTANT: Don't import the plugin at module scope. The plugin requires Node ≥20.19
11+
// (uses import.meta.dirname). Dynamically import it inside gated tests only.
1012

11-
const DIR = import.meta.dirname;
13+
const DIR = fileURLToPath(new URL('.', import.meta.url));
1214
const cwd = path.join(DIR, 'fixtures/npm');
1315
const file = path.join(cwd, 'output/bundle.js');
1416
const input = path.join(cwd, '../input.js');
@@ -18,12 +20,14 @@ const PREV_CWD = process.cwd();
1820
const [NODE_MAJOR, NODE_MINOR] = process.versions.node.split('.').map(Number);
1921
const RUN_ON_THIS_NODE = NODE_MAJOR > 20 || (NODE_MAJOR === 20 && NODE_MINOR >= 19);
2022

21-
it.runIf(RUN_ON_THIS_NODE)('invalid manager', () => {
23+
it.runIf(RUN_ON_THIS_NODE)('invalid manager', async () => {
24+
const { default: autoInstall } = await import('~package');
2225
expect(() => autoInstall({ pkgFile, manager: 'foo' as any })).toThrow(RangeError);
2326
});
2427

2528
it.runIf(RUN_ON_THIS_NODE)('npm', async () => {
2629
process.chdir(cwd);
30+
const { default: autoInstall } = await import('~package');
2731
const bundle = await rollup({
2832
input,
2933
// @ts-expect-error - rollup() ignores output here but tests kept it historically

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34

45
import del from 'del';
56
import { it, afterAll } from 'vitest';
67
import { rollup } from 'rollup';
78
import nodeResolve from '@rollup/plugin-node-resolve';
89

9-
import autoInstall from '~package';
10+
// Dynamically import the plugin within gated tests to avoid Node <20 execution
1011

11-
const DIR = import.meta.dirname;
12+
const DIR = fileURLToPath(new URL('.', import.meta.url));
1213
const cwd = path.join(DIR, 'fixtures/pnpm-bare');
1314
const file = path.join(cwd, 'output/bundle.js');
1415
const input = path.join(cwd, '../input.js');
@@ -19,6 +20,7 @@ const RUN_ON_THIS_NODE = NODE_MAJOR > 20 || (NODE_MAJOR === 20 && NODE_MINOR >=
1920

2021
it.runIf(RUN_ON_THIS_NODE)('pnpm, bare', async () => {
2122
process.chdir(cwd);
23+
const { default: autoInstall } = await import('~package');
2224
const bundle = await rollup({
2325
input,
2426
// @ts-expect-error - rollup() ignores output here but tests kept it historically

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34

45
import del from 'del';
56
import { it, afterAll } from 'vitest';
67
import { rollup } from 'rollup';
78
import nodeResolve from '@rollup/plugin-node-resolve';
89

9-
import autoInstall from '~package';
10+
// Dynamically import the plugin within gated tests to avoid Node <20 execution
1011

11-
const DIR = import.meta.dirname;
12+
const DIR = fileURLToPath(new URL('.', import.meta.url));
1213
const cwd = path.join(DIR, 'fixtures/pnpm');
1314
const file = path.join(cwd, 'output/bundle.js');
1415
const input = path.join(cwd, '../input.js');
@@ -19,6 +20,7 @@ const RUN_ON_THIS_NODE = NODE_MAJOR > 20 || (NODE_MAJOR === 20 && NODE_MINOR >=
1920

2021
it.runIf(RUN_ON_THIS_NODE)('pnpm', async () => {
2122
process.chdir(cwd);
23+
const { default: autoInstall } = await import('~package');
2224
const bundle = await rollup({
2325
input,
2426
// @ts-expect-error - rollup() ignores output here but tests kept it historically

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34

45
import del from 'del';
56
import { it, afterAll } from 'vitest';
67
import { rollup } from 'rollup';
78
import nodeResolve from '@rollup/plugin-node-resolve';
89

9-
import autoInstall from '~package';
10+
// Dynamically import the plugin within gated tests to avoid Node <20 execution
1011

11-
const DIR = import.meta.dirname;
12+
const DIR = fileURLToPath(new URL('.', import.meta.url));
1213
const cwd = path.join(DIR, 'fixtures/yarn-bare');
1314
const file = path.join(cwd, 'output/bundle.js');
1415
const input = path.join(cwd, '../input.js');
@@ -34,6 +35,7 @@ it.runIf(RUN_ON_THIS_NODE)('yarn, bare', async () => {
3435
)
3536
);
3637
}
38+
const { default: autoInstall } = await import('~package');
3739
const bundle = await rollup({
3840
input,
3941
// @ts-expect-error - rollup() ignores output here but tests kept it historically

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34

45
import del from 'del';
56
import { it, afterAll } from 'vitest';
67
import { rollup } from 'rollup';
78
import nodeResolve from '@rollup/plugin-node-resolve';
89

9-
import autoInstall from '~package';
10+
// Dynamically import the plugin within gated tests to avoid Node <20 execution
1011

11-
const DIR = import.meta.dirname;
12+
const DIR = fileURLToPath(new URL('.', import.meta.url));
1213
const cwd = path.join(DIR, 'fixtures/yarn');
1314
const file = path.join(cwd, 'output/bundle.js');
1415
const input = path.join(cwd, '../input.js');
@@ -33,6 +34,7 @@ it.runIf(RUN_ON_THIS_NODE)('yarn', async () => {
3334
)
3435
);
3536
}
37+
const { default: autoInstall } = await import('~package');
3638
const bundle = await rollup({
3739
input,
3840
// @ts-expect-error - rollup() ignores output here but tests kept it historically

0 commit comments

Comments
 (0)