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. Python(七) 高级部分:面向对象

    一.类的定义 # 面向对象 #有意义的面向对象代码 # 类 = 面向对象 # 类.对象 #实例化 # 类最基本的作用:封装 class Student(): name = '' age = 0 def ...

  2. 分享一个vue中的vue-Resource用法

    //引入 <script src="//cdn.bootcss.com/vue-resource/1.2.1/vue-resource.js" type="text ...

  3. Unix操作系统的入门与基础

    http://dev2dev.cnblogs.com/archive/2005/10/10/251894.aspx Unix操作系统的入门与基础 与大家熟悉的Windows用户界面和使用习惯不同,Un ...

  4. opengl绘制三维人物luweiqi

    素材中有四个.bmp格式的纹理文件和一个.txt的模型参数文件 文件格式说明: 纹理文件数量 纹理文件1(字符串)//.bmp 纹理文件2(字符串) 纹理文件3(字符串) . . . 材质数量 amb ...

  5. Swift实战(2)--在工程中添加object-C的类或者第三方框架(附翻译)

    原文地址:http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift Using Objec ...

  6. win7安装python开发环境,执行python

    在win7上安装python的开发环境是很easy的事情 Step1:下载python安装文件 url:https://www.python.org/download 去这里找到你想要下载的文件 St ...

  7. OL记载Arcgis Server切片

    概述: 本文讲述怎样在OpenLayers中调用Arcgis Server切片并显示. 思路: 在OpenLayers中载入Arcgis Server切片用XYZ图层,Arcgis Server的切片 ...

  8. [Android]新建项目继承Activity不继承ActionBarActivity

    在SDK更新后,在eclipse新建Android项目时.我们常常会碰到这样一种事情:新建的MainActivity不再继承Activity而是继承ActionBarActivity,因为一些人的开发 ...

  9. 荣获CCF(中国计算机学会)高级会员代表资格

    详细地址:http://www.ccf.org.cn/sites/ccf/xjhydb.jsp?contentId=2624287722908 650) this.width=650;" b ...

  10. Kinect 开发 —— 显示骨骼用户插件

    public partial class SkeletonViewer : UserControl { private readonly Brush[] _SkeletonBrushes = new ...