react-native-reanimated icon indicating copy to clipboard operation
react-native-reanimated copied to clipboard

Fix Jest 28+ `expect.extend is not a function` error

Open yusufyildirim opened this issue 2 years ago • 9 comments

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:

Ekran Resmi 2022-09-10 15 00 33

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.

yusufyildirim avatar Sep 10 '22 12:09 yusufyildirim

Thanks for the changes.

parasharrajat avatar Sep 13 '22 15:09 parasharrajat

can we merge this?

finalight avatar Sep 15 '22 09:09 finalight

Would love to see get it merged. Need to run our tests

kesha-antonov avatar Sep 15 '22 09:09 kesha-antonov

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?

finalight avatar Sep 15 '22 09:09 finalight

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

finalight avatar Sep 15 '22 09:09 finalight

Closed accidentally.

yusufyildirim avatar Sep 16 '22 15:09 yusufyildirim

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

You should have @jest/globals package if you're Jest 28+ AFAIK. How's that possible?

yusufyildirim avatar Sep 16 '22 15:09 yusufyildirim

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.

backlands avatar Sep 16 '22 17:09 backlands

@piaskowyk can you please review this PR? it would be nice to merge it soon to make it work with Jest 28+

fabrianibrahim avatar Oct 05 '22 02:10 fabrianibrahim

It would be great if this can be released with 2.x

parasharrajat avatar Oct 16 '22 17:10 parasharrajat

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?

piaskowyk avatar Oct 28 '22 08:10 piaskowyk

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.

backlands avatar Oct 31 '22 15:10 backlands

Thank you all so much for the help! 😍 I will port it to Reanimated2 branch also.

piaskowyk avatar Nov 02 '22 12:11 piaskowyk