banner image 1 banner image 2

GraphQL Automation with Database Verification

December 29, 2022
4 mins
command
blog-img 1
Saranraj S
Author

This article will show you how to use Mocha and Chai to test your GraphQL Query and connect to a database to verify the API response.

By Saranraj S — “Testing for Excellence”


GraphQL is a popular query language for interacting with APIs, and it allows clients to request only the specific data they need, rather than a fixed set of endpoints.

GraphQL Automation with Database Verification

Mocha and Chai are popular JavaScript testing tools that can be used to test a wide range of APIs, including REST APIs, GraphQL APIs, and APIs that use other protocols.

What is Mocha?

Mocha is a JavaScript testing framework that runs on Node.js. It provides a simple and flexible interface for testing both synchronous and asynchronous code, as well as several features to help you organize and run your tests.

What is Chai?

Chai is an assertion library that can be used with Mocha (or any other testing framework). It provides many functions that allow you to make assertions about the values returned by your code. Chai can be used with many different assertion styles, including “should”, “expect”, and “assert”.

Setting Up Mocha and Chai

To get started with Mocha and Chai, you will need to have Node.js and npm (the Node.js package manager) installed on your machine. Once you have these tools installed, you can use npm to install Mocha and Chai as follows:

npm install --save-dev mocha chai

This will install Mocha and Chai as development dependencies in your project.

Writing Tests with Mocha and Chai

Now, you can start writing tests. To do this, you will need to create a test file in your project. This file should contain your test cases, which are defined using the describe and it functions provided by Mocha.

Here is an example of a simple test case that uses Mocha and Chai to test an API endpoint that returns a list of students:

https://gist.github.com/saranraj-s/0d96796de256cec9f0f2a2b967fa31a9#file-sample

In this example, we use the Chai HTTP plugin to send a GET request to the /api/students endpoint and store the response in the res variable. We then use Chai's assertions to verify that the response has a status code of 200, the body is an array, and the array has at least one element.

Running Tests with Mocha

Once you have written your test cases, you can use Mocha to run them. To do this, you will need to create a script in your project’s package.json file that invokes Mocha. Here's an example of such a script:

"scripts": {
"test": "mocha"
}

With this script in place, you can run your tests by typing npm test in the terminal. Mocha will execute all of the test cases in your project and report the results.

Automate GraphQL Query using Mocha and Chai

Let’s create a test file for our GraphQL query. We’ll call it query.test.js. Inside this file, we'll import Mocha and Chai and set up our test suite:

const chai = require('chai');
const mocha = require('mocha');

const expect = chai.expect;

Now, let’s write our first test case. We’ll start by writing a GraphQL query to fetch a list of users from our API. We’ll use the chai-http library to make an HTTP request to our API and send the GraphQL query:

https://gist.github.com/saranraj-s/4d7f70ed68f0e55e852ec2c08726f3fa#file-graphql

In the above test case, we first define our GraphQL query as a string. We then use the chai-http library to make a POST request to our API endpoint, sending the query in the request body. Finally, we use Chai's expect function to make assertions about the response we receive from the API.

Connect the Mysql with Mocha and Verify the API Response with DB data

  1. You can use the mysql2 library to connect to the database and run queries.
  2. In the test script, use the assert module to verify that the API response matches the expected value from the database.

Here is an example of how the test script might look:

https://gist.github.com/saranraj-s/98e4c409dedc0d1a5f0d39fbc8887f4a#file-connectdb

I hope this helps! Let me know if you have any questions or need further assistance.


Reference :

[embed]https://mochajs.org/[/embed][embed]https://mochajs.org/[/embed]

Meet The Team!

Author

Saranraj S

Edited By

Seema Jain


We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com
blog-img 2

Discussions

blog-img 3
5 mins
May 17, 2023
Sharing Data Between Controllers: Best Practices S...

This article will help you to understand the diffe

By Naveen C

blog-img 3
5 mins
March 21, 2023
Understanding Auto Layout and Constraints in Swift...

This article gives you an easy way of understandin

By Ramasamy P