cdktn-vitest
v1.0.0
Published
Custom matchers library for Vitest
Readme
cdktn-vitest
cdktn matchers library for Vitest.
This library provides a set of matchers that allow you to write clear and expressive tests for your Terraform infrastructure code written with CDKTN.
Check out the CDKTN documentation at https://cdktn.io/docs/test/unit-tests#write-assertions to learn more.
Installation
npm install cdktn-vitest
# or
pnpm add cdktn-vitestSetup
1. Create vitest.setup.ts
Create a new file in your project root:
// vitest.setup.ts
import { setupVitest } from "cdktn-vitest";
setupVitest();2. Update vitest.config.ts
Add the setup file to your Vitest configuration:
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
setupFiles: ["./vitest.setup.ts"],
},
});3. Update tsconfig.json
Add the library types to your TypeScript configuration:
{
"compilerOptions": {
"types": ["cdktn-vitest"]
}
}Available Matchers
Resource Testing
toHaveResource(resourceConstructor)
Verify that a resource exists in the synthesized Terraform output.
expect(synthesized).toHaveResource(MyResource);toHaveResourceWithProperties(resourceConstructor, properties)
Verify that a resource exists with specific properties.
expect(synthesized).toHaveResourceWithProperties(MyResource, {
name: "my-resource",
region: "us-east-1",
});Data Source Testing
toHaveDataSource(dataSourceConstructor)
Verify that a data source exists in the synthesized output.
expect(synthesized).toHaveDataSource(MyDataSource);toHaveDataSourceWithProperties(dataSourceConstructor, properties)
Verify that a data source exists with specific properties.
expect(synthesized).toHaveDataSourceWithProperties(MyDataSource, {
filter: "value",
});Provider Testing
toHaveProvider(providerConstructor)
Verify that a provider is configured.
expect(synthesized).toHaveProvider(AwsProvider);toHaveProviderWithProperties(providerConstructor, properties)
Verify that a provider is configured with specific properties.
expect(synthesized).toHaveProviderWithProperties(AwsProvider, {
region: "us-east-1",
});Terraform Validation
toBeValidTerraform()
Check if the synthesized Terraform code is valid.
expect(synthesized).toBeValidTerraform();toPlanSuccessfully()
Verify that terraform plan executes successfully.
expect(synthesized).toPlanSuccessfully();Example Test
import { describe, it, expect } from "vitest";
import { MyStack } from "./my-stack";
import { MyResource } from "cdktn/lib/resources";
describe("MyStack", () => {
it("should create a resource with correct properties", () => {
const stack = new MyStack();
const synthesized = stack.synth().toString();
expect(synthesized).toHaveResourceWithProperties(MyResource, {
name: "my-resource",
enabled: true,
});
});
it("should be valid Terraform", () => {
const stack = new MyStack();
const synthesized = stack.synth().toString();
expect(synthesized).toBeValidTerraform();
});
});Peer Dependencies
This library requires the following peer dependencies:
cdktn@>= 0.22.0vitest@>= 4.0
License
MIT
