diff options
author | 2023-03-07 12:22:34 -0800 | |
---|---|---|
committer | 2023-03-07 12:22:34 -0800 | |
commit | f7e4eb83694aa007a492ef66c28ffbe6a2dae791 (patch) | |
tree | 7af25aa5c42a2e1b2b47ba1df35f8caa9054cbeb /test/bun.js/preload-test.test.js | |
parent | 36275a44ce7a33587bd26aad120042ab95470ff3 (diff) | |
download | bun-f7e4eb83694aa007a492ef66c28ffbe6a2dae791.tar.gz bun-f7e4eb83694aa007a492ef66c28ffbe6a2dae791.tar.zst bun-f7e4eb83694aa007a492ef66c28ffbe6a2dae791.zip |
Reorganize tests (#2332)
Diffstat (limited to 'test/bun.js/preload-test.test.js')
-rw-r--r-- | test/bun.js/preload-test.test.js | 227 |
1 files changed, 0 insertions, 227 deletions
diff --git a/test/bun.js/preload-test.test.js b/test/bun.js/preload-test.test.js deleted file mode 100644 index 76603b3a0..000000000 --- a/test/bun.js/preload-test.test.js +++ /dev/null @@ -1,227 +0,0 @@ -import { spawnSync } from "bun"; -import { describe, expect, test } from "bun:test"; -import { mkdirSync, realpathSync } from "fs"; -import { tmpdir } from "os"; -import { join } from "path"; -import { bunEnv } from "./bunEnv"; -import { bunExe } from "./bunExe"; -const preloadModule = ` -import {plugin} from 'bun'; - -plugin({ - setup(build) { - build.onResolve({ filter: /.*\.txt$/, }, async (args) => { - return { - path: args.path, - namespace: 'boop' - } - }); - build.onResolve({ namespace: "boop", filter: /.*/ }, async (args) => { - return { - path: args.path, - namespace: 'boop' - } - }); - build.onLoad({ namespace: "boop", filter: /.*/ }, async (args) => { - return { - contents: '"hello world"', - loader: 'json' - } - }); - } -}); - `; - -const mainModule = ` - import hey from './hey.txt'; - - if (hey !== 'hello world') { - throw new Error('preload test failed'); - } - - console.log('Test passed'); - process.exit(0); -`; - -const bunfig = `preload = ["./preload.js"]`; - -describe("preload", () => { - test("works", async () => { - const preloadDir = join(realpathSync(tmpdir()), "bun-preload-test"); - mkdirSync(preloadDir, { recursive: true }); - const preloadPath = join(preloadDir, "preload.js"); - const mainPath = join(preloadDir, "main.js"); - const bunfigPath = join(preloadDir, "bunfig.toml"); - await Bun.write(preloadPath, preloadModule); - await Bun.write(mainPath, mainModule); - await Bun.write(bunfigPath, bunfig); - - const cmds = [ - [bunExe(), "run", mainPath], - [bunExe(), mainPath], - ]; - - for (let cmd of cmds) { - const { stderr, exitCode, stdout } = spawnSync({ - cmd, - cwd: preloadDir, - stderr: "pipe", - stdout: "pipe", - env: bunEnv, - }); - - expect(exitCode).toBe(0); - expect(stderr.toString()).toBe(""); - expect(stdout.toString()).toContain("Test passed"); - } - }); - - test("works from CLI", async () => { - const preloadDir = join(realpathSync(tmpdir()), "bun-preload-test4"); - mkdirSync(preloadDir, { recursive: true }); - const preloadPath = join(preloadDir, "preload.js"); - const mainPath = join(preloadDir, "main.js"); - await Bun.write(preloadPath, preloadModule); - await Bun.write(mainPath, mainModule); - - const cmds = [ - [bunExe(), "-r=" + preloadPath, "run", mainPath], - [bunExe(), "-r=" + preloadPath, mainPath], - ]; - - for (let cmd of cmds) { - const { stderr, exitCode, stdout } = spawnSync({ - cmd, - cwd: preloadDir, - stderr: "pipe", - stdout: "pipe", - env: bunEnv, - }); - - expect(exitCode).toBe(0); - expect(stderr.toString()).toBe(""); - expect(stdout.toString()).toContain("Test passed"); - } - }); - - describe("as entry point", () => { - const preloadModule = ` -import {plugin} from 'bun'; - -plugin({ - setup(build) { - build.onResolve({ filter: /.*\.txt$/, }, async (args) => { - return { - path: args.path, - namespace: 'boop' - } - }); - build.onResolve({ namespace: "boop", filter: /.*/ }, async (args) => { - return { - path: args.path, - namespace: 'boop' - } - }); - build.onLoad({ namespace: "boop", filter: /.*/ }, async (args) => { - return { - contents: 'console.log("Test passed")', - loader: 'js' - } - }); - } -}); - `; - - test("works from CLI", async () => { - const preloadDir = join(realpathSync(tmpdir()), "bun-preload-test6"); - mkdirSync(preloadDir, { recursive: true }); - const preloadPath = join(preloadDir, "preload.js"); - const mainPath = join(preloadDir, "boop.txt"); - await Bun.write(preloadPath, preloadModule); - await Bun.write(mainPath, "beep"); - - const cmds = [ - [bunExe(), "-r=" + preloadPath, "run", mainPath], - [bunExe(), "-r=" + preloadPath, mainPath], - ]; - - for (let cmd of cmds) { - const { stderr, exitCode, stdout } = spawnSync({ - cmd, - cwd: preloadDir, - stderr: "pipe", - stdout: "pipe", - env: bunEnv, - }); - - expect(stderr.toString()).toBe(""); - expect(stdout.toString()).toContain("Test passed"); - expect(exitCode).toBe(0); - } - }); - }); - - test("throws an error when preloaded module fails to execute", async () => { - const preloadModule = "throw new Error('preload test failed');"; - - const preloadDir = join(realpathSync(tmpdir()), "bun-preload-test3"); - mkdirSync(preloadDir, { recursive: true }); - const preloadPath = join(preloadDir, "preload.js"); - const mainPath = join(preloadDir, "main.js"); - const bunfigPath = join(preloadDir, "bunfig.toml"); - await Bun.write(preloadPath, preloadModule); - await Bun.write(mainPath, mainModule); - await Bun.write(bunfigPath, bunfig); - - const cmds = [ - [bunExe(), "run", mainPath], - [bunExe(), mainPath], - ]; - - for (let cmd of cmds) { - const { stderr, exitCode, stdout } = spawnSync({ - cmd, - cwd: preloadDir, - stderr: "pipe", - stdout: "pipe", - env: bunEnv, - }); - - expect(stderr.toString()).toContain("preload test failed"); - expect(stdout.toString()).toBe(""); - expect(exitCode).toBe(1); - } - }); - - test("throws an error when preloaded module not found", async () => { - const bunfig = `preload = ["./bad-file.js"]`; - - const preloadDir = join(realpathSync(tmpdir()), "bun-preload-test2"); - mkdirSync(preloadDir, { recursive: true }); - const preloadPath = join(preloadDir, "preload.js"); - const mainPath = join(preloadDir, "main.js"); - const bunfigPath = join(preloadDir, "bunfig.toml"); - await Bun.write(preloadPath, preloadModule); - await Bun.write(mainPath, mainModule); - await Bun.write(bunfigPath, bunfig); - - const cmds = [ - [bunExe(), "run", mainPath], - [bunExe(), mainPath], - ]; - - for (let cmd of cmds) { - const { stderr, exitCode, stdout } = spawnSync({ - cmd, - cwd: preloadDir, - stderr: "pipe", - stdout: "pipe", - env: bunEnv, - }); - - expect(stderr.toString()).toContain("preload not found "); - expect(stdout.toString()).toBe(""); - expect(exitCode).toBe(1); - } - }); -}); |