Skip to content

Instantly share code, notes, and snippets.

@sotnikov-link
Forked from risingnote/jest-suppertest.js
Created September 25, 2017 16:39
Show Gist options
  • Save sotnikov-link/42fddc47dcdaefcc75bb92f2568e73be to your computer and use it in GitHub Desktop.
Save sotnikov-link/42fddc47dcdaefcc75bb92f2568e73be to your computer and use it in GitHub Desktop.
Use Jest (Jasmine) and supertest to test REST API.
// Need to run test async and use jest/jasmine done.fail to let it know when an assertion did not work.
const routerUnderTest = require('../../client_api')
const httptest = require('supertest')
const express = require('express')
const app = express()
app.use('/', routerUnderTest)
describe('I can use the REST api', () => {
it('displays text for the landing page', (done) => {
httptest(app)
.get('/')
.end((err, res) => {
try {
expect(err).toBeNull()
expect(res.status).toEqual(200)
expect(res.text).toEqual('You have reached the SPDZ Rest interface. See ....')
done()
} catch (err) {
done.fail(err)
}
})
})
})
// To wrap the horrible try catch block can use
const supertestWithJest = (err, res, done, asserts) => {
try {
expect(err).toBeNull()
asserts()
done()
} catch (err) {
done.fail(err)
}
}
describe('I can use the REST api', () => {
it('displays text for the landing page', (done) => {
httptest(app)
.get('/')
.end((err, res) => {
supertestWithJest(err, res, done, () => {
expect(res.status).toEqual(200)
expect(res.text).toEqual('You have reached the SPDZ Rest interface. See ....')
})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment