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
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 utility
import "alchemy/test/bun";
Test Scope Creation
Create a test
function at the top of your test suite:
// Create test scope using filename
const test = alchemy.test(import.meta);
We pass import.meta
so that all the resources created in this test suite will be isolated from other tests.
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 resource
let database = await Database(testId, {
name: `${testId}-db`,
// Other required properties...
});
// Test assertions
expect(database.id).toBeTruthy();
// Update resource
database = 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
}
TIP
It's recommended to use a try-finally
so that you can assert the resource was actually deleted.