Last active
May 14, 2019 23:41
-
-
Save dschinkel/8f2933724c23e47b8a8b278b79968ade to your computer and use it in GitHub Desktop.
Some example Mocha, Supertest, and Superagent tests I've written
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
/* | |
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) | |
}) |
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
/* | |
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