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, bunExe } from "harness"; const preloadModule = ` import {plugin} from 'bun'; plugin({ setup(build) { build.onResolve({ filter: /.*\.txt$/, }, 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, got ' + hey); } console.log('Test passed'); process.exit(0); `; const bunfig = `preload = ["./preload.js"]`; describe("preload", () => { test.todo("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(stderr.toString()).toBe(""); expect(stdout.toString()).toContain("Test passed"); expect(exitCode).toBe(0); } }); test.todo("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(stderr.toString()).toBe(""); expect(stdout.toString()).toContain("Test passed"); expect(exitCode).toBe(0); } }); describe("as entry point", () => { const preloadModule = ` import {plugin} from 'bun'; console.log('preload') plugin({ setup(build) { build.onResolve({ filter: /.*\.txt$/, }, async (args) => { return { path: args.path, namespace: 'boop' } }); build.onLoad({ namespace: "boop", filter: /.*/ }, async (args) => { return { contents: 'console.log("Test passed")', loader: 'js' } }); } }); `; test.todo("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); } }); }); n> Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/test/snippets/code-simplification-neql-define.js (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2023-02-12Add release signing to release workflowGravatar Ashcon Partovi 1-0/+34
2023-02-12prettier + stop serverGravatar Jarred Sumner 1-119/+101
2023-02-12Return server on listen (#2057)Gravatar MichaƂ Warda 2-1/+10
2023-02-12Set `require("module").globalPaths` to empty arrayGravatar Jarred Sumner 2-0/+8
2023-02-11fix string corruption in FS entry cache (#2055)Gravatar Alex Lam S.L 6-52/+72
2023-02-11fix segfault during non-install script execution (#2045)Gravatar Alex Lam S.L 8-219/+107
2023-02-11[WIP] feat(napi): add `napi-fatal-exception` (#2054)Gravatar Derrick Farris 3-0/+17
2023-02-11Fixes https://github.com/oven-sh/bun/issues/2052Gravatar Jarred Sumner 3-44/+19
2023-02-11Backport std::forward changeGravatar Jarred Sumner 50-292/+292
2023-02-10Fix #631: bun add throwing JSON lexer bug (#2041)Gravatar Justin Whear 2-1/+39
2023-02-10Fix PATH setup in macOS setup instructions (#2044)Gravatar Eric Zhang 1-1/+1
2023-02-10update to simdutf 3.2.0Gravatar Jarred Sumner 2-341/+859
2023-02-10fix(string_immutable): fix `toUTF16Alloc` for bindgen w/ `use_simdutf = false...Gravatar Derrick Farris 1-1/+2
2023-02-10Add bun-ecosystem for running tests on npm packagesGravatar Ashcon Partovi 7-0/+201
2023-02-10[install] fix duplicate check on `peerDependencies` (#2039)Gravatar Alex Lam S.L 2-9/+53
2023-02-10drop ASCII double-scan and other minor clean-ups (#2038)Gravatar Alex Lam S.L 1-137/+86
2023-02-10Enable https://github.com/tc39/proposal-set-methodsGravatar Jarred Sumner 1-0/+1
2023-02-09Upgrade WebKitGravatar Jarred Sumner 11-31/+31
2023-02-09fix assertion failure (#2033)Gravatar Alex Lam S.L 2-5/+10
2023-02-09[install] fix flaky tests (#2032)Gravatar Alex Lam S.L 2-37/+37
2023-02-09[simdutf] workaround validation OOB access (#2031)Gravatar Alex Lam S.L 2-15/+18
2023-02-09Workaround https://github.com/simdutf/simdutf/issues/213Gravatar Jarred Sumner 1-3/+4
2023-02-09[streams] fix byte accounting (#2029)Gravatar Alex Lam S.L 2-31/+45
2023-02-09Add a note about builtinsGravatar Jarred Sumner 1-0/+16
2023-02-09[bun:test] Auto-import jest globals in test filesGravatar Jarred Sumner 9-0/+135
2023-02-08move some code aroundGravatar Jarred Sumner 1-64/+91
2023-02-08Update CONTRIBUTING.mdGravatar Jarred Sumner 1-1/+1