How to mock a module exporting a class with class

2019-08-21 01:49发布

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?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-21 02:45

I've mocked react-native-sound using a manual mock (in the __mocks__ folder) which looks like this:

const isFakeFilename = filename => /^blah.*/.test(filename);

const mockFunctions = {
  play: jest.fn(cb => {
    console.log('*** Playing sound! ***');
    cb && cb(isFakeFilename(this.filename) ? false : true);
  }),
  setCategory: jest.fn(),
  getDuration: jest.fn(() => 100),
  getNumberOfChannels: jest.fn(() => 8)
};

const Sound = function(filename, blah2, cb) {
  this.play = mockFunctions.play.bind(this);
  this.filename = filename;
  const savedFilename = filename;
  setTimeout(() => {
    if (isFakeFilename(savedFilename)) {
      cb && cb(new Error('File does not exist! (mocked condition)'));
    } else {
      cb && cb();
    }
  });
};

Sound.prototype.play = mockFunctions.play.bind(Sound.prototype);
Sound.prototype.getDuration = mockFunctions.getDuration;
Sound.prototype.getNumberOfChannels = mockFunctions.getNumberOfChannels;

Sound.setCategory = mockFunctions.setCategory;

export default Sound;
export { mockFunctions };

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

import { mockFunctions } from 'react-native-sound';
// ...
expect(mockFunctions.play).toHaveBeenCalled();
查看更多
登录 后发表回答