We have express app:

import _ from 'lodash'
import faker from 'faker'
import express from 'express'
import bodyParser from 'body-parser'
import getTokenFromHeader from '../../src/routes/utils/get-token-from-header' export default startServer const users = _.times(20, () => faker.helpers.contextualCard())
const userAuth = {
username: 'jane',
password: 'I have a secure password',
}
const user = {
token: 'Wanna-hear-a-secret?-I-sometimes-sing-in-the-shower!',
} function startServer() {
const app = express() app.use(bodyParser.json()) function auth(req, res, next) {
const token = getTokenFromHeader(req)
if (!token || token !== user.token) {
res.sendStatus(401)
} else {
next()
}
} const userRouter = express.Router()
userRouter.get('/', (req, res) => {
const {query: {limit = 20, offset = 0}} = req
res.json({users: _.take(users.slice(offset), limit)})
}) // Preload user objects on routes with ':username'
userRouter.param('username', (req, res, next, param) => {
req.user = users.find(({username}) => username === param)
next()
}) userRouter.get('/:username', (req, res) => {
if (req.user) {
res.json({user: req.user})
} else {
res.sendStatus(404)
}
}) userRouter.post('/', auth, (req, res) => {
users.unshift(req.body.user)
res.json({user: users[0]})
}) userRouter.delete('/:username', auth, (req, res) => {
users.splice(users.indexOf(req.user), 1)
res.json({success: true})
}) const authRouter = express.Router()
authRouter.post('/', (req, res) => {
if (
req.body.username === userAuth.username &&
req.body.password === userAuth.password
) {
res.json({user})
} else {
res.sendStatus(401)
}
}) const apiRouter = express.Router()
apiRouter.use('/users', userRouter)
apiRouter.use('/auth', authRouter) app.use('/api', apiRouter) return new Promise(resolve => {
const server = app.listen(3001, () => {
resolve(server)
})
})
}

As you can see, we wrap Express App into a function 'startServer' and export it as default export. The return value of this function is the server which wrap into a Promise.

The good part for doing this way is that we can start and stop server whenever we want to prevent menory leak or "ADDRESS IN USED" problem.

import startServer from '../start-server'
import axios from 'axios' let server beforeAll(async () => {
server = await startServer()
}) afterAll(done => server.close(done)) test('can get users', async () => {
const user = await axios
.get('http://localhost:3001/api/users')
.then(response => response.data.users[0]) // toMatchObject, to check whether user object
// has 'name' prop which is a string
// and 'username' prop which is a string
// this is a subset, doesn't need to match all the object props
expect(user).toMatchObject({
name: expect.any(String),
username: expect.any(String)
})
}) // Test offset and limit should work
// first get 5 users
// then get last two users by given limit and offset
// then check tow users and equal to last tow user in five users.
test('get users, limit and offset should work', async () => {
const fiveUsersPromise = axios
.get('http://localhost:3001/api/users?limit=5')
.then(response => response.data.users)
const twoUsersPromise = axios
.get('http://localhost:3001/api/users?limit=2&offset=3')
.then(response => response.data.users) const response = await Promise
.all([fiveUsersPromise, twoUsersPromise])
const [fiveUsers, twoUsers] = response
const [, , ,firstFiveUser, secondFiveUser] = fiveUsers
const [firstTwoUser, secondTwoUser] = twoUsers
expect(firstTwoUser).toEqual(firstFiveUser)
expect(secondFiveUser).toEqual(secondTwoUser)
})

In the test, we call 'beforeAll' to start the server and 'afterAll' to close the server.

[Node & Testing] Intergration Testing with Node Express的更多相关文章

  1. 前端学习 node 快速入门 系列 —— 报名系统 - [express]

    其他章节请看: 前端学习 node 快速入门 系列 报名系统 - [express] 最简单的报名系统: 只有两个页面 人员信息列表页:展示已报名的人员信息列表.里面有一个报名按钮,点击按钮则会跳转到 ...

  2. node部署静态页面;node上线静态页面

    node部署静态页面上线 静态页面上线可以采用 nginx, tomcat或者node ,我们这里介绍下node部署静态页面 这里采用最简单的上线方式,我们就不用node + express + ej ...

  3. Node学习基础之安装node以及配置环境变量

    第一步去node官网下载nodejs 我放在D盘 接着在cmd输入node -v 就能得到node的版本号 还有npm -v 下来进入安装好的目录 nodejs目录 创建两个文件夹 node_cach ...

  4. Difference Between Performance Testing, Load Testing and Stress Testing

    http://www.softwaretestinghelp.com/what-is-performance-testing-load-testing-stress-testing/ Differen ...

  5. node.js系列笔记之node.js初识《一》

    node.js系列笔记之node.js初识<一> 一:环境说明 1.1 Linux系统CentOS 5.8 1.2 nodejs v0.10.15 1.3 nodejs源码下载地址 htt ...

  6. node.js入门系列(一)--Node.js简介

    什么是NodeJS JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需要独立运行的JS,NodeJS就是一个解析器. 每一种解析器都是一 ...

  7. 【node】使用nvm管理node版本

    写在前面 nvm(nodejs version manager)是nodejs的管理工具,如果你想快速更新node版本,并且不覆盖之前的版本:或者想要在不同的node版本之间进行切换: 使用nvm来安 ...

  8. 什么是Node.js?带你初识Node

    什么是Node.js Nodejs是一个基于Chrome v8引擎的JavaScript运行环境 Node.js使用了一个事件驱动,非阻塞式I/O的模型,使其轻量又高效. Node.js 的包管理器 ...

  9. Node.js的安装以及Node.js的模块管理

    索引: Node.js的安装以及Node.js的模块管理Node.js开发环境搭建以及对ES6的支持Node.js构建Vue.js项目Vue.js单文件组件的开发基于Vue.js的UI组件(Eleme ...

随机推荐

  1. Visual Studio Code Setup

    Windows https://code.visualstudio.com/docs/setup/windows Additional Components and Tools https://cod ...

  2. difference in physical path, root path, virutal path, relative virtual path, application path and aboslute path?

    http://stackoverflow.com/questions/13869817/difference-in-physical-path-root-path-virutal-path-relat ...

  3. Cisco路由器交换机配置命令详解

    1. 交换机支持的命令: 交换机基本状态:switch: :ROM状态, 路由器是rommon>hostname> :用户模式hostname# :特权模式hostname(config) ...

  4. Nagle和Cork

    我觉得这篇讲的不错. http://blog.csdn.net/c_cyoxi/article/details/8673645 Nagle算法的基本定义是任意时刻,最多只能有一个未被确认的小段. 关闭 ...

  5. [Python] isinstance() for checking object type

    isinstance("foo", str) isinstance(1, int) isinstance(4.0, float)

  6. 使用Microsoft excel 2007 进行数据分析---环境配置

    使用Microsoft excel 2007 进行数据分析---环境配置 使用前须要安装SQL server 2008 data mining Add-ins for Microsoft excel  ...

  7. oracle 数据库批处理文件

    文件夹结构 初始化脚本 |----orcl_sql |----init_user.sql |----tab_home.sql |----TAB_USER.sql |----init.bat init. ...

  8. AVEVA PDMS 三维文字工具

    AVEVA PDMS 三维文字工具 eryar@163.com 网上有个文字工具插件,可以在PDMS中创建三维的字母和数字,且字体样式只有一种,其下载地址为:http://www.plantcon.d ...

  9. button-xml 中android:clickable="false" 属性

    今天在做项目的时候,遇到了一个问题,就是需要把一个常按监听事件,加到一个linearlayout中,但是,这个linearlayout中有其他的button.textview等控件,这样就导致当我们常 ...

  10. Android 学习笔记进阶十二之裁截图片

    package xiaosi.cut; import java.io.File; import android.app.Activity; import android.content.Intent; ...