How to Create a Unit Test through their Cloud Code Functions
Introduction
This section will allow you to check the operation and testing of your functions locally using the library parse-server-test-runner.
Prerequisites
To complete this tutorial, you will need:
A local environment with Node.js installed to apply unit tests. You can follow the Official NodeJS tutorial to successfully install Node.js at your terminal.
When developers start writing a function with different intentions in mind, a major point evident in the software community is the application of imaginary scenarios for the created code to be tested. It is necessary to perform the Unit Tests procedure as it allows you to test the code in parts, which ensures that your main code remains intact and uncompromised.
And now, how about a simple practical example?
Let’s assume that you need to write a function to show in a complete sentence, the name from the worker, the position and company. We’ll need to write the function to get the following input items:
Company
Position
Worker name
Steps to complete the simple test
In your terminal, initially, we’ll create a directory and configure your test App (package.json) first, using the following command:
$ mkdir unit-test-sample && cd unit-test-sample
$ npm init
Hint:
Using the npm init command, you’ll be able to create the package.json file. It only covers the most common items and tries to guess sensible defaults. Because of this, we’ll make available the dependencies necessary for the code to work.
The result from “package.json” will be something like the example below:
JSON
{"name":"yourfoldername","version":"1.0.0","description":"Just a unit test with using a simple function.","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":""},"author":"Your Name","license":"ISC","bugs":{"url":"{url}/issues"},"homepage":""}
Now, let’s create the file (index.js) using the command below:
~/unit-test-sample$ touch index.js
We will now insert the code below into the previously created file, and the function will insert an example that can demonstrate this test with the code:
JS
1// index.js2 module.exports=function(name, position, company){3let sentence ="Hi, "+ name +"! You are "+ position +" in "+ company +" company.";4return sentence;5};
In NodeJS, the module encapsulates the related code into a single unit of code, and when you’re using module.exports, it increases encapsulated code that can be utilized in other files.
Let’s test 0/
Finally, you can work with your terminal:
~/unit-test-sample$ node
> var moduleBack4App = require('./index.js');
undefined
> moduleBack4App("Jonathan", "Developer", "Back4App")
Hi, Jonathan! You are Developer in Back4App company.
Hint:
Using the require() function, you’re able to import and export modules, and in the case above, we are using this function to require a file inside an application.
And using Parse, can I test my function?
Of course, as a test parameter, we will create a backend to control the information of a company’s employees.
Let’s structure the classes as:
Parse.User (Reference for employees)
username, email, password (required)
We recommend you to have a detailed look at the Parse Server guide in order to get some more information about User properties.
infoEmployee
Position
Department
WorkShift
userId (Pointer)
1 - Understand our final structure
Let’s get started! We will use the Parse Server Javascript Guide as a parameter for the development of our functions. Firstly, after completing the setup using the Command Line Interface (see prereqs), we’ll understand how it will work with the final structure from the files:
Notice: When you upload the files to your Cloud Code, the Command Line Interface (See prereqs) will ignore the other files and upload only the ones that are in the public and cloud folder.
2 - Writing our function
After configuring the environment for the Command Line Interface, we’ll write the function to build the process to register the Employee and save the additional information. By refactoring the code, at the main.js file, we’ll import these functions into main, like:
JS
1//In 1cloud/main.js23var cloudFunctions =require("./functions");45/* It's necessary to insert the Parse Instance in our code,
6 because at local context not is referenced.*/78 Parse.Cloud.define("registerUser", cloudFunctions.registerUser(Parse));910 Parse.Cloud.beforeSave("infoEmployee", infoEmployee.infoEmployee(Parse));
The idea is to decouple the functions from the cloud interface so we may test them without sending HTTP requests inefficiently. This will make a lot of sense as we create the test suite.
3 - Setting up the environment to test the above code!
For our test suite, we will be using Jasmine, a highly popular JavaScript testing framework. However, our code so far is completely agnostic of our tests, so you may use whatever framework or platform you prefer.
Install the development dependencies
Let’s install Jasmine globally and use, with the commands below:
~/Back4AppProject$ sudo npm install -g jasmine
~/Back4AppProject$ jasmine
Randomized with seed 48094
Started
No specs found
Finished in 0.002 seconds
Incomplete: No specs found
4 - Configuring Parse Server Test Runner in the folder
With our methods implemented in the project Cloud folder in Back4App, we will create new files on our project on Node.js that will configure this interaction.
The last step is to configure the package.json, using the command: $ npm init in the root directory (the file below is just an example with the modules required):
JSON
1{2"name":"back4approject",3"version":"1.0.0",4"description":"Back4App guide using for reference the Parse Server Test Runner",5"main":"index.js",6"engines":{7"node":">=6"8},9"repository":{10"type":"",11"url":""12},13"keywords":[14"parse",15"parse-server",16"testing",17"tests"18],19"author":"",20"license":"ISC",21"dependencies":{22"bluebird":"^3.5.0",23"express":"latest",24"mongodb":"^2.2.30",25"mongodb-runner":"^3.5.0",26"parse":"^1.10.0",27"parse-server":"^2.5.3",28"parse-server-test-runner":"^1.0.0"29}30}
And now, you’ll check that we approach the structure described in these previous steps :)
5 - Returning to the terminal
We’ll start to configure the local testing, for this we’ll follow the command below to the code for set up programmatically for testing purposes.
~/Back4AppProject$ # in the same directory from package.json
~/Back4AppProject$ npm install
After the successful installation, you’re able to check your unit test locally with the command described and receive the result, such as:
~/Back4AppProject$ jasmine src/jasmine.js
Randomized with seed 79055
Started
✔ Downloaded MongoDB 3.6.5
[]
.
1 spec, 0 failures
Finished in 19.983 seconds
Randomized with seed 79055 (jasmine --random=true --seed=79055)
Wonderful, it’s ready!
With the guide described above, you’re able to work with the Parse Server Test Runner and your functions developed for the Cloud Code to Back4App.