Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't run unit tests with Jest #245

Open
andrekovac opened this issue Aug 17, 2017 · 7 comments
Open

Can't run unit tests with Jest #245

andrekovac opened this issue Aug 17, 2017 · 7 comments
Labels

Comments

@andrekovac
Copy link

While running a most simple unit test I get TypeError: Cannot read property 'IsAndroid' of undefined as in issue #36 . However, building and running the app works perfectly fine.

Mocking react-native-sound via jest.mock('react-native-sound'); doesn't help.

The test fails in line import myComponentWithANestedSubComponentWhichUsesRNSound from './'; of the following code snippet:

import 'react-native';
import React from 'react';
import renderer from 'react-test-renderer';

jest.mock('react-native-sound');

import myComponentWithANestedSubComponentWhichUsesRNSound from './';

describe('<ListItemVideo />', () => {

  it('renders correctly', () => {
    const tree = renderer.create(<myComponentWithANestedSubComponentWhichUsesRNSound />);
  });
});

@dominictracey
Copy link

Try:

jest.mock('react-native-sound', () => 'Sound')

@henninghall
Copy link
Contributor

@dominictracey your solution works fine when putting the mock in each test file. Is there any way to put it in a single place or do I have to put it in every test file I create?

@mauron85
Copy link

mauron85 commented Oct 30, 2017

@henninghall I guess you can apply same workaround as for react-native-i18n.

Basically create folder __mocks__ somewhere in your src folder (where jest has access) and create new file react-native-sound.js with:

export default function() {
  return 'Sound';
}

@schumannd
Copy link
Contributor

@mauron85 I dont understand how that is a valid mock. I still get the error.

TypeError: Sound.setCategory is not a function

which makes sense. How do I mock this package correctly in __mocks__?

@jsvegborn
Copy link

If mocked it like:

jest.mock('react-native-sound', () => {
  class SoundMock {
    constructor(path, type, callback) {}
  }

  SoundMock.prototype.setVolume = jest.fn();
  SoundMock.prototype.setNumberOfLoops = jest.fn();
  SoundMock.prototype.play = jest.fn();
  SoundMock.prototype.stop = jest.fn();

  SoundMock.setCategory = jest.fn();

  return SoundMock;
});

and the check for function calls in my tests like

import Sound from 'react-native-sound';
...
expect(Sound.prototype.play).toHaveBeenCalled();

which seems to work for me. Maybe someone else finds it useful!

@shameemkpofficial-git
Copy link

yeah this same error am also faced 2 hrs ago im fixed like:
node_modules>react-native-sound>index.d.ts (open this folder)
export = Sound; (put this command on your end of code in the index.d.ts)
create a new folder on your root (Home) project. the folder named with mocks
inside mocks folder u want to create a fie named react-native-sound.js
inside of the react-native-sound.js you want to code like:
// @flow

class Sound {}

export default Sound;

then save and run your project the error will solve

@haqeem79
Copy link

Try this;

jest.mock('react-native-sound', () => ({
setCategory: jest.fn(),

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

No branches or pull requests

9 participants