Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jest method done() is not defined

Tags:

jestjs

I started using jest and I now need to test callbacks. To know when a callback was called, the done() is supposed to be used accourding to the documentation: https://jestjs.io/docs/en/asynchronous.html

However the done() is not recognized beeing undefined and is consequently throwing this Error:

Test suite failed to run

    TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
   pathToErrorFile:line - error TS2304: Cannot find name 'done'.

    63       done();
             ~~~~

//code to reproduce:
test('test', ()=>{
   fkt(param, ()=>{
      done();   
   }); 
}); 

I have setup jest with node and angular and in both projects this function does not exist. So what I want to know is, where this function is even coming from and how I can troubleshoot it. Note that everything else (test, describe, etc. ) is working fine done() as its exception.

like image 400
telion Avatar asked Jan 01 '23 16:01

telion


1 Answers

done is not defined as a global var. You get it passed to the test function.

test('test', done => {
   fkt(param, () => {
      done();   
   }); 
});

Note that if you specify done parameter, jest will detect it and will fail the test on timeout, if the done function is not called after the test has finished.

If done() is never called, the test will fail (with timeout error), which is what you want to happen.

Then, you have to call done even if the test fails - otherwise you won't see the error.

If we want to see in the test log why it failed, we have to wrap expect in a try block and pass the error in the catch block to done. Otherwise, we end up with an opaque timeout error that doesn't show what value was received by expect(data).

See Jest - Testing Asynchronous Code

like image 85
kvetis Avatar answered Apr 26 '23 01:04

kvetis