Created
November 29, 2018 06:35
-
-
Save BCEvanFang/372213e82011b8d1e5e1626802ccb99b to your computer and use it in GitHub Desktop.
A simple express api test using supertest, mocha and chai
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
| /** | |
| * app.js | |
| */ | |
| var express = require("express"); | |
| var app = express(); | |
| app.get('/user', function(req, res) { | |
| res.status(200).json({ name: "john" }); | |
| }); | |
| module.exports = app; | |
| // | |
| /** | |
| * app.test.js | |
| */ | |
| var app = require("../app"); | |
| var supertest = require("supertest"); | |
| var assert = require("chai").assert; | |
| // Create request object to access app api | |
| var request = supertest(app); | |
| describe("GET /user", () => { | |
| it("respond with json", () => { | |
| request | |
| .get("/user") | |
| .set("Accept", "application/json") | |
| .expect("Content-Type", /json/) | |
| .expect(200) | |
| .end(function(err, res) { | |
| if (err) throw err; | |
| assert.equal(res.body.name, "john"); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment