You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Jest报错:Cannot find module '../Utilities/Platform'

问题:React Native Jest测试找不到Platform模块

我正在使用React Native Testing Library测试组件,同时用React Native Web创建示例。运行Jest时遇到如下错误:

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

我曾尝试搜索解决方案,找到一篇相关文章但不知道如何根据文章内容修复问题。

我的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}'],
    },
  ],
};

请问有人知道如何解决这个问题吗?


内容的提问来源于stack exchange,提问作者flix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 18:15:55