react-native-reanimated
react-native-reanimated copied to clipboard
Fix Jest 28+ `expect.extend is not a function` error
Description
Reanimated is not fully compatible with Jest 28+ even after the PR https://github.com/software-mansion/react-native-reanimated/pull/3347. Using it with Jest 28+ leads to this error:

The reason is, the "expect" module's export approach has changed starting from Jest 28. It used to export the expect
function directly, as can be seen here.
This means, requiring the "expect" module like this
const expect = require('expect')
was providing direct access to the expect
function alongside with its attached methods, like extend
.
This has changed with Jest 28. export default expect
is the new export statement, which means requiring the "expect" package returns the module object, instead of the function.
https://github.com/software-mansion/react-native-reanimated/pull/3347 PR attempts to fix this error by assuming "expect" package should throw an error with Jest 28 and it can catch that and require the "expect" from the @jest/globals
package for Jest 28+ project but it's not the case in reality. "expect" package is still shipped with Jest 28, so, requiring it doesn't throw an error as expected in the PR.
By knowing the export difference, I checked if the value of the module is object or not after requiring it. If it's an object, it means user is on Jest 28+ codebase. The rest is the same with the previous PR. I require the "expect" function from @jest/globals
and use it.
Tested on Jest 29 and Jest 27.5.1
Fixes #3553 and #3215.
Thanks for the changes.
can we merge this?
Would love to see get it merged. Need to run our tests
i tried to patch-package but i still have the same error
● Test suite failed to run
TypeError: expect.extend is not a function
my codebase has a mix of javascript and typescript, is there anything else I need to patch?
i did a further debugging
i log the value of this particular line
const { expect: expectModule } = require('@jest/globals');
expectModule
returns undefined in this case
and since it's undefined, assigning it to the variable expect
gives a undefined as well
Closed accidentally.
i did a further debugging
i log the value of this particular line
const { expect: expectModule } = require('@jest/globals');
expectModule
returns undefined in this caseand since it's undefined, assigning it to the variable
expect
gives a undefined as well
You should have @jest/globals
package if you're Jest 28+ AFAIK. How's that possible?
I was able to test this patch in my react-native project with Jest and Jest-Native and everything is working once I also updated to the setupFilesAfterEnv
value.
The error received by @finalight appears to occur if you have not migrated from using the setupFile
to using setupFilesAfterEnv
as outlined in the documentation here for Jest 28 and older.
@piaskowyk can you please review this PR? it would be nice to merge it soon to make it work with Jest 28+
It would be great if this can be released with 2.x
Hey 👋 We have two similar PR with a fix, this one and #3583. The #3583 seems more complex, could someone confirm if #3583 fixes your problem?
Hey wave We have two similar PR with a fix, this one and #3583. The #3583 seems more complex, could someone confirm if #3583 fixes your problem?
I did as requested and have tested #3583 as well as retested this PR and both work but further notes about #3583 are added to that PR for your review.
Thank you all so much for the help! 😍 I will port it to Reanimated2 branch also.