aboutsummaryrefslogtreecommitdiff
path: root/docs/test
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-05-21 00:48:25 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-05-21 00:48:25 -0700
commitd5c9e5ee79e7b8eae0a8ff2d42d1f38319add0dd (patch)
tree4aed4fd724d5c4be6f5483c7a88b4024387c2fa2 /docs/test
parentdef8824bc283ecea4932fb173ba4cd8f191345bc (diff)
downloadbun-d5c9e5ee79e7b8eae0a8ff2d42d1f38319add0dd.tar.gz
bun-d5c9e5ee79e7b8eae0a8ff2d42d1f38319add0dd.tar.zst
bun-d5c9e5ee79e7b8eae0a8ff2d42d1f38319add0dd.zip
Add --preload docs
Diffstat (limited to 'docs/test')
-rw-r--r--docs/test/extending.md135
1 files changed, 135 insertions, 0 deletions
diff --git a/docs/test/extending.md b/docs/test/extending.md
new file mode 100644
index 000000000..d0c700a64
--- /dev/null
+++ b/docs/test/extending.md
@@ -0,0 +1,135 @@
+# Lifecycle hooks
+
+Like the runtime, `bun:test` also supports `--preload` scripts. These scripts are loaded before any tests are run. This is useful for setting up test fixtures, mocking, and configuring the test environment.
+
+{% codetab }
+
+```ts#preloaded.ts
+import { beforeAll, beforeEach, afterEach, afterAll } from "bun:test";
+
+beforeAll(() => {
+ console.log("beforeAll");
+});
+
+beforeEach(() => {
+ console.log("beforeEach");
+});
+
+afterEach(() => {
+ console.log("afterEach");
+});
+
+afterAll(() => {
+ console.log("afterAll");
+});
+```
+
+{% /codetab %}
+
+Test file:
+
+```ts#my-test.test.ts
+import { expect, test } from "bun:test";
+
+test("1 + 1", () => {
+ expect(1 + 1).toEqual(2);
+ console.log("1 + 1");
+});
+```
+
+Run the test with `--preload`:
+
+```sh
+$ bun test --preload=preloaded.ts
+```
+
+It outputs:
+
+```sh
+beforeAll
+beforeEach
+1 + 1
+afterEach
+afterAll
+```
+
+## List of lifecycle hooks
+
+The following lifecycle hooks are available in `--preload`:
+
+| Hook | Description |
+| ------------ | --------------------------- |
+| `beforeAll` | Runs once before all tests. |
+| `beforeEach` | Runs before each test. |
+| `afterEach` | Runs after each test. |
+| `afterAll` | Runs once after all tests. |
+
+# Configuration
+
+To save yourself from having to type `--preload` every time you run tests, you can add it to your `bunfig.toml`:
+
+```toml
+[test]
+preload = ["preloaded.ts"]
+```
+
+# Loaders & Resolvers
+
+{% note %}
+Plugin support is not implemented yet. There is a bug and this feature is not working.
+{% /note %}
+
+`bun:test` supports the same plugin API as bun's runtime and bun's bundler. See [Plugins](docs/bundler/plugins#usage) for more information.
+
+## Example loader
+
+{% codetab }
+
+```ts#loader.ts
+import { plugin } from 'bun';
+
+plugin({
+ name: 'My loader',
+ setup(build) {
+ build.onResolve({ filter: /\.txt$/ }, (args) => {
+ return {
+ path: args.path,
+ namespace: 'my-loader',
+ };
+ });
+
+ build.onLoad({ filter: /my-loader:.txt$/ }, (args) => {
+ return {
+ contents: 'Hello world!',
+ loader: 'text',
+ };
+ });
+ },
+});
+```
+
+{% /codetab %}
+
+Now in your test file, you can import `.txt` files:
+
+```ts#my-test.test.ts
+import { expect, test } from "bun:test";
+import text from "./hello.txt";
+
+test("text is 'Hello world!'", () => {
+ expect(text).toEqual("Hello world!");
+});
+```
+
+To run the test, you need to add `loader.ts` to `preload`:
+
+```toml
+[test]
+preload = ["loader.ts"]
+```
+
+Or you can pass --preload to the command line:
+
+```sh
+$ bun test --preload=loader.ts
+```