resource-router-middleware
resource-router-middleware copied to clipboard
Testing example
Can you give an example of running a test case for index for example (with jest or any other testing framework)
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.
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)