I have been trying to get the the mocha testing to work for Serverless v1.0 and have achieved it with moderate success. also the answer from flo helped a lot.
But i am not able send the path variable to it to the test method which is defined like this…
events:
- http:
path: sendsms/{phone}
method: get
How do i pass this to my test function, my test function runs with this error
TypeError: Cannot read property 'phone' of undefined
can someone help me in this for how to pass the phone to my test function.
As @contractorwolf already alluded to this variable will not be available by default in your local mocha tests if you run a function. Serverless is not automatically integrated with Mocha tests, so you have to mock those parameters yourself in local unit tests. You should be able to invoke it after its deployed though through serverless invoke
Any news on this? I want to do the same thing in my local, the only way I know to test my functions in my local environment is calling: serverless invoke local --function function_name. In the case that @medampudi mentioned how can I pass the {phone} in command line?
When you call the endpoint form the browser you can get the parameter, (in Nodejs) like this: event.pathParameters.parID
if you want to invoke by command line in your local environment and in order to keep the same source code, the way I found is like this: serverless invoke local --function function_name --path lib/data.json
where data.json has the pathParameters parameter value, e.g: {“pathParameters”:{“parID”:“123456”}}
I know is not a very elegant solution but it works, I hope this can help.