Skip to content

Commit

Permalink
fixes --conditions for bun test (oven-sh#13902)
Browse files Browse the repository at this point in the history
  • Loading branch information
snoglobe authored Sep 12, 2024
1 parent f5b7a67 commit c319794
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
36 changes: 36 additions & 0 deletions test/js/bun/resolve/import-custom-condition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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`],
Expand Down

0 comments on commit c319794

Please sign in to comment.