Skip to content

Commit

Permalink
feat(integ-tests): add serializedJson on match utility
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbarthelet committed Dec 6, 2022
1 parent df163ec commit b7cace1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/@aws-cdk/integ-tests/README.md
Expand Up @@ -349,13 +349,16 @@ message.assertAtPath('Messages.0.Body', ExpectedResult.objectLike({
#### Match

`integ-tests` also provides a `Match` utility similar to the `@aws-cdk/assertions` module. `Match`
can be used to construct the `ExpectedResult`.
can be used to construct the `ExpectedResult`. While the utility is similar, only a subset of methods are currently available on the `Match` utility of this module: `arrayWith`, `objectLike`, `stringLikeRegexp` and `serializedJson`.

```ts
declare const message: AwsApiCall;

message.expect(ExpectedResult.objectLike({
Messages: Match.arrayWith([
{
Payload: Match.serializedJson({ key: 'value' }),
},
{
Body: {
Values: Match.arrayWith([{ Asdf: 3 }]),
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/integ-tests/lib/assertions/match.ts
Expand Up @@ -27,4 +27,12 @@ export abstract class Match {
public static stringLikeRegexp(pattern: string): { [key: string]: string } {
return { $StringLike: pattern };
}

/**
* Matches any string-encoded JSON and applies the specified pattern after parsing it.
* @param pattern the pattern to match after parsing the encoded JSON.
*/
public static serializedJson(pattern: { [key: string]: any }): { [key: string]: { [key: string]: any } } {
return { $SerializedJson: pattern };
}
}
Expand Up @@ -60,6 +60,7 @@ class MatchCreator {
* Messages: [{
* Body: Match.objectLike({
* Elements: Match.arrayWith([{ Asdf: 3 }]),
* Payload: Match.serializedJson({ key: 'value' }),
* }),
* }],
* });
Expand All @@ -73,6 +74,9 @@ class MatchCreator {
* Elements: {
* $ArrayWith: [{ Asdf: 3 }],
* },
* Payload: {
* $SerializedJson: { key: 'value' }
* }
* },
* },
* }],
Expand All @@ -89,6 +93,9 @@ class MatchCreator {
* Elements: {
* $ArrayWith: [{ Asdf: 3 }],
* },
* Payload: {
* $SerializedJson: { key: 'value' }
* }
* },
* },
* }
Expand All @@ -97,6 +104,7 @@ class MatchCreator {
* {
* Body: Match.objectLike({
* Elements: Match.arrayWith([{ Asdf: 3 }]),
* Payload: Match.serializedJson({ key: 'value' }),
* }),
* }
*/
Expand All @@ -111,6 +119,8 @@ class MatchCreator {
return Match.objectLike(v[nested]);
case '$StringLike':
return Match.stringLikeRegexp(v[nested]);
case '$SerializedJson':
return Match.serializedJson(v[nested]);
default:
return v;
}
Expand Down
Expand Up @@ -188,6 +188,56 @@ describe('AssertionHandler', () => {
});
});

describe('serializedJson', () => {
test('pass', async () => {
// GIVEN
const handler = assertionHandler() as any;
const request: AssertionRequest = {
actual: {
Payload: JSON.stringify({
Key: 'value',
Elements: [{ Asdf: 3 }, { Asdf: 4 }],
}),
},
expected: ExpectedResult.objectLike({
Payload: Match.serializedJson({
Key: 'value',
Elements: Match.arrayWith([{ Asdf: 3 }]),
}),
}).result,
};

// WHEN
const response: AssertionResult = await handler.processEvent(request);

// THEN
expect(response.assertion).toEqual('{"status":"success"}');
});

test('fail', async () => {
// GIVEN
const handler = assertionHandler() as any;
const request: AssertionRequest = {
actual: {
Payload: JSON.stringify({ stringParam: 'foo' }),
},
expected: ExpectedResult.objectLike({
Payload: Match.serializedJson({ stringParam: 'bar' }),
}).result,
};

// WHEN
const response: AssertionResult = await handler.processEvent(request);

// THEN
expect(JSON.parse(response.assertion)).toEqual({
status: 'fail',
message: 'Expected bar but received foo at /Payload(serializedJson)/stringParam (using serializedJson matcher)\n' +
'{\n \"Payload\": \"{\\\"stringParam\\\":\\\"foo\\\"}\"\n}',
});
});
});

describe('not using Match', () => {
test('pass', async () => {
// GIVEN
Expand Down

0 comments on commit b7cace1

Please sign in to comment.