question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't run unit tests with Jest

See original GitHub issue

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 />);
  });
});

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:10
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

20reactions
jsvegborncommented, Jan 15, 2020

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!

9reactions
dominictraceycommented, Aug 31, 2017

Try:

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting - Jest
Troubleshooting. Uh oh, something went wrong? Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why​.
Read more >
Jest | IntelliJ IDEA Documentation - JetBrains
You can run and debug tests with Jest right in IntelliJ IDEA. You can see the test results in a treeview and easily...
Read more >
Can't run Jest tests from editor in WebStorm - Stack Overflow
I can run Jest via a run configuration and it works fine. However, I can't right-click on a test in the editor and...
Read more >
Can't run unit tests with Jest #244 - GitHub
While running a most simple unit test I get TypeError: Cannot read property 'IsAndroid' of undefined as in issue #36 .
Read more >
vscode-jest - Visual Studio Marketplace
tests with dynamic names (test.each with variables, template-literals, etc.) will not be translated; therefore, they can only be run through ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found