Created
April 16, 2017 22:21
-
-
Save pnhoang/a5032fce9eeb8df4bc6da591f7ae8d72 to your computer and use it in GitHub Desktop.
node.js express testing with supertest and mocha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SuperTest works with any test framework, here is an example without using any test framework at all: | |
const request = require('supertest'); | |
const express = require('express'); | |
const app = express(); | |
app.get('/user', function(req, res) { | |
res.status(200).json({ name: 'tobi' }); | |
}); | |
request(app) | |
.get('/user') | |
.expect('Content-Type', /json/) | |
.expect('Content-Length', '15') | |
.expect(200) | |
.end(function(err, res) { | |
if (err) throw err; | |
}); | |
Here's an example with mocha, note how you can pass done straight to any of the .expect() calls: | |
describe('GET /user', function() { | |
it('respond with json', function(done) { | |
request(app) | |
.get('/user') | |
.set('Accept', 'application/json') | |
.expect('Content-Type', /json/) | |
.expect(200, done); | |
}); | |
}); | |
https://www.npmjs.com/package/supertest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment