Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AppSync): Add JS resolver helper & matchers #79

Merged
merged 9 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 30 additions & 39 deletions doc/matchers/appsync.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,43 @@
# AppSync

A collection of matchers to test mapping templates.
A collection of matchers to test AWS AppSync mapping templates and JS resolvers.

Use the `appSyncMappingTemplate` helper function with mapping template matchers.
## Helper Functions

- `template`: A string representing the mapping template
### `appSyncResolver(input: AppSyncResolverInput)`

Use the `appSyncResolver` helper function to test JS resolvers.

- `code`: The path to a file containing an `APPSYNC_JS` resolver code. The path can either be absolute, or relative to the working directory (`process.cwd()`).
- `function`: The function to test. Must be `request` or `response`.
- `context`: The [context object](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference-js.html) to be passed to the function

### `appSyncMappingTemplate(input: AppSyncMappingTemplateInput)`

Use the `appSyncMappingTemplate` helper function to test VTL mapping templates.

- `template`: The path to a file containing a mapping template. The path can either be absolute, or relative to the working directory (`process.cwd()`).
- `context`: The [context object](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html#accessing-the-context) to be injected into the template

### `toEvaluateTo<E>(expected: E)`
## Matchers

Asserts that a mapping template evaluates to a given string or object for a given context.
### `toEvaluateTo<E>(expected: E)`

If you pass an object as `value`, the matcher will try to parse the generated template into a javascript object before comparing the values.
Asserts that a mapping template or resolver evaluates to a given object for a given context.

```typescript
// matching as a string
await expect(
appSyncMappingTemplate({
template: fs.readFileSync('tempalte.vtl', { encoding: 'utf8' }),
appSyncResolver({
code: __dirname + '/resolver.js',
function: 'request',
context: {
arguments: {
id: '123',
},
},
}),
).toEvaluateTo(`
{
"version" : "2017-02-28",
"operation" : "GetItem",
"key" : {
"pk" : {"S":"123"}
}
}
`);
```
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I removed support for string predicates.
in AppSync, it should always be a JSON anyway.
if the evaluated mapping template is not a valid js, it will throw an error.


```typescript
// matching as an object also works as long as the mapping template evaluates to a valid JSON
// otherwise, an error will be thrown
await expect(
appSyncMappingTemplate({
template: fs.readFileSync('tempalte.vtl', { encoding: 'utf8' }),
context: {
arguments: {
id: '123',
},
},
}),
).toEvaluateTo<DynamoDBGetItem>({
version: '2017-02-28',
).toEvaluateTo<DynamoDBGetItemRequest>({
operation: 'GetItem',
key: {
pk: { S: '123' },
Expand Down Expand Up @@ -88,12 +77,14 @@ await expect(
},
}),
).toEvaluateToInlineSnapshot(`
{
"version" : "2017-02-28",
"operation" : "GetItem",
"key" : {
"pk" : {"S":"123"}
}
Object {
"key": Object {
"pk": Object {
"S": "789",
},
},
"operation": "GetItem",
"version": "2017-02-28",
}
`);
```
15 changes: 12 additions & 3 deletions examples/__tests__/__snapshots__/appSync.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Mapping Template should evaluate a template snapshot as object 1`] = `
exports[`JS resolvers should evaluate a js resolver with snapshot 1`] = `
Object {
"key": Object {
"id": Object {
"S": "123",
},
},
"operation": "GetItem",
}
`;

exports[`Mapping Template should evaluate a template snapshot 1`] = `
Object {
"key": Object {
"pk": Object {
Expand All @@ -11,5 +22,3 @@ Object {
"version": "2017-02-28",
}
`;

exports[`Mapping Template should evaluate a template snapshot as string 1`] = `"hello 456"`;
120 changes: 67 additions & 53 deletions examples/__tests__/appSync.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { appSyncMappingTemplate } from 'sls-jest';
import { DynamoDBGetItemRequest } from '@aws-appsync/utils';
import { appSyncMappingTemplate, appSyncResolver } from 'sls-jest';

type DynamoDBGetItem = {
version: string;
Expand All @@ -8,44 +9,10 @@ type DynamoDBGetItem = {
};
};

const template = `
#set($id=$ctx.args.id)
{
"version" : "2017-02-28",
"operation" : "GetItem",
"key" : {
"pk" : $util.dynamodb.toDynamoDBJson($id)
}
}
`;
const template = __dirname + '/assets/mapping-template.vtl';

describe('Mapping Template', () => {
it('should evaluate a template string', async () => {
const expected = `
{
"version" : "2017-02-28",
"operation" : "GetItem",
"key" : {
"pk" : {"S":"123"}
}
}
`;

// test that the template evaluates to the expected value as a string
await expect(
appSyncMappingTemplate({
template,
context: {
arguments: {
id: '123',
},
},
}),
).toEvaluateTo(expected);
});

it('should evaluate a template object', async () => {
// test that the template evaluates to the expected value as an object
it('should evaluate a template', async () => {
await expect(
appSyncMappingTemplate({
template,
Expand All @@ -64,9 +31,7 @@ describe('Mapping Template', () => {
});
});

it('should evaluate a template snapshot as object', async () => {
// test that the template evaluates to the expected snapshot
// if the snapshot evaluates to an object, it is parsed before being saved
it('should evaluate a template snapshot', async () => {
await expect(
appSyncMappingTemplate({
template,
Expand All @@ -79,9 +44,7 @@ describe('Mapping Template', () => {
).toEvaluateToSnapshot();
});

it('should evaluate a template inline snapshot as object', async () => {
// test that the template evaluates to the expected inline snapshot
// if the snapshot evaluates to an object, it is parsed before being saved
it('should evaluate a template inline snapshot', async () => {
await expect(
appSyncMappingTemplate({
template,
Expand All @@ -103,32 +66,83 @@ describe('Mapping Template', () => {
}
`);
});
});

it('should evaluate a template snapshot as string', async () => {
// test that the template evaluates to the expected snapshot
describe('JS resolvers', () => {
const code = __dirname + '/assets/js-resolver.js';

it('should evaluate a js resolver', async () => {
await expect(
appSyncMappingTemplate({
template: 'hello ${ctx.args.id}',
appSyncResolver({
code,
function: 'request',
context: {
arguments: {
id: '456',
id: '123',
},
},
}),
).toEvaluateTo<DynamoDBGetItemRequest>({
operation: 'GetItem',
key: {
id: { S: '123' },
},
});

await expect(
appSyncResolver({
code,
function: 'response',
context: {
arguments: {
id: '123',
},
result: {
id: '123',
name: 'test',
},
},
}),
).toEvaluateTo({
id: '123',
name: 'test',
});
});

it('should evaluate a js resolver with snapshot', async () => {
await expect(
appSyncResolver({
code,
function: 'request',
context: {
arguments: {
id: '123',
},
},
}),
).toEvaluateToSnapshot();
});

it('should evaluate a template inline snapshot', async () => {
// test that the template evaluates to the expected inline snapshot
it('should evaluate a js resolver inline snapshot', async () => {
await expect(
appSyncMappingTemplate({
template: 'hello ${ctx.args.id}',
appSyncResolver({
code,
function: 'request',
context: {
arguments: {
id: '789',
},
},
}),
).toEvaluateToInlineSnapshot(`"hello 789"`);
).toEvaluateToInlineSnapshot(`
Object {
"key": Object {
"id": Object {
"S": "789",
},
},
"operation": "GetItem",
}
`);
});
});
13 changes: 13 additions & 0 deletions examples/__tests__/assets/js-resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { get } from '@aws-appsync/utils/dynamodb';

export const request = (ctx) => {
return get({
key: {
id: ctx.args.id,
},
});
};

export const response = (ctx) => {
return ctx.result;
};
8 changes: 8 additions & 0 deletions examples/__tests__/assets/mapping-template.vtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#set($id=$ctx.args.id)
{
"version" : "2017-02-28",
"operation" : "GetItem",
"key" : {
"pk" : $util.dynamodb.toDynamoDBJson($id)
}
}
Loading
Loading