Make it Real Elite — Fifth week — Tests

Alejandro Estrada
Jul 21, 2017 · 2 min read

This was my first time doing tests. The first thing that I thought was, why is this useful if I can do manual tests on postman; then I decide to give it a chance and see why is important to use some test on the code that I create.


Jest:

Jest is a framework to make some test to react, it was created by the Facebook developers team. The first thing that I do with it was a test to some async methods created with express (this was not the best framework to test express but was a great challenge). Jest is the best way to test react

Mocha & Chai:

These are one of the best combinations to tests node.js; it is easy to use because the syntax is easy to understand, the only things to do is define the test to do, make some calls to the server using chai and define which properties should have the response like the type of data, the properties and all the things you considered the response should have

process.env.NODE_ENV = ‘test’;
let mongoose = require(“mongoose”);
let User = require(‘../models/user’);
let chai = require(‘chai’);
let chaiHttp = require(‘chai-http’);
let server = require(‘../server’);
let should = chai.should();
chai.use(chaiHttp);describe(‘User’, () => {
beforeEach((done) => {
User.remove({}, (err) => {
done();
});
});
describe(‘/POST new user’, () => {
it(‘it should not POST a new user without userName’, (done) => {
let user = {
email: ‘test@test.com’,
password: ‘test’,
}
chai.request(server)
.post(‘/api/user’)
.send(user)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a(‘object’);
res.body.should.have.property(‘errors’);
res.body.errors.should.have.property(‘userName’);
res.body.errors.userName.should.have.property(‘kind’).eql(‘required’);
done()
})
});
})
})

The final verdict:

After being a little skeptical on doing tests to the code, I found by myself the importance of doing them. In this week I decided to clean a little bit my code on the server app used in the canvas; I had created some test to it and when I started cleaning my code and making some changes I found that was faster to check if the changers were without mistakes, just checking the result of the test and no doing manual test; I made a lot of changes and found some mistakes thanks to the test, this mistakes would cost me a lot of time if I do a manual check so after this I found that the test can save you a lot of useful time and can help you find the mistakes faster than checking all the code after a crash.

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade