Skip to content

Instantly share code, notes, and snippets.

@ericwooley
Last active August 20, 2017 21:18
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 ericwooley/6fdd41eadd5b2f0cf4c65cd2aab71c85 to your computer and use it in GitHub Desktop.
Save ericwooley/6fdd41eadd5b2f0cf4c65cd2aab71c85 to your computer and use it in GitHub Desktop.
jest test for integration
// see https://github.com/request/request-promise
const requestPromise = require('request-promise')
// in case you want to mess with where the integration test happens
const api = process.env.API_URL || 'http://localhost:3200'
/************************************************************************
* Test Setup Functions
* These functions make our actual tests much shorter and readable
************************************************************************/
// this accepts request options,
// and returns a function that makes a request with those options
// which tests the result against a snapshot
const testEndpointSnapshot = (requestOptions, options = {}) =>
// returns function, so you can pass it right into a test
() =>
// Makes a request with those options
requestPromise(requestOptions).then(result =>
// expect the result to match a snapshot named after the uri
expect(result).toMatchSnapshot(
requestOptions.method +
' ' +
requestOptions.uri +
(options.snapShotName || '')
)
)
// short cut for get requests, so all that you need to pass in is the uri
const testGetEndpoint = (endpoint, options = {}) =>
testEndpointSnapshot(
{
method: 'GET',
uri: api + endpoint
},
options
)
/************************************************************************
* Actual Tests
* Where the actual tests are being described and run
************************************************************************/
// Describe the /products route
describe('/products', () => {
// remember that `testGetEndpoint('/products/')` returns a function,
// which will be run as the test.
it('should list all products', testGetEndpoint('/products/'))
// remember that `testEndpointSnapshot({...})` returns a function,
// which will be run as the test.
it(
'should create a new product',
testEndpointSnapshot({
json: true,
method: 'POST',
uri: api + '/products/',
body: {
name: 'some product',
description: 'shoes',
price: 12.25
}
})
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment