Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 3, 2022

Conversation

yusufyildirim
Copy link
Contributor

Description

Reanimated is not fully compatible with Jest 28+ even after the PR #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.

#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.

@parasharrajat
Copy link

Thanks for the changes.

@finalight
Copy link

can we merge this?

@kesha-antonov
Copy link

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

@finalight
Copy link

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
Copy link

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

@yusufyildirim
Copy link
Contributor Author

Closed accidentally.

@yusufyildirim yusufyildirim reopened this Sep 16, 2022
@yusufyildirim
Copy link
Contributor Author

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?

@backlands
Copy link

backlands commented Sep 16, 2022

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.

@fabrianibrahim
Copy link

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

@parasharrajat
Copy link

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

@piaskowyk
Copy link
Member

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?

@backlands
Copy link

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.

@piaskowyk
Copy link
Member

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

@piaskowyk piaskowyk merged commit 90d91de into software-mansion:main Nov 3, 2022
piaskowyk pushed a commit that referenced this pull request Nov 3, 2022
## Description
Reanimated is not fully compatible with Jest 28+ even after the PR #3347. Using it with Jest 28+ leads to this error:

<img width="796" alt="Ekran Resmi 2022-09-10 15 00 33" src="https://user-images.githubusercontent.com/22980987/189482337-cc2655f6-9463-4a05-9998-5fc3592f5cc0.png">

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](https://github.com/facebook/jest/blob/v27.5.1/packages/expect/src/index.ts#L441). 

This means, requiring the "expect" module like this

```ts
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](https://github.com/facebook/jest/blob/v28.0.0/packages/expect/src/index.ts#L449). `export default expect` is the new export statement, which means requiring the "expect" package returns the module object, instead of the function.

#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.
geriux pushed a commit to wordpress-mobile/react-native-reanimated that referenced this pull request Jan 25, 2023
…n#3559)

Reanimated is not fully compatible with Jest 28+ even after the PR software-mansion#3347. Using it with Jest 28+ leads to this error:

<img width="796" alt="Ekran Resmi 2022-09-10 15 00 33" src="https://user-images.githubusercontent.com/22980987/189482337-cc2655f6-9463-4a05-9998-5fc3592f5cc0.png">

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](https://github.com/facebook/jest/blob/v27.5.1/packages/expect/src/index.ts#L441).

This means, requiring the "expect" module like this

```ts
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](https://github.com/facebook/jest/blob/v28.0.0/packages/expect/src/index.ts#L449). `export default expect` is the new export statement, which means requiring the "expect" package returns the module object, instead of the function.

software-mansion#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 software-mansion#3553 and software-mansion#3215.
fluiddot pushed a commit to wordpress-mobile/react-native-reanimated that referenced this pull request Jun 5, 2023
…n#3559)

## Description
Reanimated is not fully compatible with Jest 28+ even after the PR software-mansion#3347. Using it with Jest 28+ leads to this error:

<img width="796" alt="Ekran Resmi 2022-09-10 15 00 33" src="https://user-images.githubusercontent.com/22980987/189482337-cc2655f6-9463-4a05-9998-5fc3592f5cc0.png">

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](https://github.com/facebook/jest/blob/v27.5.1/packages/expect/src/index.ts#L441). 

This means, requiring the "expect" module like this

```ts
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](https://github.com/facebook/jest/blob/v28.0.0/packages/expect/src/index.ts#L449). `export default expect` is the new export statement, which means requiring the "expect" package returns the module object, instead of the function.

software-mansion#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 software-mansion#3553 and software-mansion#3215.
js-jslog pushed a commit to js-jslog/harpguru that referenced this pull request Aug 21, 2023
And then reanimated became incompatible due to
software-mansion/react-native-reanimated#3559
which then only required an upgrade to that package.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Won't build Jest issue
7 participants