Cloud Code Unit Tests
This section will allow you to check the operation and testing of your functions locally using the library parse-server-test-runner.
- 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.
- Back4App Command Line Configured with the project.
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.
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
In your terminal, initially, we’ll create a directory and configure your test App (package.json) first, using the following command:
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:
Now, let’s create the file (index.js) using the command below:
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:
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.
Finally, you can work with your terminal:
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.
Of course, as a test parameter, we will create a backend to control the information of a company’s 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.
- Position
- Department
- WorkShift
- userId (Pointer)
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.
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:
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.
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.
Let’s install Jasmine globally and use, with the commands below:
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.
Now, we will configure the files created above with the codes shown below:
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):
And now, you’ll check that we approach the structure described in these previous steps :)
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.
After the successful installation, you’re able to check your unit test locally with the command described and receive the result, such as:
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.