Skip to content

Instantly share code, notes, and snippets.

@luqmanoop
Last active November 22, 2018 20:25
Show Gist options
  • Save luqmanoop/acc8d059230910149b6302c9cd9841fb to your computer and use it in GitHub Desktop.
Save luqmanoop/acc8d059230910149b6302c9cd9841fb to your computer and use it in GitHub Desktop.
A simple TDD case for a `/cats` endpoint
import chai, { expect } from "chai";
import chaiHttp from "chai-http";
import server from "../server";
chai.use(chaiHttp);
const ENDPOINT = "/api/v1/cats";
let request = null;
describe("/api/v1/cats", () => {
before(() => {
request = chai.request(server).keepOpen();
});
after(() => request.close());
describe("GET /", () => {
it("should respond with an array of cats", done => {
request.get(ENDPOINT).then(res => {
expect(res.body).to.be.an("array").that.is.not.empty;
done();
});
});
});
});
@luqmanoop
Copy link
Author

Keeps server connection open/close (to aid multiple requests) using Mocha before/after hooks.

before(() => {
    request = chai.request(server).keepOpen();
  });

so instead of doing this for every request in all test suite

chai.request(app).get(someEndpoint)

we have this instead

request.get(endpoint)

The test verifies that the endpoint responds with an array of cats

@marcdomain
Copy link

Good job. Your explanations made it easier to understand.

@akhilome
Copy link

Superb one, @codeshifu.

Thanks for providing clarity with the comment, too. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment