Jest: example async test or resolve will not work and return error of ` TypeError: Cannot read property 'then' of undefined`

Created on 8 Dec 2017  路  4Comments  路  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?
Bug

What is the current behavior?
Return error when setup the test and mock properly 99% according to the tutorial page here https://facebook.github.io/jest/docs/en/asynchronous.html

**If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
Test suppose to pass. An exampl here
https://github.com/adamchenwei/jest-playground
run npm install && npm run test:watch you should observe the error.

What is the expected behavior?
Test should pass as tutorial indicated.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

They are in the demo repo https://github.com/adamchenwei/jest-playground

Most helpful comment

In the doc folder where you define user.js and request.js you should have a __mocks__ folder and within that you should define the request.js mock function.

All 4 comments

Not really sure what is the significant difference beside I config the jest to place test in the same folder as component as well as use timeout instead of process.nextTick

In the doc folder where you define user.js and request.js you should have a __mocks__ folder and within that you should define the request.js mock function.

@jack-evans how you know that so quickly? Did I missed it from the doc reading? Or there is way somehow you debug and found the issue? ? Super curious to know how you did that!!!
Just in case anyone interested in the fix.. https://github.com/adamchenwei/jest-playground/commit/08a861ff15daac7d5ee5aaa7b0e71bd3369abc85

There is this on the Jest website that I remember using when i first wrote some manual mocks:

https://facebook.github.io/jest/docs/en/manual-mocks.html

so only if you're mocking a node module do you put it at the root level (or the level at which your node modules folder is located)

In terms of the debugging:
Cannot read property 'then' of undefined means that in the case of something.then(...), the something is undefined, the only thing in user that did .then was after the request so I knew there was an issue with making the request....

In addition there was this console.log line which wasnt being printed so I knew that request wasnt being mocked properly

Was this page helpful?
0 / 5 - 0 ratings