Skip to content

Instantly share code, notes, and snippets.

@dschinkel
Last active May 14, 2019 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dschinkel/8f2933724c23e47b8a8b278b79968ade to your computer and use it in GitHub Desktop.
Save dschinkel/8f2933724c23e47b8a8b278b79968ade to your computer and use it in GitHub Desktop.
Some example Mocha, Supertest, and Superagent tests I've written
/*
Note:
Uses superagent and mocha assertions.
This is how I do these tests nowdays, without supertest, just plain superagent.
*/
import request from 'superagent'
it('returns a list of participants', async () => {
const url = 'https://someurl';
const response = await request
.get(url)
.set({ Authorization: `Bearer xxxxxxxxxx` })
.accept('application/json')
const participants = response.body.data
expect(participants.length).to.equal(2)
})
/*
Note:
I'm not really using supertest's assertion lib in this example; it's just using supertest as a way to get to superagent
(which in this case is totally unecessary) and then instead of supertest's assertion lib, simply using mocha asserts :).
Furthermore, the style here is I like to seperate out my arrange, act, assert into variables that read well,
then makes my ultimate assertion simpler and so overall easier to read and debug if I need to debug
*/
import supertest from 'supertest';
import app from '../../app';
describe.skip('Company - Integration Tests', () => {
let request;
before(() => {
const service = app.listen(4000);
request = supertest.agent(service);
});
it('returns a featured company', async () => {
const uri = '/companies/featured',
response = await request.get(uri);
expect(response.body.id).to.equal("1");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment