mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
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
This commit is contained in:
@@ -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
|
||||
|
||||
106
test/js/bun/test/preload-test.test.js
Normal file
106
test/js/bun/test/preload-test.test.js
Normal file
@@ -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");
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user