I have been following https://docs.aws.amazon.com/appsync/latest/devguide/test-debug-resolvers-js.html to write unit tests for my AppSync functions and resolvers. Everything works well until I try to test the error states. My code is as follows:
code.js
import { util } from '@aws-appsync/utils'
/**
* Request a single item with `id` from the attached DynamoDB table datasource
* @param ctx the context object holds contextual information about the function invocation.
*/
export function request(ctx) {
const { args: { id } } = ctx
return {
operation: 'GetItem',
key: util.dynamodb.toMapValues({ id })
}
}
/**
* Returns the result directly
* @param ctx the context object holds contextual information about the function invocation.
*/
export function response(ctx) {
const { error, result } = ctx;
if (error) {
return util.appendError(error.message, error.type, result);
}
return result;
}
context.json
{
"arguments": {
"id": "1234",
"description": "test"
},
"source": {},
"result": {
"id": "1234"
},
"request": {},
"error": {
"type": "ERROR",
"message": "Error Happened"
},
"prev": {}
}
code.test.js
const AWS = require('aws-sdk')
const fs = require('fs')
const client = new AWS.AppSync({ region: 'us-east-2' })
const runtime = {name:'APPSYNC_JS',runtimeVersion:'1.0.0')
test('Error handling', async () => {
const code = fs.readFileSync('./code.js', 'utf8')
const context = fs.readFileSync('./context.json', 'utf8')
const contextJSON = JSON.parse(context)
const response = await client.evaluateCode({ code, context, runtime, function: 'request' }).promise()
const result = JSON.parse(response.evaluationResult)
expect(result).not.toEqual("")
})
I'm expecting the response to come back with the result and errors block but instead get an empty string as the "evaluated response function", I think this is due to appendError
returning void
but I'm not entirely sure how to fix it so it returns the result and error blocks.