resource-router-middleware icon indicating copy to clipboard operation
resource-router-middleware copied to clipboard

Testing example

Open serain opened this issue 7 years ago • 2 comments

Can you give an example of running a test case for index for example (with jest or any other testing framework)

serain avatar Nov 26 '18 19:11 serain

Mentioned it in the other issue, but I'd recommend using something like supertest for testing, since that allows for passing through any registered middleware.

For everything else, testing can be made easier by wrapping up your request handlers as their own module and only using resource-router-middleware to bind them to routes.

developit avatar Nov 26 '18 21:11 developit

Thanks for getting back to me and for the suggestion.

I'm still having some issues with supertest unfortunately, even with this minimalist example:

foo.js

import resource from 'resource-router-middleware'

export default () => resource({
  id: 'foo',

  create: [
    (req, res) => {
      res.status(201).json({ 'foo': 'bar' })
    }
  ]
})

foo.test.js

import request from 'supertest'
import foo from '../../../src/routes/v1/foo'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000

test('create should return 201', () => {
  return request(foo).post('/').then(response => {
    expect(response.statusCode).toBe(201)
  })
})

I'm getting the following error even when increasing the timeout to 60 seconds as above:

 FAIL  tests/routes/v1/foo.test.js (61.578s)
  ✕ create should return 201 (60047ms)

  ● create should return 201

    Timeout - Async callback was not invoked within the 60000ms timeout specified by jest.setTimeout.

      4 | jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000
      5 |
    > 6 | test('create should return 201', () => {
        | ^
      7 |   return request(foo).post('/').then(response => {
      8 |     expect(response.statusCode).toBe(201)
      9 |   })

      at Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:85:20)
      at Object.test (tests/routes/v1/foo.test.js:6:1)

serain avatar Nov 26 '18 22:11 serain