如何用jest模拟react-native模块(不是第三方模块)

rco*_*rie 7 jestjs react-native

我正在尝试模拟一个带有react-native (而不是第三方模块)的模块,例如LayoutAnimation:

import * as RN from 'react-native'

RN.LayoutAnimation = jest.fn()
Run Code Online (Sandbox Code Playgroud)

但测试失败了:

TypeError: Cannot read property 'decelerationRate' of undefined

  at Object.<anonymous> (node_modules/react-native/Libraries/Components/WebView/WebView.ios.js:555:3254)
  at Object.get WebView [as WebView] (node_modules/react-native/Libraries/react-native/react-native-implementation.js:73:22)
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以模拟/存根RN模块,例如LayoutAnimation或任何其他本地(而非第三方)模块?

Ant*_*amp 11

尽量做到 jest.mock('LayoutAnimation');

  • 在文件package.json中,您有:`“ jest”:{“ preset”:“ react-native”}`。所以,是的,开玩笑知道原生API (2认同)