Skip to content

Instantly share code, notes, and snippets.

@SethO
Last active July 29, 2024 13:40
Show Gist options
  • Save SethO/631f013ba343d1a3ba73faa8e55269f0 to your computer and use it in GitHub Desktop.
Save SethO/631f013ba343d1a3ba73faa8e55269f0 to your computer and use it in GitHub Desktop.
Code Examples for OwnerShip Matter's Post "Testing AWS AppSync JavaScript Resolvers"
import {
AppSyncClient,
EvaluateCodeCommand,
} from '@aws-sdk/client-appsync';
// ARRANGE
const appSync = new AppSyncClient({ region: 'us-west-2' });
const filePath = './src/resolvers/Query.getProduct';
const context = { ... };
const input = {
runtime: { name: 'APPSYNC_JS', runtimeVersion: '1.0.0'},
code: fs.readFileSync(filePath, {encoding: 'utf8'}),
context,
function: 'response',
};
const command = new EvaluateCodeCommand(input);
// ACT
const result = await appSync.send(command);
// ASSERT
expect(result).toBeDefined();
expect(result.errors).toBeUndefined();
expect(result.evaluationResult).toBeDefined();
// (more expectations)
import esbuild from 'esbuild';
// ARRANGE
const appSync = new AppSyncClient({ region: 'us-west-2' });
const filePath = './src/resolvers/Query.getProduct';
const fileUnderTest = esbuild.buildSync({
entryPoints: [fileUnderTestPath],
external: ['@aws-appsync/utils'],
bundle: true,
write: false,
platform: 'node',
target: 'esnext',
format: 'esm',
sourcesContent: false,
}).outputFiles[0].text;
const context = { ... };
const input = {
runtime: { name: 'APPSYNC_JS', runtimeVersion: '1.0.0'},
code: fileUnderTest,
context,
function: 'response',
};
const command = new EvaluateCodeCommand(input);
// ACT
const result = await appSync.send(command);
// ASSERT
expect(result).toBeDefined();
expect(result.errors).toBeUndefined();
expect(result.evaluationResult).toBeDefined();
// (more expectations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment