Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/cli/run/module-type-fixture/cjs/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(eval("typeof module === 'undefined'"));
module.exports = {};
2 changes: 2 additions & 0 deletions test/cli/run/module-type-fixture/cjs/exports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(eval("typeof module === 'undefined'"));
module.exports = {};
2 changes: 1 addition & 1 deletion test/cli/run/module-type-fixture/cjs/import.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import * as fs from "node:fs";
console.log(eval("typeof module === 'undefined'"));
+fs;
fs.constants;
2 changes: 2 additions & 0 deletions test/cli/run/module-type-fixture/esm/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(eval("typeof module === 'undefined'"));
module.exports = {};
2 changes: 2 additions & 0 deletions test/cli/run/module-type-fixture/esm/exports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(eval("typeof module === 'undefined'"));
module.exports = {};
2 changes: 1 addition & 1 deletion test/cli/run/module-type-fixture/esm/import.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import * as fs from "node:fs";
console.log(eval("typeof module === 'undefined'"));
+fs;
fs.constants;
72 changes: 72 additions & 0 deletions test/cli/run/run-detect-module-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { join } from "path";

// package.json "type" -> file name -> expected module type of the main entry
const table = {
cjs: {
"hello.cjs": "commonjs",
"hello.js": "commonjs",
"hello.mjs": "module",
"hello.ts": "commonjs",
"hello.tsx": "module",
"hello.cts": "commonjs",
"hello.jsx": "module",
"hello.mts": "module",
// files using ES import and no exports will be detected as module
"import.cjs": "module",
// files using CommonJS markers like module.exports will be detected as
// commonjs, beating both package.json "type" and the file extension
"exports.js": "commonjs",
"exports.mjs": "commonjs",
},
esm: {
"hello.cjs": "commonjs",
"hello.js": "module",
"hello.mjs": "module",
"hello.ts": "module",
"hello.tsx": "module",
"hello.cts": "commonjs",
"hello.jsx": "module",
"hello.mts": "module",
// files using ES import and no exports will be detected as module
"import.cjs": "module",
// files using CommonJS markers like module.exports will be detected as
// commonjs, beating both package.json "type" and the file extension
"exports.js": "commonjs",
"exports.mjs": "commonjs",
},
} as const;

const cases = Object.entries(table).flatMap(([packageType, files]) =>
Object.entries(files).map(([file, expectedType]) => ({ packageType, file, expectedType })),
);

test("detect module type", async () => {
// each fixture prints `typeof module === 'undefined'`: "false" means it ran as commonjs
const actual = await Promise.all(
cases.map(async ({ packageType, file }) => {
await using proc = Bun.spawn({
cmd: [bunExe(), "run", join(import.meta.dir, "module-type-fixture", packageType, file)],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
if (exitCode !== 0) {
throw new Error(`Failed to run ${packageType}/${file}: ${stderr.trim()}`);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
if (stderr !== "") {
throw new Error(`Unexpected stderr from ${packageType}/${file}: ${stderr.trim()}`);
}
const out = stdout.trim();
const detected =
out === "true" ? "module" : out === "false" ? "commonjs" : `unexpected output ${JSON.stringify(out)}`;
return `${packageType} ${file} -> ${detected}`;
}),
);

expect(actual).toEqual(
cases.map(({ packageType, file, expectedType }) => `${packageType} ${file} -> ${expectedType}`),
);
});
54 changes: 0 additions & 54 deletions test/cli/run/run-detect-module-type.ts

This file was deleted.

38 changes: 38 additions & 0 deletions test/cli/run/run-importmetamain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";

const probe = `console.log(JSON.stringify([typeof require, import.meta.main, !import.meta.main, require.main === module, require.main !== module]));`;
Comment thread
coderabbitai[bot] marked this conversation as resolved.

test.concurrent("import.meta.main", async () => {
using dir = tempDir("importmetamain-esm", {
"index1.js": `import "fs"; ${probe}`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "index1.js"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(stdout.trim()).toBe(JSON.stringify(["function", true, false, true, false]));
expect(exitCode).toBe(0);
});

test.concurrent("import.meta.main in a common.js file", async () => {
using dir = tempDir("importmetamain-cjs", {
"index1.js": `module.exports = {}; ${probe}`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "index1.js"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(stdout.trim()).toBe(JSON.stringify(["function", true, false, true, false]));
expect(exitCode).toBe(0);
});
38 changes: 0 additions & 38 deletions test/cli/run/run-importmetamain.ts

This file was deleted.