-
-
Save sotnikov-link/42fddc47dcdaefcc75bb92f2568e73be to your computer and use it in GitHub Desktop.
Use Jest (Jasmine) and supertest to test REST API.
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
// 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