-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): unit tests for cfn utils provider handler (#27759)
Adds unit test coverage for cfn utils provider handler. Closes #27729. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
d449cfd
commit b02752b
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/aws-cdk-lib/core/test/private/cfn-utils-provider-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { CfnUtilsResourceType } from '../../lib/private/cfn-utils-provider/consts'; | ||
import { handler } from '../../lib/private/cfn-utils-provider/index'; | ||
|
||
test('parses value as JSON', async () => { | ||
// GIVEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceEvent> = { | ||
ResourceType: CfnUtilsResourceType.CFN_JSON, | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
RepositoryName: 'MyRepo', | ||
Value: JSON.stringify({ | ||
test: 'Random', | ||
}), | ||
}, | ||
}; | ||
|
||
// WHEN | ||
const response = await invokeHandler(event); | ||
|
||
// THEN | ||
expect(response).toEqual({ | ||
Data: { | ||
Value: { | ||
test: 'Random', | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('format JSON value as string', async () => { | ||
// GIVEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceEvent> = { | ||
ResourceType: CfnUtilsResourceType.CFN_JSON_STRINGIFY, | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
RepositoryName: 'MyRepo', | ||
Value: { | ||
test: 'Random', | ||
}, | ||
}, | ||
}; | ||
|
||
// WHEN | ||
const response = await invokeHandler(event); | ||
|
||
// THEN | ||
expect(response).toEqual({ | ||
Data: { | ||
Value: JSON.stringify({ | ||
test: 'Random', | ||
}), | ||
}, | ||
}); | ||
}); | ||
|
||
test('fails if wrong resource type', async () => { | ||
// GIVEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceEvent> = { | ||
ResourceType: 'Create', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
RepositoryName: 'MyRepo', | ||
}, | ||
}; | ||
|
||
// WHEN | ||
await expect(() => invokeHandler(event)).rejects.toThrow(/unexpected resource type "Create"/); | ||
}); | ||
|
||
// helper function to get around TypeScript expecting a complete event object, | ||
// even though our tests only need some of the fields | ||
async function invokeHandler(event: Partial<AWSLambda.CloudFormationCustomResourceEvent>) { | ||
return handler(event as AWSLambda.CloudFormationCustomResourceEvent); | ||
} |