-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import { util } from '@aws-appsync/utils' is empty object #363
Comments
Stumbled upon this on r/aws by u/seedsseedsseeds also:
They had to mock the library themselves. What is the correct solution? |
I am having this issue trying to |
Hi. See details here: https://docs.aws.amazon.com/appsync/latest/devguide/test-resolvers.html |
Hi @onlybakam , thanks for your reply. Does that mean that the best solution is to (1) mock the functions if you need to test locally and (2) use For anyone stumbling on this, you can mock the required functions in Vitest using: // mock the required functioins from @aws-appsync/utils
vi.mock('@aws-appsync/utils', () => {
const originalAppSyncUtils = require('@aws-appsync/utils');
const { marshall } = require('@aws-sdk/util-dynamodb');
const { v4 } = require('uuid');
return {
...originalAppSyncUtils,
util: {
error: (msg: string | undefined) => { throw new Error(msg) },
autoId: () => v4(),
dynamodb: {
toMapValues: (val: any) => { return marshall(val); }
},
time: {
nowEpochSeconds: () => Math.floor(Date.now() / 1000),
nowISO8601: () => new Date().toISOString()
}
}
}
}); and you can use //...
describe('Online/AWS Runtime Resolver Test', async () => {
const client = new AppSync({ region: 'us-east-1' });
const runtime = { name: 'APPSYNC_JS', runtimeVersion: '1.0.0' };
const __dirname = path.resolve('amplify/data');
test('Create todo resolver', async () => {
const createTodoInput = {
"content": `Test subject ${uuidV4()}`
};
const code = fs.readFileSync(__dirname + '/features/todo/todo.create.resolver.js', 'utf8')
const contextJSON = createTestContextForEvaluate<{ input: typeof createTodoInput }>({ input: createTodoInput });
const response = await client.evaluateCode({
code,
context: JSON.stringify(contextJSON),
runtime,
function: 'request'
}).promise();
const result = JSON.parse(response.evaluationResult!);
expect(result?.key?.id?.S).toBeTypeOf('string');
expect(result?.attributeValues?.content?.S).toEqual(contextJSON.arguments.input.content);
});
});
//... I've put a full example here https://github.com/naedx/amplify-playground/tree/dev/projects/amplify-appsync-vitest |
I'm having some trouble using the
@aws-appsync/utils
(v1.8.0) in my Vitest tests. Given the following test scriptutil
is equal to{}
;Importing
Context
from@aws-appsync/utils
works as expected.I've placed a full, self contained example here.The text was updated successfully, but these errors were encountered: