1

I want to running jest to test my component, I'm using React Native Testing Library to test and React Native Web to create an example. Whenever I tried to run jest I facing an error like this:

● Test suite failed to run

    Cannot find module '../Utilities/Platform' from 'node_modules/react-native/Libraries/StyleSheet/processColor.js'

    Require stack:
      node_modules/react-native/Libraries/StyleSheet/processColor.js
      node_modules/react-native/Libraries/Components/View/ReactNativeStyleAttributes.js
      node_modules/react-native/Libraries/StyleSheet/StyleSheet.js
      node_modules/react-native/index.js
      node_modules/@testing-library/react-native/build/helpers/host-component-names.js
      node_modules/@testing-library/react-native/build/fire-event.js
      node_modules/@testing-library/react-native/build/pure.js
      node_modules/@testing-library/react-native/build/index.js
      packages/snackbar/__tests__/snackbar.test.js


    However, Jest was able to find:
        '../Utilities/Platform.android.js'
        '../Utilities/Platform.d.ts'
        '../Utilities/Platform.flow.js'
        '../Utilities/Platform.ios.js'
        '../Utilities/Platform.js.flow'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node'].

    See https://jestjs.io/docs/configuration#modulefileextensions-arraystring

      56 |             React.createElement(Text, { style: [{ color, fontWeight: '500' }, labelStyle] }, label)))));
      57 | };
    > 58 | const styles = StyleSheet.create({
         |                ^
      59 |     SnackbarText: {
      60 |         flex: 1,
      61 |         padding: 16,

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
      at Object.require (node_modules/react-native/Libraries/StyleSheet/processColor.js:15:18)
      at Object.require (node_modules/react-native/Libraries/Components/View/ReactNativeStyleAttributes.js:14:1)
      at Object.require (node_modules/react-native/Libraries/StyleSheet/StyleSheet.js:28:36)
      at Object.require [as StyleSheet] (node_modules/react-native/index.js:304:12)
      at Object.StyleSheet (packages/snackbar/dist/Snackbar.js:58:16)
      at Object.require (packages/snackbar/dist/SnackbarProvider.js:4:1)
      at Object.require (packages/snackbar/dist/index.js:1:1)
      at Object.require (packages/snackbar/__tests__/snackbar.test.js:3:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.12 s
Ran all test suites.

I've tried to google how to fix it before asking here, but all I found is this article https://dev.to/miguelniblock/jest-for-native-base-on-nextjs-with-react-native-web-34b8 and I don't know how to fix it based on that article.

jest.config.js

const snackbarPackage = require('./packages/snackbar/package.json');

module.exports = {
  verbose: true,
  preset: 'react-native',
  // testEnvironment: 'node',
  setupFilesAfterEnv: ['./jest.setup.js'],
  collectCoverage: true,
  coverageReporters: ['html'],
  moduleNameMapper: {
    '@rn-flix/snackbar': '<rootDir>/packages/snackbar/dist/index.js',
  },
  projects: [
    {
      displayName: snackbarPackage.name,
      testMatch: ['<rootDir>/packages/snackbar/**/*.test.js'],
      transformIgnorePatterns: [
        '<rootDir>/node_modules/(?!(jest-)?react-native|@react-native)',
        // '<rootDir>/packages/snackbar/src/',
      ],
      globals: {
        __DEV__: true,
      },
      modulePathIgnorePatterns: ['<rootDir>/packages/snackbar/example'],
      collectCoverageFrom: ['<rootDir>/packages/snackbar/src/**/*.{js,jsx}'],
    },
  ],
};

anyone know how to fix the issue?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.