View on GitHub

Notes

reference notes

Express Testing with SuperTest Cheat Sheet

Setup

  1. Install SuperTest: npm install supertest --save-dev
  2. Import SuperTest: const request = require("supertest");
  3. Import Express: const express = require("express");
  4. Create an Express app: const app = express();
  5. Use the router you want to test: app.use("/", index);

Writing Tests

Example

test("index route works", done => {
  request(app)
    .get("/")
    .expect("Content-Type", /json/)
    .expect({ name: "frodo" })
    .expect(200, done);
});

Testing POST Routes

Example

test("testing route works", done => {
  request(app)
    .post("/test")
    .type("form")
    .send({ item: "hey" })
    .then(() => {
      request(app)
        .get("/test")
        .expect({ array: ["hey"] }, done);
    });
});

Additional Notes