Cloud Code Functions

Cloud Code Unit Tests

22min

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.
  • An app created at Back4App.
  • Follow the Create New App tutorial to learn how to create an app at Back4App.
  • Back4App Command Line Configured with the project.
  • Follow the Setting up Cloud Code tutorial to learn how to set up cloud code for a project.

First off, we need to talk about Unit Test!

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


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


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:

├──Back4AppProject │ ├── cloud │ │ ├── functions.js │ │ ├── main.js │ ├── public │ ├── package.json │ ├── index.js │ ├── node_modules │ ├── src │ │ ├── jasmine.js

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


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.

Parse Server 3.X
Parse Server 2.X


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.

~/Back4AppProjectsrc$ touch index.js ~/Back4AppProject$ mkdir src && cd src ~/Back4AppProject/src$ touch jasmine.js

Now, we will configure the files created above with the codes shown below:

index.js
jasmine.js


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


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.