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

Fail to run Jest testing #394

Open
cedricfressin opened this issue Dec 4, 2024 · 4 comments
Open

Fail to run Jest testing #394

cedricfressin opened this issue Dec 4, 2024 · 4 comments
Labels
enhancement New feature or request
Milestone

Comments

@cedricfressin
Copy link

Description

When I run Jest test suites, I got this error, due to usage under the hood of react-native-nitro-modules:

Failed to get NitroModules: The native "NitroModules" Turbo/Native-Module could not be found.
    * Make sure react-native-nitro-modules/NitroModules is correctly autolinked (run `npx react-native config` to verify)
    * Make sure you enabled the new architecture (TurboModules) and CodeGen properly generated the "NativeNitroModules"/NitroModules specs. See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md
    * Make sure you are using react-native 0.75.0 or higher.
    * Make sure you rebuilt the app.
    * Make sure you ran `expo prebuild`.
    * Make sure you ran `pod install` in the ios/ directory.

Is there any Jest preset to use, I haven't find any in this repo/docs or on react-native-nitro-modules

Steps to Reproduce

  1. Create an Expo project
  2. Install and configure react-native-unistyles@beta
  3. Run yarn expo prebuild --clean
  4. Run any test suites with Jest that contains JSX

Snack or Repository Link (Optional)

No response

Unistyles Version

3.0.0-beta.1

React Native Version

0.76.3

Platforms

Android, iOS, Web

Expo

Yes

@jpudysz
Copy link
Owner

jpudysz commented Dec 4, 2024

I don't think there is any. It's also too early for me to invest time in preparing such an example.
I will definitely do it later during the RC phase. Keeping this issue for future reference.

@jpudysz jpudysz added the enhancement New feature or request label Dec 4, 2024
@jpudysz jpudysz added this to the 3.0.0-rc.1 milestone Dec 4, 2024
@rkrv
Copy link

rkrv commented Dec 20, 2024

@jpudysz Any suggestions on getting Jest tests configured with the new Unistyles? I am so close 🤏 to upgrading my project to the new arch, and this is the final blocker that I know of.

@jpudysz
Copy link
Owner

jpudysz commented Dec 20, 2024

Follow errors and try to mock the structure. If you get error from UnistylesRuntime then create empty UnistylesRuntime object and so on.

@rkrv
Copy link

rkrv commented Dec 20, 2024

Ended up with this:

// jest.config.js
module.exports = {
  moduleNameMapper: { 'react-native-unistyles': '<rootDir>/__mocks__/react-native-unistyles.js' },
  setupFiles: ['<rootDir>/jest.setup.js'],
  // ...
};
// jest.setup.js
import './unistyles'; // <-- File that initializes Unistyles
// __mocks__/react-native-unistyles.js
// You'll want to edit this mock to fit your use case
let theme;

module.exports = {
  UnistylesRuntime: {
    hasAdaptiveThemes: false,

    setTheme: (newTheme) => {
      theme = newTheme;
    },
  },

  StyleSheet: {
    configure: (config) => {
      theme = config.themes.light;
    },

    create: (styles) => {
      if (typeof styles === 'function') {
        return styles(theme);
      }

      return styles;
    },
  },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants