Skip to main content

Testing

Alchemy provides built-in testing utilities powered by Vitest for testing infrastructure resources. Tests create real resources, verify their behavior, and clean up automatically.

Setting Up Tests

Alchemy tests use alchemy.test() to create isolated test scopes:

Test Structure

Test Lifecycle

Alchemy tests follow a consistent pattern:

Test Configuration

Test Prefix

Use a unique prefix to avoid conflicts between test runs:
Use branch names or CI job IDs as prefixes to ensure tests from different branches don’t conflict.

Test Timeout

Set timeout for long-running tests:

Quiet Mode

Disable logging in tests:

Testing Patterns

Create, Update, Delete

Test the full resource lifecycle:

Resource Dependencies

Test resources that depend on each other:

End-to-End Testing

Test complete workflows:

Error Handling

Test error conditions:

Verification Helpers

Fetch and Expect OK

Create a helper for testing HTTP endpoints:

Resource Existence Verification

Verify resources are truly deleted:

Test Hooks

beforeAll and afterAll

Run setup and teardown for all tests:

Running Tests

Run All Tests

This automatically:
  • Diffs with main branch
  • Runs only changed tests
  • Requires you to be on a feature branch

Run Specific Tests

Watch Mode

Re-run tests on file changes:

Test Environment Variables

Tests require provider credentials:
Load them in your test file:

State Storage for Tests

Tests use SQLite by default for state storage:
Or use other state stores:

Best Practices

Example: Complete Test Suite

Troubleshooting

Test Timeouts

Increase timeout for slow resources:

State Conflicts

Use unique prefixes to avoid conflicts:

Cleanup Failures

Manually destroy orphaned resources:

Next Steps