Reputation: 1945
Is it possible to use jest in react-native
projects, which use components from react-native-elements
?
The example jest file https://github.com/vanGalilea/react-native-testing/blob/master/tests/Login.test.tsx works fine as it is. As soon as I change the <Text>
component from the react-native
version to the react-native-elements
version, I get the following error message.
● 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
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
Environment:
Upvotes: 4
Views: 2366
Reputation: 119
Try using this mock
import .....
jest.mock('@rneui/themed', ()=>({
Button: jest.fn()
}));
describe( ... )
Upvotes: 3
Reputation: 569
● 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 version 0.67.2 to fix mockModal issue. Here is the update from react-native: https://github.com/facebook/react-native/blob/main/CHANGELOG.md#fixed
Issue: https://github.com/facebook/react-native/issues/32939
Upvotes: 1
Reputation: 1945
A temporary workaround is to downgrade react-native to 0.66.4
. This seems to fix the problem.
Update 2022-01-21:
The react-native-elements
development team acknowledged the problem. Moreover, they have already fixed it in the beta version 4.0.0-beta.0
. Check the following issue for the latest updates on this bug.
https://github.com/react-native-elements/react-native-elements/issues/3304
Upvotes: 0