diff options
author | 2023-04-05 18:28:41 -0700 | |
---|---|---|
committer | 2023-04-05 18:28:41 -0700 | |
commit | 864302a634ff4539c188845c981d4402e2a1a908 (patch) | |
tree | eaf621ffcbc5a19f56f88c32cf7803e1f6eeb94c | |
parent | b50f3d3f6f429edd674a6c5dd8d182ba77361b69 (diff) | |
download | bun-864302a634ff4539c188845c981d4402e2a1a908.tar.gz bun-864302a634ff4539c188845c981d4402e2a1a908.tar.zst bun-864302a634ff4539c188845c981d4402e2a1a908.zip |
Add tests for `bun test` with preload scripts (#2566)
* Use zsh-compatible syntax in cowsay example
zsh interprets the string !" differently than bash or sh, but we can use single quotes in all of them. See https://unix.stackexchange.com/a/497335/548905.
* Add tests for bun:test with preload scripts
* Look at `stderr` in `bun test --preload` tests
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | test/js/bun/test/preload-test.test.js | 106 |
2 files changed, 107 insertions, 1 deletions
@@ -40,7 +40,7 @@ bun run index.tsx # TS and JSX supported out of the box bun test # run tests bun run start # run the `start` script in `package.json` bun install <pkg> # install a package -bunx cowsay "Hello, world!" # execute a package +bunx cowsay 'Hello, world!' # execute a package ``` ## Install diff --git a/test/js/bun/test/preload-test.test.js b/test/js/bun/test/preload-test.test.js new file mode 100644 index 000000000..2a093428d --- /dev/null +++ b/test/js/bun/test/preload-test.test.js @@ -0,0 +1,106 @@ +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.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 { expect, test } from 'bun:test'; +import hey from './hey.txt'; + +test('says hello world', () => { + expect(hey).toBe('hello world'); +}); +`; + +const bunfig = `preload = ["./preload.js"]`; + +describe("preload for bun:test", () => { + test("works with bunfig", async () => { + const preloadDir = join(realpathSync(tmpdir()), "bun-test-preload-test1"); + mkdirSync(preloadDir, { recursive: true }); + const preloadPath = join(preloadDir, "preload.js"); + const mainPath = join(preloadDir, "main.test.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(), "test", mainPath], + ]; + + for (let cmd of cmds) { + const { stderr, exitCode, stdout } = spawnSync({ + cmd, + cwd: preloadDir, + stderr: "pipe", + stdout: "pipe", + env: bunEnv, + }); + + expect(exitCode).toBe(0); + const str = stderr.toString(); + expect(str).toContain("✓ says hello world"); + expect(str).toContain("1 pass"); + expect(str).toContain("0 fail"); + } + }); + + test("works from CLI", async () => { + const preloadDir = join(realpathSync(tmpdir()), "bun-test-preload-test2"); + mkdirSync(preloadDir, { recursive: true }); + const preloadPath = join(preloadDir, "preload.js"); + const mainPath = join(preloadDir, "main.test.js"); + await Bun.write(preloadPath, preloadModule); + await Bun.write(mainPath, mainModule); + + const cmds = [ + [bunExe(), `-r=${preloadPath}`, "test", mainPath], + ]; + + for (let cmd of cmds) { + const { stderr, exitCode, stdout } = spawnSync({ + cmd, + cwd: preloadDir, + stderr: "pipe", + stdout: "pipe", + env: bunEnv, + }); + + expect(exitCode).toBe(0); + const str = stderr.toString(); + expect(str).toContain("✓ says hello world"); + expect(str).toContain("1 pass"); + expect(str).toContain("0 fail"); + } + }); +}); |