我正在尝试设置我的反应本机测试环境react-native-testing-library https://github.com/callstack/react-native-testing-library和笑话。我的反应本机应用程序使用react-native-encrypted-storage
。当我运行第一个示例测试(下面的代码)时,它失败了RNEcryptedStorage
未定义。
import React from "react";
import "react-native";
// Note: test renderer must be required after react-native.
import renderer from "react-test-renderer";
import App from "../App";
it("renders correctly", () => {
console.log("Rendering");
renderer.create(<App />);
});
完整错误:
RNEncryptedStorage 未定义
在对象。 (node_modules/react-native-encrypted-storage/lib/commonjs/EncryptedStorage.ts:7:9)
在对象。 (node_modules/react-native-encrypted-storage/lib/commonjs/index.ts:1:1)
这是我第一次设置测试环境,所以不确定从哪里开始解决这个问题。
上面的方法是可行的,但是如果你模拟react-native的其他方面,它可能会给其他模拟带来问题。如果您想单独模拟 RNEncryptedStorage,您可以尝试对上述解决方案进行细微的修改:
__mocks__/react-native-encrypted-storage/index.js
const RNEncryptedStorage = {
setItem: jest.fn(() => Promise.resolve()),
getItem: jest.fn(() => Promise.resolve('{ "foo": 1 }')),
removeItem: jest.fn(() => Promise.resolve()),
clear: jest.fn(() => Promise.resolve()),
};
export default RNEncryptedStorage;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)