描述
我正在使用 react-native-reanimated 并且在测试时,useSharedValue 会抛出一个错误,即在使用 jest 和 react-native-testing-library 时它不是函数。我通过使用作为包的一部分提供的模拟来模拟 react-native-reanimated。我还注意到 VS Code 突出显示 reanimated 没有导出成员 useSharedValue、useAnimatedStyle 和 withTiming,但是该组件仍然可以正常运行而没有任何错误。
有谁知道需要做什么才能正确模拟它?
代码
用于查看组件是否呈现的测试
import React from "react";
import { render } from "react-native-testing-library";
import { Switch } from "../src/native/Switch";
describe("Switch Package", () => {
it("renders", () => {
render();
});
});
放置在名为 react-native-reanimated.js 的模拟中的模拟文件
jest.mock("react-native-reanimated", () => require("react-native-reanimated/mock") );
使用 Reanimated 的组件 - Switch.tsx
import { Button } from 'react-native';
import {
useSharedValue,
useAnimatedStyle,
withTiming,
Easing,
} from 'react-native-reanimated';
function Switch() {
const opacity = useSharedValue(0);
const style = useAnimatedStyle(() => {
return {
opacity: withTiming(opacity.value, {
duration: 200,
easing: sing.out(Easing.cubic),
}),
};
});
return (
<Animated.View style={[{width: 100, height: 100, backgroundColor: 'green'}, style]} />
<Button onPress={() => (opacity.value = 1)} title="Hey" />
);
}
包版本
React:16.11.0 React Native:.062.2 React Native Reanimated:2.0.0-alpha.3
截图