I have a module using react-native-sound
in the following ways:
const Sound = require('react-native-sound');
...
const something = Sound.DOCUMENT;
const someOtherThing = new Sound();
How do I mock such a module?
I have a module using react-native-sound
in the following ways:
const Sound = require('react-native-sound');
...
const something = Sound.DOCUMENT;
const someOtherThing = new Sound();
How do I mock such a module?
I've mocked react-native-sound using a manual mock (in the __mocks__ folder) which looks like this:
Note how methods on the Sound import are added directly (
Sound.setCategory
), and methods on instances of the class (play
,getDuration
etc.) are added using the prototype.There's a little added complexity that you may not need using the
mockFunctions
export. I use that to check for calls to the mocked functions by importing it separately into the test file like