Set app on modules.app
const express = require('express')
const routes = require('./routes')
const cors = require('cors')
const app = express()
app.use(express.json())
app.use(cors())
app.use(express.urlencoded({ extended: true })) // Enables POST
app.use('/', routes.router)
app.get('/', ( req, res ) => {
res.send({ status: 'ok'}).status(200);
});
app.use((err, req, res, next) => {
const {statusCode = 500, message} = err
res.status(statusCode).send(message)
})
app.listen(config.app_port, () =>
console.log(`
馃殌 Server ready at: http://localhost:${config.app_port} 猸愶笍`
),
)
// This line
module.exports = app
Install mocha
npm install --save-dev mocha supertest
Use Mocha by descript -> it -> expect
const app = require('../app) # WebServer usin Express
const request = require('supertest')
const assert = require('assert')
describe('GET /users', () => {
it('should return users in body', async () => {
await request(app)
.get(`/users`)
.expect(200)
.expect(res => {
if (!res.body.users) throw new Error('Users not found')
})
})
})