Correct syntax for event.json when mocking a PreSignUp triggered lambda function
See original GitHub issueWhich Category is your question related to? Mocking Lambda functions
Amplify CLI Version 4.18
What AWS Services are you utilizing? API, Auth, Functions, Storage, S3
Hi Amplify,
I’m trying to learn how to mock functions locally using Amplify, so that I can stop doing edit/push over and over, but I can’t seem to find any good docs on the subject.
Starting “simple”, I’m just trying to test a stand alone function.
My (React) app uses Auth.signUp()
like this:
const cognitoUser = await Auth.signUp({
username: username,
password: password,
attributes: {
email: email,
"custom:appleEmail": appleEmail,
"custom:googleEmail": googleEmail
}
});
The request gets intercepted and handled by a Pre sign-up lambda trigger function. And when that event arrives there it looks like this:
Output from console.log(event)
in the lambda function
{
version: '1',
region: 'us-west-2',
userPoolId: 'us-west-2_asbcd1234',
userName: 'Kimfucious',
callerContext: {
awsSdkVersion: 'aws-sdk-unknown-unknown',
clientId: 'ab41fed6r1kspmh12345678'
},
triggerSource: 'PreSignUp_SignUp',
request: {
userAttributes: {
'custom:googleEmail': 'example@gmail.com',
'custom:appleEmail': 'example@icloud.com',
email: 'example@example.com'
},
validationData: null
},
response: {
autoConfirmUser: false,
autoVerifyEmail: false,
autoVerifyPhone: false
}
}
What I don’t understand is what to put in event.json
in the src
directory of the lambda function in question.
For example, if in the lambda function I want to perform certain actions based on triggerSource
like this:
console.log("EVENT", event)
console.log("TRIGGER_SOURCE", event.triggerSource)
if (event.triggerSource === "PreSignUp_SignUp") {
// do something
} else if (event.triggerSource === "PreSignUp_ExternalProvider") {
// do something else
} else {
// do something because no event.triggerSource matches the above
}
If I use the event from the above (where triggerSource
is PreSignUp_SignUp
) as the event.json for the mock function call, my expectation is that the function would receive the event and be processed by the first if/conditional.
However, here’s what I see when running `amplify mock function functionName:
- The first console log prints out
event
, and it looks okay to me. - The second console log prints out
undefined
forevent.triggerSource
- This results in the function always processing with the final else clause of the conditional.
I believe that I have a fundamental misunderstanding here about how this is all supposed to work, like “mocking isn’t invoking”, “you should be using SAM”, or something, but, again, I can’t seem to find the right docs/examples.
Any help toward finding the right docs and/or understanding this better would be most appreciated.
Thanks 🌮 !
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Thanks for the input @alexkates
I also noticed that while @jplaza’s idea might work to parse the string when working with amplify mock, that parsing is unwanted with actually running the functions through the app while in dev.
I’m trying out amplify too and the problem you are having @kimfucious is that the event is passed as a string instead of an object as expected. I don’t know if I’m missing something too but for the meantime I’m doing this