Files
bun.sh/test/cli/run/self-reference.test.ts
Jarred Sumner 2e02d9de28 Use ReadableStream.prototype.* in tests instead of new Response(...).* (#20937)
Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Alistair Smith <hi@alistair.sh>
Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
2025-07-14 00:47:53 -07:00

69 lines
2.0 KiB
TypeScript

import { spawn } from "bun";
import { describe, expect, test } from "bun:test";
import { writeFile } from "fs/promises";
import { bunExe, tmpdirSync } from "harness";
import { join } from "path";
const testWord = "bunny";
const testString = `${testWord} ${testWord}`;
describe("bun", () => {
test("should resolve self-imports by name", async () => {
const tempDir = tmpdirSync();
for (const packageName of ["pkg", "@scope/pkg"]) {
// general check without exports
await writeFile(
join(tempDir, "package.json"),
JSON.stringify({
name: packageName,
}),
);
await writeFile(join(tempDir, "index.js"), `module.exports.testWord = "${testWord}";`);
await writeFile(
join(tempDir, "other.js"),
`const pkg = require("${packageName}");\nimport pkg2 from "${packageName}"\nconsole.log(pkg.testWord,pkg2.testWord);`,
);
let subprocess = spawn({
cmd: [bunExe(), "run", "other.js"],
cwd: tempDir,
stdout: "pipe",
});
let out = await subprocess.stdout.text();
expect(out).not.toContain(testString);
// should not resolve not exported files
await writeFile(
join(tempDir, "package.json"),
JSON.stringify({
name: packageName,
exports: { "./index.js": "./index.js" },
}),
);
subprocess = spawn({
cmd: [bunExe(), "run", "other.js"],
cwd: tempDir,
stdout: "pipe",
});
out = await subprocess.stdout.text();
expect(out).not.toContain(testString);
// should resolve exported files
await writeFile(
join(tempDir, "other.js"),
`const pkg = require("${packageName}/index.js");\nimport pkg2 from "${packageName}/index.js"\nconsole.log(pkg.testWord,pkg2.testWord);`,
);
subprocess = spawn({
cmd: [bunExe(), "run", "other.js"],
cwd: tempDir,
stdout: "pipe",
});
out = await subprocess.stdout.text();
expect(out).toContain(testString);
}
});
});