From c3197948c4bc6e97c1ca6ad4cc7cd735c3cfa0a3 Mon Sep 17 00:00:00 2001 From: snwy Date: Wed, 11 Sep 2024 18:04:21 -0700 Subject: [PATCH] fixes --conditions for bun test (#13902) --- src/cli.zig | 2 +- .../resolve/import-custom-condition.test.ts | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/cli.zig b/src/cli.zig index 5ff74fb50ac6d..027ce2b4950ae 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -567,7 +567,7 @@ pub const Arguments = struct { ctx.passthrough = args.remaining(); - if (cmd == .AutoCommand or cmd == .RunCommand or cmd == .BuildCommand) { + if (cmd == .AutoCommand or cmd == .RunCommand or cmd == .BuildCommand or cmd == .TestCommand) { if (args.options("--conditions").len > 0) { opts.conditions = args.options("--conditions"); } diff --git a/test/js/bun/resolve/import-custom-condition.test.ts b/test/js/bun/resolve/import-custom-condition.test.ts index 350f0665ec1f8..fa753b5a0b54a 100644 --- a/test/js/bun/resolve/import-custom-condition.test.ts +++ b/test/js/bun/resolve/import-custom-condition.test.ts @@ -7,12 +7,14 @@ let dir: string; beforeAll(() => { dir = tempDirWithFiles("customcondition", { "./node_modules/custom/index.js": "export const foo = 1;", + "./node_modules/custom/browser.js": "export const foo = 2;", "./node_modules/custom/not_allow.js": "throw new Error('should not be imported')", "./node_modules/custom/package.json": JSON.stringify({ name: "custom", exports: { "./test": { first: "./index.js", + browser: "./browser.js", default: "./not_allow.js", }, }, @@ -54,6 +56,7 @@ beforeAll(() => { }); writeFileSync(`${dir}/test.js`, `import {foo} from 'custom/test';\nconsole.log(foo);`); + writeFileSync(`${dir}/test.test.js`, `import {foo} from 'custom/test';\nconsole.log(foo);`); writeFileSync(`${dir}/test.cjs`, `const {foo} = require("custom2/test");\nconsole.log(foo);`); writeFileSync( `${dir}/multiple-conditions.js`, @@ -87,6 +90,39 @@ it("custom condition 'import' in package.json resolves", async () => { expect(stdout.toString("utf8")).toBe("1\n"); }); +it("custom condition 'import' in package.json resolves with browser condition", async () => { + const { exitCode, stdout } = Bun.spawnSync({ + cmd: [bunExe(), "--conditions=browser", `${dir}/test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + + expect(exitCode).toBe(0); + expect(stdout.toString("utf8")).toBe("2\n"); +}); + +it("custom condition 'import' in package.json resolves in bun test", async () => { + const { exitCode, stdout } = Bun.spawnSync({ + cmd: [bunExe(), "test", "--conditions=first", `${dir}/test.test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + + expect(exitCode).toBe(0); + expect(stdout.toString("utf8")).toBe("1\n"); +}); + +it("custom condition 'import' in package.json resolves in bun test with browser condition", async () => { + const { exitCode, stdout } = Bun.spawnSync({ + cmd: [bunExe(), "test", "--conditions=browser", `${dir}/test.test.js`], + env: bunEnv, + cwd: import.meta.dir, + }); + + expect(exitCode).toBe(0); + expect(stdout.toString("utf8")).toBe("2\n"); +}); + it("custom condition 'require' in package.json resolves", async () => { const { exitCode, stdout } = Bun.spawnSync({ cmd: [bunExe(), "--conditions=first", `${dir}/test.cjs`],