hienlh / LuhihiCb

0 stars 0 forks source link

Complete document about ExpressJS #7

Closed hienlh closed 5 years ago

hienlh commented 5 years ago

Description To have document about ExpressJS

Acceptance Criteria

hienlh commented 5 years ago

Requirement Module

Mocha, chai, supertest

yarn add mocha chai supertest

For typescript:

yarn add @types/mocha
yarn add -D chai mocha supertest @types/supertest

Usage

Create unit test file

  1. Create a folder at root to hold all test file, folder's name is up to you. I call it test here.

  2. 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!')
            })
    });

});

Some important supertest/superagent function

get(url, callback)

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.

post(url, callback)

To make a post request to the router.