Automated Unit Testing of Node Fn Functions using Jest

Lucas Jellema
6 min readAug 8, 2020

Crucial pieces of functionality in cloud native applications is implemented in Functions. In the case of Oracle Cloud Infrastructure specifically, the Functions framework is typically Project Fn and the implementation language of choice is … up to the DevOps team. Popular languages for implementing Functions include Go, Python, Java and JavaScript.

Automated code level tests are essential to describe and prove the behavior of the software and to allow rapid and safe refactoring of the code. These tests should be executable in a fully automated way, run against the code as it is without special provisions in the code for running the test, without requiring deployment of the code to a central environment and focus on the core: not the external dependencies used by the code at runtime and not the frameworks that wrap the code at runtime. In the case of Fn Functions, automated code level unit tests should not include the Fn libraries — but only deal with the code the developers in the DevOps team have written to implement the required functionality in the code.

This article describes what we can do with regard to these tests for Javascript based functions. It shows how we use Jest for writing the test and for running the test and for mocking the FDK (Fn framework). This last step means that we can test the code without executing any part of the Fn libraries.

The steps — that I will demonstrate in detail below are:

  • Prepare environment for Fn (install Fn Server and Fn Client)
  • Install Jest (using NPM)
  • Create Function with JavaScript runtime
  • Configure Jest in function’s package.json
  • Create a Mock FDK
  • Create a Test module func_test.js (require the function to test, require mock fdk, optionally: define setup steps to prepare for tests, define tests : description, call and expectation)
  • Run tests through npm to get a report fo the passed and failed tests(no interaction with the FDK libraries takes place when the tests are executed)

Demonstration in simplest possible case: Hello World

In an environment set up for Fn and Node development — with Fn Server and Client and with Node and NPM installed — I will demonstrate the installation and configuration of Jest and the creation of the hello function and…

--

--

Lucas Jellema

Lucas Jellema is CTO and IT architect at Conclusion, The Netherlands. He is Oracle ACE Director, one time JavaOne Rockstar and programmer