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:
Jake Boone
2023-04-05 18:28:41 -07:00
committed by GitHub
parent b50f3d3f6f
commit 864302a634
2 changed files with 107 additions and 1 deletions

View File

@@ -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

View 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");
}
});
});