Unit testing 为什么用jest.mock模拟世博会时@expo/vector图标未定义?

Unit testing 为什么用jest.mock模拟世博会时@expo/vector图标未定义?,unit-testing,react-native,jestjs,expo,Unit Testing,React Native,Jestjs,Expo,对于带有jest的单元测试,我用以下内容模拟了expo目录: jest.mock('expo', () => { const { View } = require('react-native'); const constantsMock = { // the camera class has constants defined on it Type: { back: 'BACK', front: 'FRONT', }, }; cons

对于带有jest的单元测试,我用以下内容模拟了expo目录:

jest.mock('expo', () => {
  const { View } = require('react-native');
  const constantsMock = { // the camera class has constants defined on it
    Type: {
      back: 'BACK',
      front: 'FRONT',
    },
  };
  const cameraMock = Object.assign({}, View, { Constants: constantsMock }); // assign so we can modify a copy, not the orig View - since its passed by reference

  return {
    Permissions: {
      askAsync: jest.fn(),
    },
    Camera: cameraMock,
    CameraObject: View,
  };
});
这起作用-但导致react记录以下错误:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

    Check the render method of `InKeyboardCamera`.
        in InKeyboardCamera
通过消除过程,即取出渲染组件的一部分并检查何时抛出以及何时未抛出,我发现产生此错误的组件如下:

(<SimpleLineIcons name="refresh" size={19} color="white" />)
这很奇怪,因为我没有模仿@expo/vector图标模块,它是在现场工作的,但不是在测试环境中。但是可以肯定的是,在该文件中注销console.logSimpleLineIcons会导致未定义的错误

我能够消除以下错误:

jest.mock('@expo/vector-icons', () => {
  const { View } = require('react-native');
  return {
    SimpleLineIcons: View,
    Ionicons: View,
  };
});

但它留下了一个问题:为什么嘲笑世博会包会影响@expo/vector icons包?

世博会基本上有一个SDK32内置的“世博会矢量图标”模块。这就是为什么在通过世博会创建项目时不需要单独安装“世博矢量图标”模块的原因,单独安装这些模块可能会导致冲突

通过查看预设文件中忽略的转换模式可以理解

jestPreset.transformIgnorePatterns=[ '节点|模块/?!jest-?反应本机|反应克隆引用元素|@反应本机社区|指数|@指数?/.*|反应导航|@反应导航/*.@unimodules/*|哨兵博览会|本机基础|反应本机svg', ];
jest.mock('@expo/vector-icons', () => {
  const { View } = require('react-native');
  return {
    SimpleLineIcons: View,
    Ionicons: View,
  };
});