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

Unable to run Jest and V2 with reactNative version 0.73.6 #1729

Closed
dmregister opened this issue Apr 12, 2024 · 3 comments
Closed

Unable to run Jest and V2 with reactNative version 0.73.6 #1729

dmregister opened this issue Apr 12, 2024 · 3 comments
Assignees
Labels
bug Something isn't working. It's clear that this does need to be fixed.

Comments

@dmregister
Copy link

dmregister commented Apr 12, 2024

When running our jest test after upgrading our React Native version to .73, all the tests are failing with:

ReferenceError: self is not defined

    > 1 | import { Realtime } from 'ably';

On react-native version .70, it seems like it was importing the nodejs version rather than the react-native build. This is probably due to RN now supporting exports from the package.json. The quick fix is to force jest to import the nodejs version e.g.

moduleNameMapper: {
    ably: require.resolve('ably'),
  },

but this is not ideal. Our jest config is using the react-native preset.

Using the example file and applying the attached patch will result in the same error:
https://github.com/ably-labs/ably-js-react-native-example
jest_error_ably.patch

┆Issue is synchronized with this Jira Task by Unito

@ttypic ttypic added the bug Something isn't working. It's clear that this does need to be fixed. label Apr 12, 2024
@ttypic
Copy link
Contributor

ttypic commented Apr 12, 2024

Thank you very much for such a detailed report; we are looking into this.

@VeskeR
Copy link
Contributor

VeskeR commented Apr 17, 2024

Hello @dmregister . Thank you for reporting this!

We identified the issue with our React Native bundle, which used the self property for the global object in the UMD wrapper. This property is not available in the Node environment, in which Jest is configured to run by default with the 'react-native' preset. This has been fixed in #1738 and will be included in the next ably-js patch version release.

That being said, upon further testing, I encountered other obstacles when running jest tests for a React Native app using the non-mocked ably-js. Our React Native bundle relies on various browser APIs to function (WebSocket and XMLHttpRequest, for example). However, the Node environment in which Jest runs its tests does not load any DOM or browser APIs by design, leading to errors like ReferenceError: WebSocket is not defined when attempting to run tests.

Mocking these browser APIs can become complex quickly. Therefore, I would suggest either mocking the ably client or using your workaround to force Jest to import the node.js ably-js bundle:

moduleNameMapper: {
    ably: require.resolve('ably'),
},

You can also force Jest to resolve the exports field using the node condition for all imported bundles (although this seems like the least ideal solution) in jest.config.js:

testEnvironmentOptions: {
    customExportConditions: ['node'],
},

@VeskeR
Copy link
Contributor

VeskeR commented May 1, 2024

ReferenceError: self is not defined error when running Jest tests in React Native was fixed in v2.0.3 ably-js release.

Since ably-js requires certain browser APIs to function correctly, and the Jest testing environment for React Native does not provide them, some additional workarounds are required to make it work. See the comment above for more details.

Closing this as completed.

@VeskeR VeskeR closed this as completed May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. It's clear that this does need to be fixed.
Development

No branches or pull requests

3 participants