Current Situation:
- I have a file that has 11 different
NodejsFunction
s. I am looking to write assertions with CDK Template. - I have code that checks the whole stack, and says "Is there a Lambda handler?":
template.hasResourceProperties("AWS::Lambda::Function", { Handler: "index.handler" }))
Requirement:
How do I ensure each NodejsFunction
function has Handler: "index.handler"
? Can I narrow down to specific public readonly lambdaExample: NodejsFunction
from the stack, or map over the services in the stack?
Current Test:
import { Template } from "aws-cdk-lib/assertions";
import { createStacks } from "../../bin/template";
describe.only("lambdaStack", () => {
let allStacks, template: Template;
beforeAll(async () => {
allStacks= await createStacks(true);
template = Template.fromStack(allStacks.lambdaStack);
});
it("should have Handler = 'handler'", () => template.hasResourceProperties("AWS::Lambda::Function", { Handler: "index.handler" }));
});
CodePudding user response:
To assert every resource has the desired property, use allResourceProperties:
template.allResourcesProperties("AWS::Lambda::Function", {
Handler: "index.handler",
});
To assert a given resource (by Logical ID) has the desired property, filter and assert:
expect(
Object.entries(template.findResources("AWS::Lambda::Function")).filter(
([k, v]) =>
k.match(/^LambdaExample[A-F0-9]{8}$/) &&
v["Properties"]?.["Handler"] === "index.handler"
)
).toHaveLength(1);