Last active
July 29, 2024 13:40
-
-
Save SethO/631f013ba343d1a3ba73faa8e55269f0 to your computer and use it in GitHub Desktop.
Code Examples for OwnerShip Matter's Post "Testing AWS AppSync JavaScript Resolvers"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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