mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
Add --import as alias to --preload for nodejs compat (#20523)
This commit is contained in:
1
test/config/bunfig/fixtures/preload/many/index.ts
Normal file
1
test/config/bunfig/fixtures/preload/many/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
console.log(globalThis.preload);
|
||||
1
test/config/bunfig/fixtures/preload/many/preload1.ts
Normal file
1
test/config/bunfig/fixtures/preload/many/preload1.ts
Normal file
@@ -0,0 +1 @@
|
||||
(globalThis.preload ??= []).push("multi/preload1.ts");
|
||||
1
test/config/bunfig/fixtures/preload/many/preload2.ts
Normal file
1
test/config/bunfig/fixtures/preload/many/preload2.ts
Normal file
@@ -0,0 +1 @@
|
||||
(globalThis.preload ??= []).push("multi/preload2.ts");
|
||||
1
test/config/bunfig/fixtures/preload/many/preload3.ts
Normal file
1
test/config/bunfig/fixtures/preload/many/preload3.ts
Normal file
@@ -0,0 +1 @@
|
||||
(globalThis.preload ??= []).push("multi/preload3.ts");
|
||||
@@ -7,13 +7,17 @@ const fixturePath = (...segs: string[]) => resolve(import.meta.dirname, "fixture
|
||||
type Opts = {
|
||||
args?: string[];
|
||||
cwd?: string;
|
||||
env?: Record<string, string>;
|
||||
};
|
||||
type Out = [stdout: string, stderr: string, exitCode: number];
|
||||
const run = (file: string, { args = [], cwd }: Opts = {}): Promise<Out> => {
|
||||
const run = (file: string, { args = [], cwd, env = {} }: Opts = {}): Promise<Out> => {
|
||||
const res = Bun.spawn([bunExe(), ...args, file], {
|
||||
cwd,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
env: bunEnv,
|
||||
env: {
|
||||
...env,
|
||||
...bunEnv,
|
||||
},
|
||||
} satisfies SpawnOptions.OptionsObject<"ignore", "pipe", "pipe">);
|
||||
|
||||
return Promise.all([
|
||||
@@ -134,3 +138,65 @@ describe("Given a `bunfig.toml` file with a relative path without a leading './'
|
||||
expect(code).toBe(0);
|
||||
});
|
||||
}); // </given a `bunfig.toml` file with a relative path without a leading './'>
|
||||
|
||||
describe("Test that all the aliases for --preload work", () => {
|
||||
const dir = fixturePath("many");
|
||||
|
||||
it.each(["--preload=./preload1.ts", "--require=./preload1.ts", "--import=./preload1.ts"])(
|
||||
"When `bun run` is run with %s, the preload is executed",
|
||||
async flag => {
|
||||
const [out, err, code] = await run("index.ts", { args: [flag], cwd: dir });
|
||||
expect(err).toBeEmpty();
|
||||
expect(out).toBe('[ "multi/preload1.ts" ]');
|
||||
expect(code).toBe(0);
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["1", "2", "3", "4"])(
|
||||
"When multiple preload flags are used, they execute in order: --preload, --require, --import (#%s)",
|
||||
async i => {
|
||||
let args: string[] = [];
|
||||
if (i === "1") args = ["--preload", "./preload1.ts", "--require", "./preload2.ts", "--import", "./preload3.ts"];
|
||||
if (i === "2") args = ["--import", "./preload3.ts", "--preload=./preload1.ts", "--require", "./preload2.ts"];
|
||||
if (i === "3") args = ["--require", "./preload2.ts", "--import", "./preload3.ts", "--preload", "./preload1.ts"];
|
||||
if (i === "4") args = ["--require", "./preload1.ts", "--import", "./preload3.ts", "--require", "./preload2.ts"];
|
||||
const [out, err, code] = await run("index.ts", { args, cwd: dir });
|
||||
expect(err).toBeEmpty();
|
||||
expect(out).toBe('[ "multi/preload1.ts", "multi/preload2.ts", "multi/preload3.ts" ]');
|
||||
expect(code).toBe(0);
|
||||
},
|
||||
);
|
||||
|
||||
it("Duplicate preload flags are only executed once", async () => {
|
||||
const args = ["--preload", "./preload1.ts", "--require", "./preload1.ts", "--import", "./preload1.ts"];
|
||||
const [out, err, code] = await run("index.ts", { args, cwd: dir });
|
||||
expect(err).toBeEmpty();
|
||||
expect(out).toBe('[ "multi/preload1.ts" ]');
|
||||
expect(code).toBe(0);
|
||||
});
|
||||
|
||||
it("Test double preload flags", async () => {
|
||||
const dir = fixturePath("many");
|
||||
const args = [
|
||||
"--preload",
|
||||
"./preload1.ts",
|
||||
"--preload=./preload2.ts",
|
||||
"--preload",
|
||||
"./preload3.ts",
|
||||
"-r",
|
||||
"./preload3.ts",
|
||||
];
|
||||
const [out, err, code] = await run("index.ts", { args, cwd: dir });
|
||||
expect(err).toBeEmpty();
|
||||
expect(out).toMatchInlineSnapshot(`"[ "multi/preload1.ts", "multi/preload2.ts", "multi/preload3.ts" ]"`);
|
||||
expect(code).toBe(0);
|
||||
});
|
||||
}); // </Test that all the aliases for --preload work>
|
||||
|
||||
test("Test BUN_INSPECT_PRELOAD is used to set preloads", async () => {
|
||||
const dir = fixturePath("many");
|
||||
const [out, err, code] = await run("index.ts", { args: [], cwd: dir, env: { BUN_INSPECT_PRELOAD: "./preload1.ts" } });
|
||||
expect(err).toBeEmpty();
|
||||
expect(out).toMatchInlineSnapshot(`"[ "multi/preload1.ts" ]"`);
|
||||
expect(code).toBe(0);
|
||||
}); // </Test BUN_INSPECT_PRELOAD is used to set preloads>
|
||||
|
||||
Reference in New Issue
Block a user