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.

Using JEST with react-native-elements - mockModal is not a function

See original GitHub issue

The code below works fine if I use <Text> from react-native. If I use the react-native-elements version, I get the following error message.

Code:

import React from "react";
import { render } from "@testing-library/react-native"
import { test } from "@jest/globals"

// import { Text, } from "react-native";
// Using the following statement, instead of the above causes an error!!??
import { Text } from "react-native-elements"; 

const Example = () => {
  	return <Text>Submit</Text>;
};

test("renders correctly", async () => {
  const { getByText } = render(<Example />, ); 
  getByText(/submit/i); 
}) 

1st Error:

  ● Test suite failed to run

    Jest encountered an unexpected token
    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
    By default "node_modules" folder is ignored by transformers.
    Here's what you can do:
...
...
    Details:
    C:\react\testingLibrary\node_modules\react-native-elements\dist\index.js:6
    import Button from './buttons/Button';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

To fix the error above, I added the following line to the jest.config.js file.

transformIgnorePatterns: ['node_modules/(?!(jest-)?@react-native|react-native|react-native-elements/*)',],

This got rid of the error above. However, now, I am getting the following error message.

  ● Test suite failed to run

    TypeError: mockModal is not a function

      at node_modules/react-native/jest/setup.js:116:12
      at Object.<anonymous> (node_modules/react-native/jest/mockModal.js:16:15)
      at node_modules/react-native/jest/setup.js:115:28

How can I fix this error?

My Environment:

software version
react-native 0.67.0
react-native-elements 3.4.2
@testing-library/jest-native 4.0.4
@testing-library/react-native 9.0.0
jest 27.4.7
metro-react-native-babel-preset 0.66.2
react-test-renderer 17.0.2
@babel/core 7.16.7
@babel/runtime 7.16.7
babel-jest 27.4.6

Update:

The problem could be temporarily fixed by downgrading react-native to 0.66.4

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
eliw00dcommented, Jan 21, 2022

Turns out this is actually a regression in 0.67.x and a workaround has been provided there that solves it without the need to upgrade to 4.0.0.beta-0 for me: https://github.com/facebook/react-native/issues/32939#issuecomment-1018660604

1reaction
eliw00dcommented, Jan 20, 2022

I can confirm the same thing after upgrading to 0.67.1. The same tests passed with 0.66.4.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using JEST with react-native-elements - mockModal is not a ...
○ Test suite failed to run TypeError: mockModal is not a function at node_modules/react-native/jest/setup.js:116:12. Update to react-native ...
Read more >
mockModal is not a function · Issue #32939 - GitHub
Use Modal in a component, write a Jest test for that component, get error for mockModal not being a function. Snack, code example,...
Read more >
How To Mock A React Component In Jest | Rob Marshall
This post gives examples of how to simply mock a React component in Jest. It will cover default and named exported components.
Read more >
Jest - TypeError: response.json is not a function-React Native
Coding example for the question Jest - TypeError: response.json is not a function-React Native.
Read more >
Testing - React Native Elements
For Snapshot Testing, we use Jest. You can read more about snapshot testing here. Functional Testing​. Functional tests ensures that a component functions...
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