Tell Bun to load this plugin before your tests run
preload = ["./svelte-loader.ts"]
This also works:
test.preload = ["./svelte-loader.ts"]
* * *
Add an example `.svelte` file in your project.
Counter.svelte
<script>
export let initialCount = 0;
let count = initialCount;
</script>
<button on:click="{()" ="">(count += 1)}>+1</button>
* * *
Now you can `import` or `require` `*.svelte` files in your tests, and it will load the Svelte component as a JavaScript module.
hello-svelte.test.ts
import { test, expect } from "bun:test"; import { render, fireEvent } from "@testing-library/svelte"; import Counter from "./Counter.svelte";
test("Counter increments when clicked", async () => { const { getByText, component } = render(Counter); const button = getByText("+1");
// Initial state expect(component.$$.ctx[0]).toBe(0); // initialCount is the first prop
// Click the increment button await fireEvent.click(button);
// Check the new state expect(component.$$.ctx[0]).toBe(1); });
* * *
Use `bun test` to run your tests.
terminal
bun test