Testing webtask.io programming models

Webtask.io is a serverless platform where u can run code without any system administration. It has three build-in programming models and a fourth for custom models. This enables that logic can be organized in simple functions that do one thing and have the same output for the same arguments always.
Organizing the code base like this also makes it easy to test these function with a test runner like tape

Testing the Simple function model

This model is straightforward it returns a function that takes a callback as argument with the following signature:

cb(error, result)

Knowing this we could use a simple spy to test the error or result.
And putting a simple test case on the example provided from webtask.io site it could make the function testable the following way:

And then running it with tape it would produce the following output

$ ./node_modules/.bin/tape simple-function.test.js 
TAP version 13
# Simple function test cases
ok 1 Assert expected: null matches actual: null
ok 2 Assert expected: {"i_am":"done "} matches actual: {"i_am":"done "}
# tests 2
# pass 2
# ok

Testing the Function with context model

Building on the previous model this function takes two arguments context and a callback that have the same signature as the simple function callback.
The context is a Javascript object. For making this function testable we could create a mock object for the context and apply a spy on the callback as show in the example below:

Again we can run it with tape to see the output as following

$ ./node_modules/.bin/tape function-with-context.test.js 
TAP version 13
# Function with context test cases
ok 1 Assert expected: null matches actual: null
ok 2 Assert expected: {"hello_you":"Boris"} matches actual: {"hello_you":"Boris"}
ok 3 Assert expected: null matches actual: null
ok 4 Assert expected: {"hello_you":"Anonymous"} matches actual: {"hello_you":"Anonymous"}
# tests 4
# pass 4
# ok

Testing the Full HTTP control model

This model has three arguments the same context as previous model but also takes a request and response al of which are Javascript objects.
To make this model testable we could mock the Javascript objects and their functionalities as shown below:

And tape producing the following output

$ ./node_modules/.bin/tape full-http-control.test.js 
TAP version 13
# Full http control test cases
ok 1 Assert expected: 200 matches actual: 200
ok 2 Assert expected: {"Content-Type":"text/html "} matches actual: {"Content-Type":"text/html "}
ok 3 Assert expected: <h1>Hello, world!</h1> matches actual: <h1>Hello, world!</h1>
# tests 3
# pass 3
# ok


Testing your code may seem a bit of a overkill in these examples but when u expand this to a more realistic logic the time spend adding tests adds to creating more reliable code and with these simple programming models and tools such as tape making it almost effortless and does not make you feel your loosing time when writing tests.


Repository with the full code base can be found here


Found any technical error? please submit it to me and I will correct the page as soon as possible also note I am not a English native so any grammatical errors please notify me and I will correct them.


One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.