Appsync VTL Tester
![downloads](https://badgen.net/npm/dt/appsync-template-tester)
Write unit tests for AWS AppSync VTL resolvers, with popular frameworks such as Jest.
Use
Example
yarn add appsync-template-tester --dev
import Parser from 'appsync-template-tester';
import { readFileSync } from 'fs';
import { join } from 'path';
const templateFilePath = join(__dirname, './pathToFile.vtl');
const template = readFileSync(templateFilePath, {
encoding: 'utf8',
});
const parser = new Parser(template);
test('Test the resolver', () => {
const context = {
result: {
id: 'testId',
},
};
const response = parser.resolve(context);
expect(response.id).toBe('testId');
});
Util helpers supported
This module supports all the provided core, map & time $util methods, and most of the dynamodb methods. The underlying methods can be seen in the Resolver Mapping Template Utility Reference
docs.
Note: The errors list is also not returned (but $util.error will throw an error).
Extensions
AWS AppSync provides extension methods via $extensions
, for example $extensions.evictFromApiCache
. It can be useful to assert that your VTL template is invoking these methods, therefore, you can provide custom extensions with your own implementations. Note that default extension methods are not provided. To read more about AWS AppSync extensions see the Extensions
docs.
const mockEvictFromApiCache = jest.fn();
const parser = new Parser(`$extensions.evictFromApiCache("Query", "users", {
"context.arguments.id": $context.arguments.id
})`);
parser.resolve({ arguments: { id: 10 } }, undefined, {
evictFromApiCache: mockEvictFromApiCache,
});
expect(mockEvictFromApiCache).toHaveBeenCalledTimes(1)
expect(mockEvictFromApiCache).toHaveBeenCalledWith("Query", "users", { "context.arguments.id": 10 })