GraphQL Automation with Database Verification
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.
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.
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.
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”.
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.
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-sampleIn 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.
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.
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:
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.
mysql2
library to connect to the database and run queries.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-connectdbI hope this helps! Let me know if you have any questions or need further assistance.
Author
Edited By
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
Leave a Reply