Skip to content

Commit 80684b5

Browse files
author
Dane Pilcher
committed
test: add test to assert js resolver is valid js
1 parent 9c6ab19 commit 80684b5

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

packages/backend-data/src/convert_js_resolvers.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import {
66
AmplifyData,
77
AmplifyDataDefinition,
88
} from '@aws-amplify/data-construct';
9-
import { resolve } from 'path';
9+
import { join, resolve } from 'path';
10+
import { tmpdir } from 'os';
1011
import { fileURLToPath } from 'url';
11-
import { convertJsResolverDefinition } from './convert_js_resolvers.js';
12+
import {
13+
convertJsResolverDefinition,
14+
defaultJsResolverCode,
15+
} from './convert_js_resolvers.js';
1216
import { a } from '@aws-amplify/data-schema';
17+
import { writeFileSync } from 'node:fs';
1318

1419
// stub schema for the AmplifyApi construct
1520
// not relevant to this test suite
@@ -28,6 +33,29 @@ const createStackAndSetContext = (): Stack => {
2833
return stack;
2934
};
3035

36+
void describe('defaultJsResolverCode', () => {
37+
void it('returns the default JS resolver code with api id and env name in valid JS', async () => {
38+
const code = defaultJsResolverCode('testApiId', 'testEnvName');
39+
assert(code.includes("ctx.stash.awsAppsyncApiId = 'testApiId';"));
40+
assert(code.includes("ctx.stash.amplifyBranchName = 'testEnvName';"));
41+
42+
const tempDir = tmpdir();
43+
const filename = join(tempDir, 'js_resolver_handler.js');
44+
writeFileSync(filename, code);
45+
46+
const resolver = await import(filename);
47+
const context = { stash: {}, prev: { result: 'result' } };
48+
assert.deepEqual(resolver.request(context), {});
49+
50+
// assert api id and env name are added to the context stash
51+
assert.deepEqual(context.stash, {
52+
awsAppsyncApiId: 'testApiId',
53+
amplifyBranchName: 'testEnvName',
54+
});
55+
assert.equal(resolver.response(context), 'result');
56+
});
57+
});
58+
3159
void describe('convertJsResolverDefinition', () => {
3260
let stack: Stack;
3361
let amplifyApi: AmplifyData;

packages/backend-data/src/convert_js_resolvers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const JS_PIPELINE_RESOLVER_HANDLER = './assets/js_resolver_handler.js';
2222
*
2323
* Add Amplify API ID and environment name to the context stash for use in the customer-provided handlers.
2424
*/
25-
const defaultJsResolverCode = (
25+
export const defaultJsResolverCode = (
2626
amplifyApiId: string,
2727
amplifyEnvironmentName: string
2828
): string => {

0 commit comments

Comments
 (0)