A Slightly Improved Way to Test OpenWhisk Functions Locally

Lars Trieloff
May 7, 2017 · 2 min read

I’m playing with the OpenWhisk node.js runtime right now, and as I’m still in trial-and-error mode, I’m trying to run my code locally, before I deploy it to Adobe I/O Runtime.

In doing so, I found Raymond’s test.js script incredibly helpful, as it allows me to test my code locally, pass parameters as command line arguments, and it even resolves promises (this is how OpenWhisk handles asynchronous responses).

There were a couple of additional things I needed in the script, in particular:

  • help me remember the command line arguments, as I’m forgetful
  • support plain (non-Promise) responses, as my main code is still very simple
  • accept JSON objects on stdin, so I can pass nested objects

I’ve modified and extended Raymond’s script, as follows:

If you want to run it, you can either pass arguments on the command line, as in Raymond’s original:

$ node test.js ./main.js foo=bar

Or, you can pass a object through stdin, which is useful if you have a more complex input object.

$ echo '{"foo":"bar"}' | node test.js ./main.js
$ cat test.json | node test.js ./main.js

Additionally, my modified script can deal with a that does not properly use exports, this means, the line

exports.main = main;

Is entirely optional, unlike described in Raymond’s blog post.

Update

This script is now officially part of the OpenWhisk Developer Tools project.

Apache OpenWhisk

Apache OpenWhisk is a serverless cloud platform that…

Apache OpenWhisk

Apache OpenWhisk is a serverless cloud platform that executes code in response to events

Lars Trieloff

Written by

Principal at Adobe

Apache OpenWhisk

Apache OpenWhisk is a serverless cloud platform that executes code in response to events