You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importserver,{app}from'/server.ts'constmockGetCache=jest.fn();constmockSetCache=jest.fn();// This is mocking classjest.mock('../../src/infra/clients/memcached-client',()=>jest.fn().mockImplementation(()=>({getCache: mockGetCache,setCache: mockSetCache,})));it('POST cardData Error',async()=>{mockGetCache.mockImplementation(()=>{thrownewError('test-error in POST cardData');});constresponse=awaitrequest(app).post('/api/v1/ad/cardData').send(fakeCardData);expect(response.statusCode).toBe(500);expect(response.body).toEqual({code: 9999,message: 'test-error in POST cardData'});});
Application code
server.ts
// ...// set up routesapp.use('/api/v1',routes);// set up error handlerapp.use(errorHandler);// ....
Simple workflow
My code doesn't respond 500 to client directly. I use express error handler to catch all error and responds error to client here.
So I also use express-async-handler to catch unexpected error, which will call next() and let error-handler catch the error.
Problem:
When I start server, everything works fine with both postman, curl, or my Frontend Website and Mobile.
However it went into Timeout problem when I do integration test with supertest.
Below is log
● ad server unit test ›POST cardData Error
thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
Possible reason
next function not working normally.
After digging deep to the express-async-handler, and I found that everything is good with it.
At the final step express-async-handler also call next(err), but my error-handler didn't catch the err.
I tried remove express router, and use the handler directly by app.post(...) in server.ts
like
// ...// set up routesapp.use('/api/v1',(req,res,next)=>{try{//...}catch(err){console.log('[handler Error]',err)returnnext(err)}});// set up error handlerapp.use(errorHandler);// ....
This will work correctly.
Still have no clue why.
anyone can save me?
The text was updated successfully, but these errors were encountered:
This issue is because of jest.useFakeTimer will probably mock out some event loop function under next().
Prevent using jest.useFakeTimer will fix this issue.
Env:
typescript 4.7.4
Node 14
Supertest 6.2.4
Jest 28.1.3
Express 4.18.1
Express-async-Handler 1.2.0
Test Code
Application code
Simple workflow
My code doesn't respond 500 to client directly. I use express error handler to catch all error and responds error to client here.
So I also use express-async-handler to catch unexpected error, which will call
next()
and let error-handler catch the error.Problem:
When I start server, everything works fine with both postman, curl, or my Frontend Website and Mobile.
However it went into Timeout problem when I do integration test with
supertest
.Below is log
Possible reason
next
function not working normally.After digging deep to the
express-async-handler
, and I found that everything is good with it.At the final step
express-async-handler
also callnext(err)
, but my error-handler didn't catch theerr
.found similar issue here #529
update
I tried remove express router, and use the handler directly by
app.post(...)
inserver.ts
like
This will work correctly.
Still have no clue why.
anyone can save me?
The text was updated successfully, but these errors were encountered: