Closed hienlh closed 5 years ago
Mocha, chai, supertest
yarn add mocha chai supertest
For typescript:
yarn add @types/mocha
yarn add -D chai mocha supertest @types/supertest
Create a folder at root to hold all test file, folder's name is up to you. I call it test
here.
Create a test file (I write it in typescript)
import {expect} from 'chai';
import request from 'supertest';
import app from '../src/';
describe('Unit testing the root route', function () {
it('should return OK status', function () {
return request(app)
.get('/')
.expect(200);
});
it('should return message on rendering', function () {
return request(app)
.get('/')
.expect((res) => {
expect(res.text).to.contain('Connect success!')
})
});
});
describe
is a mocha function to define the test file. It contains a title and a callback function to process testing.
it
is used to write testing code. First parameter is a short describe for testing function. Second one is a callback function.
request()
need a HTTP server so I pass my express appication definded in src/index.js
Some subfunction of request
I'll talk on following session.
To make a get request to the router. You need to pass an url for router endpoint. You don't to pass full url, just need the tail of url like /webhook
.
To make a post request to the router.
Description To have document about ExpressJS
Acceptance Criteria