Testing
Alchemy resources are easy to test since they’re just functions, but Alchemy also offers a simple alchemy.test
utility to help isolate your Scopes for each test suite.
Test Setup
Section titled “Test Setup”Import alchemy’s test utility and your resource:
import { describe, expect } from "bun:test";import alchemy, { destroy } from "alchemy";import { Database } from "../src/neon/database";
// make sure to augment `alchemy` by importing your preferred testing utilityimport "alchemy/test/bun";
Test Scope Creation
Section titled “Test Scope Creation”Create a test
function at the top of your test suite:
// Create test scope using filenameconst test = alchemy.test(import.meta, { prefix: BRANCH_PREFIX});
We pass import.meta
so that all the resources created in this test suite will be isolated from other tests.
Resource Test Implementation
Section titled “Resource Test Implementation”Now, create a test as you ordinarily would:
test("create, update, and delete database", async (scope) => { // ..});
Note how our test is passed a scope
value - we’ll use that at the end to clean up our resources.
Inside our test, we can simple create and update our resources, make assertions, etc.:
// Create resourcelet database = await Database(testId, { name: `${testId}-db`, // Other required properties...});
// Test assertionsexpect(database.id).toBeTruthy();
// Update resourcedatabase = await Database(testId, { // Updated properties...});
Finally, wrap all of this in a try-finally
so that we can ensure our test resources are cleaned up.
try { // (create, update and assertions)} finally { // delete all resources await destroy(scope);
// Verify resource was deleted if you want to}