Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Can't mock custom module #61

Open
chapati23 opened this issue Jan 6, 2017 · 3 comments
Open

Can't mock custom module #61

chapati23 opened this issue Jan 6, 2017 · 3 comments

Comments

@chapati23
Copy link

chapati23 commented Jan 6, 2017

My Module:

// ./App/Services/MyBridge
import { NativeModules } from 'react-native'
export const MyBridge = NativeModules.MyBridge
export default MyBridge

My Mock:

// ./testSetup.js
mockery.enable()
mockery.registerAllowable('./App/Services/MyBridge', {
  nativeCall: () => {}
})

mockery.registerMock('./App/Services/MyBridge', {
  nativeCall: () => {}
})

Expected Behavior
MyBridge.nativeCall is mocked

Actual Behavior
Error message during test run Error: Cannot read property 'nativeCall' of undefined

I can't, for the life of me, figure this out. Mocking 3rd party modules works fine.
Any ideas? (mockery@2.0.0)

@chapati23
Copy link
Author

Found a workaround. In case anyone else runs into this:

import mockery from 'mockery'
import * as rn from 'react-native'

mockery.registerMock('react-native', {
  ...rn,
  NativeModules: {
    MyBridge: {
      nativeCall: () => {}
    }
  }
})

This feels a bit dirty though. So leaving this open to wait for some feedback if there's a better way to do it.

@RealOrangeOne
Copy link

@chapati23 it's not really clear from your snippets, but remember the first arg of registerMock has to be exactly what's inside require(), not just a path to the file you want to mock. Are you doing this?

@chapati23
Copy link
Author

@RealOrangeOne not sure i understand. in this case i'm mocking the actual react-native npm package so it does work like it's in the snippet

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants