SelfLender / react-native-biometrics

React Native module for iOS and Android biometrics
MIT License
664 stars 228 forks source link

How to create a mock of this module for testing with Jest? #211

Closed arthedza closed 2 years ago

arthedza commented 2 years ago
jest.mock('react-native-biometrics', () => {
  return jest.fn().mockImplementation(() => ({
    simplePrompt: jest.fn(() =>
      Promise.resolve({
        success: true,
        error: undefined,
      })
    ),
    isSensorAvailable: jest.fn(() =>
      Promise.resolve({
        available: true,
        biometryType: 'TouchID',
        error: undefined,
      })
    ),
  }));
});
akkadaya commented 2 years ago

Still not working!

for this test:

expect(new ReactNativeBiometrics().simplePrompt).toHaveBeenCalledWith({ success: true })

I'm getting:

Error: expect(received).toHaveBeenCalledWith(...expected)

Matcher error: received value must be a mock or spy function

Received has type:  function
Received has value: [Function anonymous]
akkadaya commented 2 years ago

Hi @arthedza, are tests working for you?

by the way, when testing, if I import from:

import ReactNativeBiometrics, { BiometryType } from 'react-native-biometrics'

I get:

TypeError: react_native_biometrics_1.default is not a constructor

therefore, I have to import from:

import ReactNativeBiometrics, { BiometryType } from 'react-native-biometrics/index'