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

Exported jest mocks use wrong API #327

Closed
myklosbotond opened this issue Mar 18, 2020 · 1 comment
Closed

Exported jest mocks use wrong API #327

myklosbotond opened this issue Mar 18, 2020 · 1 comment
Labels
bug Something isn't working released

Comments

@myklosbotond
Copy link

Environment

System:
    OS: macOS 10.15.3
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 2.70 GB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.14.0 - ~/.asdf/installs/nodejs/12.14.0/bin/node
    Yarn: 1.22.0 - ~/.asdf/shims/yarn
    npm: 6.13.4 - ~/.asdf/installs/nodejs/12.14.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 28
      Build Tools: 28.0.3
      Android NDK: 20.1.5948944
  IDEs:
    Xcode: 11.3.1/11C504 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0 
    react-native: 0.61.5 => 0.61.5

Platforms

neither, issue appears in jest tests

Versions

  • react-native-netinfo: 5.6.2
  • react-native: 0.61.5
  • react: 16.9.0

Description

The mock exported by the package to be used in the jest.setup.js file provides a different API than the package itself.

The mock that is currently exported is

const RNCNetInfoMock = {
    getCurrentState: jest.fn(),
    addListener: jest.fn(),
    removeListeners: jest.fn(),
};

whereas the TypeScript definition file has the following default export:

declare const _default: {
    configure: typeof configure;
    fetch: typeof fetch;
    addEventListener: typeof addEventListener;
    useNetInfo: typeof useNetInfo;
};
export default _default;

Running tests on a component that uses addEventListener gives the following error:

 TypeError: _netinfo.default.addEventListener is not a function

since the mock only provides an addListener function.

If I mock the module out manually with the functions I'm using, it works as expected

jest.mock('@react-native-community/netinfo', () => ({
    addEventListener: jest.fn(),
}));

The current documentation also implies that NativeModules needs to be imported in the jest setup file, although it isn't used.

Reproducible Demo

Create any component that calls addEventListener and write a simple test for it:

component:

import NetInfo from '@react-native-community/netinfo';
import * as React from 'react';

export default class App extends Component {
  constructor() {
    super()
    NetInfo.addEventListener(state => {
    })
  }

  render() {
    return (
      <View>
        <Text>Test</Text>
      </View>
    );
  }
} 

test:

import * as React from 'react';
import * as renderer from 'react-test-renderer';

import App from '../app';

describe('App', () => {
    it('renders correctly', () => {
        const app = renderer.create(<App />);

        expect(app.toJSON()).toMatchSnapshot();
    });
});
@myklosbotond myklosbotond added the bug Something isn't working label Mar 18, 2020
react-native-community-bot pushed a commit that referenced this issue Apr 14, 2020
## [5.7.1](v5.7.0...v5.7.1) (2020-04-14)

### Bug Fixes

* Fix the provided Jest mock so it works with the instructions in the README ([a24ebb1](a24ebb1)), closes [#345](#345) [#327](#327)
@react-native-community-bot
Copy link
Collaborator

🎉 This issue has been resolved in version 5.7.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

No branches or pull requests

2 participants