-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjest-utils.ts
37 lines (31 loc) · 1.09 KB
/
jest-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import createAppSync from '@conduitvc/appsync-emulator-serverless/jest'
import { vtl } from '@conduitvc/appsync-emulator-serverless/vtl'
import { create } from '@conduitvc/appsync-emulator-serverless/util'
import { readFileSync } from 'fs'
import { resolve } from 'path'
import json5 from 'json5'
;(global as any).fetch = require('node-fetch')
const appsyncCreated = createAppSync()
export const appsyncClient = appsyncCreated
export function loadVTL(filename: string): string {
filename = filename.replace('~', 'mapping-templates/')
const fullPath = resolve(__dirname, filename)
return readFileSync(fullPath, 'utf8')
}
export interface GraphQLResponse {
data: object | string
errors: Array<GraphQLResponseError>
}
export interface GraphQLResponseError {
message: string
}
export function renderVTL(vm: string, context: object = {}, macros: object = {}): GraphQLResponse {
let errors = []
let parsedVTL = vtl(vm, { ...context, util: create(errors) }, macros).trim()
try {
parsedVTL = json5.parse(parsedVTL)
} catch (e) {
console.warn(e)
}
return { data: parsedVTL, errors }
}