diff options
| author | 2023-05-16 11:49:35 -0400 | |
|---|---|---|
| committer | 2023-05-16 08:49:35 -0700 | |
| commit | c842f5bfe30842377a89ef91f610b523469447aa (patch) | |
| tree | 6de82519dfe8ad68dc2c3311e107240ab7209b16 /test/cli/run/run-cjs.test.ts | |
| parent | fe74c948cd3691295e2a7a8c8f6fa4229583c9ba (diff) | |
| download | bun-c842f5bfe30842377a89ef91f610b523469447aa.tar.gz bun-c842f5bfe30842377a89ef91f610b523469447aa.tar.zst bun-c842f5bfe30842377a89ef91f610b523469447aa.zip | |
Fix running commonjs modules as entry point (#2885)
* fix cjs run issue
* use a primordial
* fix behavior
* fix it again
* fix tste
Diffstat (limited to 'test/cli/run/run-cjs.test.ts')
| -rw-r--r-- | test/cli/run/run-cjs.test.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/cli/run/run-cjs.test.ts b/test/cli/run/run-cjs.test.ts new file mode 100644 index 000000000..935cbe452 --- /dev/null +++ b/test/cli/run/run-cjs.test.ts @@ -0,0 +1,60 @@ +import { expect, test } from "bun:test"; +import { mkdirSync, realpathSync } from "fs"; +import { bunEnv, bunExe } from "harness"; +import { tmpdir } from "os"; +import { join } from "path"; + +test("running a commonjs module works", async () => { + const dir = join(realpathSync(tmpdir()), "bun-run-test1"); + mkdirSync(dir, { recursive: true }); + await Bun.write(join(dir, "index1.js"), "module.exports = 1; console.log('hello world');"); + let { stdout } = Bun.spawnSync({ + cmd: [bunExe(), join(dir, "index1.js")], + cwd: dir, + env: bunEnv, + }); + expect(stdout.toString("utf8")).toEqual("hello world\n"); +}); + +test("running with Symbol.for(CommonJS)", async () => { + const dir = join(realpathSync(tmpdir()), "bun-run-test2"); + mkdirSync(dir, { recursive: true }); + await Bun.write( + join(dir, "index1.js"), + `// @bun +const fn = () => console.log('hello world'); +fn[Symbol.for("CommonJS")] = true; +export default fn; +`, + ); + let { stdout } = Bun.spawnSync({ + cmd: [bunExe(), join(dir, "index1.js")], + cwd: dir, + env: bunEnv, + }); + expect(stdout.toString("utf8")).toEqual("hello world\n"); +}); + + +test("not running with export default class", async () => { + const dir = join(realpathSync(tmpdir()), "bun-run-test2"); + mkdirSync(dir, { recursive: true }); + await Bun.write( + join(dir, "index1.js"), + `// @bun +class Foo { + constructor() { + console.log('hello world'); + } +}; +Foo[Symbol.for("CommonJS")] = true; +export default Foo +`, + ); + let { stdout } = Bun.spawnSync({ + cmd: [bunExe(), join(dir, "index1.js")], + cwd: dir, + env: bunEnv, + }); + expect(stdout.toString("utf8")).toEqual(""); +}); |
