72 lines
2.4 KiB
TypeScript
72 lines
2.4 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { bunEnv, bunExe, tempDir } from "harness";
|
|
|
|
// tsconfig "jsx": "react-jsx" selects the production automatic runtime (jsx/jsxs),
|
|
// "react-jsxdev" selects the development runtime (jsxDEV), matching TypeScript/esbuild.
|
|
// https://github.com/oven-sh/bun/issues/4227
|
|
|
|
const shimFiles = {
|
|
"node_modules/shim/package.json": JSON.stringify({
|
|
name: "shim",
|
|
version: "1.0.0",
|
|
type: "module",
|
|
exports: {
|
|
".": "./rt.js",
|
|
"./jsx-runtime": "./rt.js",
|
|
"./jsx-dev-runtime": "./dev.js",
|
|
},
|
|
}),
|
|
"node_modules/shim/rt.js": `
|
|
export const Fragment = Symbol.for("F");
|
|
export const jsx = () => (console.log("prod jsx"), {});
|
|
export const jsxs = jsx;
|
|
`,
|
|
"node_modules/shim/dev.js": `
|
|
export const Fragment = Symbol.for("F");
|
|
export const jsxDEV = () => (console.log("dev jsxDEV"), {});
|
|
`,
|
|
"m.jsx": `const a = <div p="1">x</div>;\nglobalThis.s = a;\n`,
|
|
};
|
|
|
|
describe("tsconfig compilerOptions.jsx", () => {
|
|
test.each([
|
|
["react-jsx", "prod jsx", "shim/jsx-runtime"],
|
|
["react-jsxdev", "dev jsxDEV", "shim/jsx-dev-runtime"],
|
|
] as const)('"%s" selects the matching automatic runtime', async (jsx, runStdout, importSource) => {
|
|
using dir = tempDir("jsx-tsconfig", {
|
|
...shimFiles,
|
|
"tsconfig.json": JSON.stringify({
|
|
compilerOptions: { jsx, jsxImportSource: "shim" },
|
|
}),
|
|
});
|
|
|
|
// bun run
|
|
{
|
|
await using proc = Bun.spawn({
|
|
cmd: [bunExe(), "run", "m.jsx"],
|
|
env: { ...bunEnv, NODE_ENV: undefined, BUN_ENV: undefined },
|
|
cwd: String(dir),
|
|
stdout: "pipe",
|
|
stderr: "inherit",
|
|
});
|
|
const [stdout, exitCode] = await Promise.all([proc.stdout.text(), proc.exited]);
|
|
expect({ stdout: stdout.trim(), exitCode }).toEqual({ stdout: runStdout, exitCode: 0 });
|
|
}
|
|
|
|
// bun build
|
|
{
|
|
await using proc = Bun.spawn({
|
|
cmd: [bunExe(), "build", "m.jsx", "--external", "shim*"],
|
|
env: { ...bunEnv, NODE_ENV: undefined, BUN_ENV: undefined },
|
|
cwd: String(dir),
|
|
stdout: "pipe",
|
|
stderr: "inherit",
|
|
});
|
|
const [stdout, exitCode] = await Promise.all([proc.stdout.text(), proc.exited]);
|
|
expect(stdout).toContain(`"${importSource}"`);
|
|
expect(stdout).not.toContain(importSource === "shim/jsx-runtime" ? "jsx-dev-runtime" : '"shim/jsx-runtime"');
|
|
expect(exitCode).toBe(0);
|
|
}
|
|
});
|
|
});
|