Using Supertest to test your API and adding to CircleCI
If you want to test your API service, here at this article you will find your solution.
Before you start
Create an account:
Documentation:
https://jsonplaceholder.typicode.com/
Visual Code:
Download and Install nodejs.
Jest:
https://jestjs.io/docs/getting-started
Supertest:
https://www.npmjs.com/package/supertest
After install visual studio code and Supertest you are able to create your tests, so first let’s create a folder “supertest” and then open this folder in your visual studio code and type npm init on terminal. A package.json is gonna be create, modify the informations below, before move to the next step(You should have created a repository previously, because you are going to use the path in git repository line)
Change our package.json to
{"name": "mytest","version": "1.0.0","description": "this is a test","main": "index.js","scripts": {"test": "jest"},"repository": {"type": "git","url": "git+https://github.com/danilow86/supertest.git"},"author": "Danilo","license": "ISC","bugs": {"url": "https://github.com/danilow86/supertest/issues"},"homepage": "https://github.com/danilow86/supertest#readme","dependencies": {"jest": "^26.6.3","supertest": "^6.1.3"}}
Our First Test
I am assuming that you previously installed jest and supertest, so moving on creating a test:
Remember our reference: https://jsonplaceholder.typicode.com/, let’s create a file with ‘spec.js’ to insert our test
//Import Supertestconst request = require('supertest');//Describe your Actiondescribe('GET /posts', function() {//Create a testit('Should return a status code 200', function(done) {request('https://jsonplaceholder.typicode.com/')//HTTP Verb.get('posts/5')//Headers.set('Accept', 'application/json')//Our assertion.expect('Content-Type', /json/).expect(200, done);});
});
So now let’s run the following command npm test and we should see the following result
Above you can find our first test, basically we have an assertion that contain our validation which guarantees that we will have a response with the code 200. Before we add our test to Circle CI let’s add one more test validating a Post method and a return message, so copy the following content
//Import Supertestconst request = require('supertest');//Describe your Actiondescribe('GET /posts', function() {//Create a testit('Should return a status code 200', function(done) {request('https://jsonplaceholder.typicode.com/')//HTTP Verb.get('posts/5')//Headers.set('Accept', 'application/json')//Our assertion.expect('Content-Type', /json/).expect(200, done);});});describe('Create /post', () => {it('Should Return my Json', done => {request('https://jsonplaceholder.typicode.com/').post('posts').set('Accept', 'application/json').send({title: 'test',body: 'Earth is not flat',userId: 2}).expect('Content-Type', /json/).expect({ title: 'test', body: 'Earth is not flat', userId: 2, id: 101 }, done);});});
Add to your git repository and commit your project, if you have any doubts about how to do, follow this article https://medium.com/@danilow86/a-quick-useful-git-guide-by-me-6688dfc6e7c
Adding your project to CircleCI
Now go to https://app.circleci.com/projects/project-dashboard/ (if you are logged in with your github account you should see the list of your projects), click on the project you want to configure, click on use the existing configuration and download the yml file template
going back to the project, create a new folder circleCi and inside this folder create a file config.yml and paste the content previously downloaded. Go to your project on circleCi and select the option Commit and Run
After this you should see your logs containing all the execution process like below:
Steps:
Tests
So now have your tests running added to CircleCi
Repository:
https://github.com/danilow86/supertest
CircleCi Dashboard:
References: