We’ll start testing our endpoints using postman. This is very important, especially when creating your applications using Test Driven Development(TDD) principles.

If you haven’t done previous steps, I would recommend going over previous steps, since you are testing the backend.

We’ll start going over how we are gonna test our endpoints. The updated version of postman has chai built in. Which is testing library that makes testing easier due to it’s similarities to the english language. We won’t be doing anything to complex.

Here is the chai documentation. You should reference heavily when testing with postman.

First create a postman_collection folder outside your src folder. Then cd into it and create a ecommerceApp.postman_testing.json within that directory.

mkdir postman_collection
cd postman_collection
touch ecommerceApp.postman_testing.json

Now copy the collection from this url and paste it to your ecommerceApp.postman_testing.json file.

https://www.getpostman.com/collections/356ccb6c3277e1ebcba4

Make, all the properties within double quotes after you paste it to make it valid json.

Now go to postman and click the import on the top left to import the collection. Then import the file.

Now we have the collection uploaded.

Now we will just do some basic testing on the get endpoints. We will first do our ‘/api/users’.

We will access our response via the pm.response.json() since the type is json, and assign it to variable called response.

//Get the current response from the postman global object which will be of type json.
const response = pm.response.json();

Now to start testing, use the pm.test which will take a note as first argument describing the test, then a callback initiating the test.

pm.test('Note', () => {
//Test Callback
});

We will create a test for our status code, and we are also gonna purposely fail one test for each endpoint.

//Get the current response from the postman global object which will be of type json.
const response = pm.response.json();
//Do the pm.test to perform a test with a note, which can be anything, and a callback performing the test.
pm.test('check the status code is 200', () => {
//Use chai language chains to improve the readability of the test on the pm object.
pm.response.to.have.status(200);
})
//Now we are gonna make it purposely fail.
pm.test('check if the status code 201', () => {
//Use chai language chains to improve the readability of the test on the response VARIABLE.
response.to.have.status(201)
})

Now we have tested first endpoint, now run your tests. In your tests tab you should 1/2 test should past.

Now lets test the /api/products that will get all the products. We will test if the products returned is an array, and it returns a status code of 200. Then check if the response is also a object to make it purposely fail.

First set response to the pm.response.json(). Then start performing your first test by checking if the status code is 200.

//Get the current response from the postman global object which will be of type json.
const response = pm.response.json();
//Do the pm.test to perform a test with a note, which can be anything, and a callback performing the test.
pm.test('check the status code is 200', () => {
//Use chai language chains to improve the readability of the test on the pm object.
pm.response.to.have.status(200);
})

Now check if the response is an array, then make it purposely fail, by checking if it’s a object.

//Do the pm.test to perform a test with a note, which can be anything, and a callback performing the test.
pm.test('check if response is an array', () => {
//Use chai language chains to improve the readability of the test on the pm object.
//Test if the resposne is an array
pm.expect(response).to.be.an('array');
})
//Now we are gonna make it purposely fail.
pm.test('check if response is a object', () => {
//Use chai language chains to improve the readability of the test on the response VARIABLE.
//Test if the response is an object
pm.expect(response).to.be.an('object');
})

Now run your tests, and in your tests tab it should be 2/3

One more test for our get endpoints. We will test getting a specific product using it’s id.

We will assign the response to pm.response.json(). Then will do the same tests except we will have our passing test be checking if it’s a object. Since it is returning a object, and our failing test will be checking if it’s an array.

//Get the current response from the postman global object which will be of type json.
const response = pm.response.json();
//Do the pm.test to perform a test with a note, which can be anything, and a callback performing the test.
pm.test('check the status code is 200', () => {
//Use chai language chains to improve the readability of the test on the pm object.
pm.response.to.have.status(200);
})
//Do the pm.test to perform a test with a note, which can be anything, and a callback performing the test.
pm.test('check if response is an object', () => {
//Use chai language chains to improve the readability of the test on the pm object.
//Test if the resposne is an array
pm.expect(response).to.be.an('object');
})
//Now we are gonna make it purposely fail.
pm.test('check if response is a array', () => {
//Use chai language chains to improve the readability of the test on the response VARIABLE.
//Test if the response is an object
pm.expect(response).to.be.an('array');
})

Test it, and the tests tab should have 2/3 pass.

Great, we have our get endpoints tested on. Now will just need to test all of the create, update, delete endpoints.

Here is my Github for reference.

Next step we will finish our testing and finally start doing front-end work.

Happy Coding!