Categories
SAP Blogs

CAP: Unit Testing using Mocha and Chai

CAP: Unit Testing using Mocha and Chai

“A good developer will thoroughly unit test his code, while a lazy good developer will automate his unit tests”

Writing test scripts for unit testing automation is essential aspect for development team that follows Agile and DevOps methodology. Imagine an existing program needs to be enhanced with a new feature, a developer implements the new feature and unit test the changes, however, the developer didn’t unit test existing business scenarios, or didn’t test completely all scenarios because it’s just too many. Unknowingly, the latest changes introduced some bugs to existing features and it was detected only at the later stage of development lifecycle — and worst case, left undetected until the code reached the production system. And this case happens all the time.

So now if the program already have unit tests in place and there’s a process to execute it automatically, before deployment to a server environment, the developer is notified immediately of the error and is able to react right away and fix the problem.

In this blog, I will talk about setting up Mocha and Chai, a popular testing framework, in the context of SAP Cloud Application Programming Model.

Prerequisites

  • SAP Business Application Studio / Visual Studio Code
  • SAP Cloud Platform Account
  • Preparation

    For this demo, I will be using the resulting project from my previous blog – Consume External Service – Part 2.

    The working project can be found in this GitHub repository — Sample CAP Project.

    If you have followed through the steps for testing the application in my previous blog, then you already know how to configure the package.json for testing with mock and real data.

    So far, the way I’ve shown it is by nullifying the credentials property by adding double dash in front of it.

    By right, you don’t want to manually change your configuration just because you want to switch your testing method from mock to real data or vice versa. What you would prefer is to have a fixed configuration and it will depend on how you execute your application that it will use the appropriate configuration.

    In order to achieve this, we need to update the configuration just like the one below:

    1. Testing using mock data
    2. Testing using real data direct from the external service
    3. This is the configuration that is meant to run in SCP Cloud Foundry
    Setting up Mocha and Chai test frameworks

    By now, we have properly configured our package.json to be able to call our mock data for automated unit testing. The next relevant thing to do is to start setting up Mocha and Chai frameworks.
    1. From the terminal, run the following command:

    2. Add a test npm script in the package.json that will be used for executing mocha test scripts.

    3. Now that we have an npm script to run for test execution, next step is to create the server of the OData service. Create a new folder called tests in the root folder. Then create a file called server.js inside this new folder. Finally, use the code below to bootstrap CDS and serve our application:

    4. Create a new file called test.js inside the tests folder. This will host the test scripts.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    ×
    Placement Report
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Interview Questions
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Learn More
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Sign Up Now
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Join Us
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Get started
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Buy Now
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Download Course Content
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Call Back
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Ask for Demo
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Enquiry
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Please fill form to see Video
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Schedule Your Demo
    CAP: Unit Testing using Mocha and Chai

    CAP: Unit Testing using Mocha and Chai
    ×
    Apply for scholarship