Passing Query parameters to your serverless functions


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…

      - 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.

Are you testing using invoke on the command line or with the browser?

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

Hey guys!

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?

I checked in

but it seems that there is no any --parameter option or something like that.

If I deploy to the aws and I test with the browser it works but I want to have a way to test it in my local.


I will reply myself :slight_smile:

The way I found is using --path option.

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.



Thanks @achi2211, this works!!

If you want to avoid creating the data.json file you can use the --data param. Like this:

sls invoke local --function function_name --data '{“pathParameters”:{“parID”:“123456”}}'