Skip to content

Commit 122ec82

Browse files
committedSep 14, 2023
Extract test helpers
1 parent 2ef1175 commit 122ec82

File tree

3 files changed

+56
-49
lines changed

3 files changed

+56
-49
lines changed
 

‎__tests__/helpers.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";;
2+
3+
const { AppSyncClient, EvaluateCodeCommand } = require("@aws-sdk/client-appsync");
4+
const { util } = require("..");
5+
6+
let client = null;
7+
8+
const runOnAWS = async (s) => {
9+
if (!client) {
10+
client = new AppSyncClient();
11+
}
12+
13+
const code = `
14+
import { util } from '@aws-appsync/utils';
15+
16+
export function request(ctx) {
17+
return ${s};
18+
}
19+
20+
export function response(ctx) {
21+
}
22+
`;
23+
24+
const command = new EvaluateCodeCommand({
25+
code,
26+
context: "{}",
27+
function: "request",
28+
runtime: {
29+
name: "APPSYNC_JS",
30+
runtimeVersion: "1.0.0",
31+
},
32+
});
33+
34+
const result = await client.send(command);
35+
try {
36+
return JSON.parse(result.evaluationResult);
37+
} catch (e) {
38+
console.error("invalid json", result);
39+
}
40+
}
41+
42+
// If TEST_TARGET is AWS_CLOUD then run the check against AWS. Otherwise, run locally.
43+
const checkValid = async (s) => {
44+
let result;
45+
if (process.env.TEST_TARGET === "AWS_CLOUD") {
46+
result = await runOnAWS(s);
47+
} else {
48+
result = eval(s);
49+
}
50+
expect(result).toMatchSnapshot();
51+
}
52+
53+
54+
module.exports = { checkValid };

‎__tests__/index.test.js

+1-49
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
* Helpers definition from https://docs.aws.amazon.com/appsync/latest/devguide/dynamodb-helpers-in-util-dynamodb-js.html
33
*/
44

5-
const { util } = require("..");
6-
const { AppSyncClient, EvaluateCodeCommand } = require("@aws-sdk/client-appsync");
5+
const { checkValid } = require("./helpers.js");
76

8-
let client = null;
97

108
describe("dynamodb helpers", () => {
119
describe.skip("toDynamoDB", () => {
@@ -97,49 +95,3 @@ describe("dynamodb helpers", () => {
9795
);
9896
});
9997
});
100-
101-
const runOnAWS = async (s) => {
102-
if (!client) {
103-
client = new AppSyncClient();
104-
}
105-
106-
const code = `
107-
import { util } from '@aws-appsync/utils';
108-
109-
export function request(ctx) {
110-
return ${s};
111-
}
112-
113-
export function response(ctx) {
114-
}
115-
`;
116-
117-
const command = new EvaluateCodeCommand({
118-
code,
119-
context: "{}",
120-
function: "request",
121-
runtime: {
122-
name: "APPSYNC_JS",
123-
runtimeVersion: "1.0.0",
124-
},
125-
});
126-
127-
const result = await client.send(command);
128-
try {
129-
return JSON.parse(result.evaluationResult);
130-
} catch (e) {
131-
console.error("invalid json", result);
132-
}
133-
}
134-
135-
// If TEST_TARGET is AWS_CLOUD then run the check against AWS. Otherwise, run locally.
136-
const checkValid = async (s) => {
137-
let result;
138-
if (process.env.TEST_TARGET === "AWS_CLOUD") {
139-
result = await runOnAWS(s);
140-
} else {
141-
result = eval(s);
142-
}
143-
expect(result).toMatchSnapshot();
144-
}
145-

‎jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const config = {
22
testEnvironment: 'node',
3+
testMatch: ["**/__tests__/*test.js"],
34
};
45

56
export default config;

0 commit comments

Comments
 (0)