Búsqueda de sitios web

reactjs - Simulando react-native-keychain, undefined no es una función


Estoy intentando escribir una prueba para mis funciones auxiliares, que utilizan la biblioteca react-native-keychain.

De acuerdo con la documentación, simulé la biblioteca.

Agregué un directorio react-native-keychain al directorio __mocks__ y añadí el siguiente código:

const keychainMock = {  
SECURITY_LEVEL_ANY: 'MOCK_SECURITY_LEVEL_ANY',
SECURITY_LEVEL_SECURE_SOFTWARE: 'MOCK_SECURITY_LEVEL_SECURE_SOFTWARE',
SECURITY_LEVEL_SECURE_HARDWARE: 'MOCK_SECURITY_LEVEL_SECURE_HARDWARE',
setGenericPassword: jest.fn().mockResolvedValue(),
getGenericPassword: jest.fn().mockResolvedValue(),
resetGenericPassword: jest.fn().mockResolvedValue(),
};
export default keychainMock;

Mi función auxiliar llama al método getGenericPassword.

{
getGenericPassword().then(genericPassword => {
  if (genericPassword) {
    setUser({jwt: genericPassword.password, authorities: []});
  }
});
}

y mi prueba se ve así:

describe('AuthProvider', () => {

test('Snapshot', () => {
const component = renderer.create(
  <AuthProvider>
    <MockComponent />
  </AuthProvider>,
);

expect(component.toJSON()).toMatchSnapshot();
});

Obtengo el siguiente error al ejecutar las pruebas:

TypeError: (0 , _reactNativeKeychain.getGenericPassword) is not a function

Package.json:

"react": "17.0.2",
"react-native": "0.68.1",
"react-native-keychain": "^8.0.0",
"jest": "26.6.3",

Entonces los mocks parecen funcionar ya que no está llamando al método real, pero el jest.fn() tampoco.

¿Qué me estoy perdiendo?

Artículos relacionados