Skip to content

Instantly share code, notes, and snippets.

@ericwooley
Last active August 20, 2017 21:18
Show Gist options
  • Select an option

  • Save ericwooley/6fdd41eadd5b2f0cf4c65cd2aab71c85 to your computer and use it in GitHub Desktop.

Select an option

Save ericwooley/6fdd41eadd5b2f0cf4c65cd2aab71c85 to your computer and use it in GitHub Desktop.

Revisions

  1. ericwooley revised this gist Aug 20, 2017. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions api.integration.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,8 @@ const api = process.env.API_URL || 'http://localhost:3200'
    * 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
    // 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
    @@ -42,9 +43,11 @@ const testGetEndpoint = (endpoint, options = {}) =>

    // Describe the /products route
    describe('/products', () => {
    // remember that `testGetEndpoint('/products/')` returns a function, which will be run as the test.
    // 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.
    // remember that `testEndpointSnapshot({...})` returns a function,
    // which will be run as the test.
    it(
    'should create a new product',
    testEndpointSnapshot({
  2. ericwooley revised this gist Aug 20, 2017. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions api.integration.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@ const api = process.env.API_URL || 'http://localhost:3200'
    * 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 = {}) =>
    @@ -38,7 +37,7 @@ const testGetEndpoint = (endpoint, options = {}) =>

    /************************************************************************
    * Actual Tests
    * These functions make our actual tests much shorter and readable
    * Where the actual tests are being described and run
    ************************************************************************/

    // Describe the /products route
  3. ericwooley revised this gist Aug 20, 2017. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion api.integration.js
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,12 @@ 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 = {}) =>
    @@ -30,6 +36,12 @@ const testGetEndpoint = (endpoint, options = {}) =>
    options
    )

    /************************************************************************
    * Actual Tests
    * These functions make our actual tests much shorter and readable
    ************************************************************************/

    // 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/'))
    @@ -47,4 +59,4 @@ describe('/products', () => {
    }
    })
    )
    })
    })
  4. ericwooley revised this gist Aug 20, 2017. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion api.integration.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,12 @@ const testEndpointSnapshot = (requestOptions, options = {}) =>
    // 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.uri + options.snapShotName)
    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
  5. ericwooley created this gist Aug 20, 2017.
    45 changes: 45 additions & 0 deletions api.integration.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    // 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'

    // 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.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
    )

    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
    }
    })
    )
    })