-
-
Notifications
You must be signed in to change notification settings - Fork 843
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
react-native-mapbox-gl/maps/setup-jest not working as expected #998
Comments
I just set this up a week or two ago and ran into the first issue until I added the transformIgnorePatterns bit. It sounds like maybe we need to update our docs to include that in the jest setup. For the other issue, I don't remember seeing that particular error. Could you post an example of the code for a failing unit test and its setup? Maybe a stack trace so we can see exactly where it is failing? I added this to a file in my mocks folder as well, but I'm not sure if it will help fix your problem. You may have other parts of the code that you are using that you need to add to this as well, but even then I'm not sure that it will fix it for you:
|
hey @HughBoert, sorry for the late reply. I've set up a new react-native project and fiddled a bit with it. I'm not sure what jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter.js', () => {
function MockEventEmitter() {}
MockEventEmitter.prototype.addListener = jest.fn(() => ({remove: jest.fn()}));
MockEventEmitter.prototype.removeListener = jest.fn();
return MockEventEmitter;
}); let me know how that works out for you Thanks 🙇 |
I can verify that this happens on the latest version:
|
Thanks for the comment @agabriele-radius. |
This fails on an empty Here's a sample repo I set up to prove this There's a patch as per @ferdicus's comment above on the b/patches branch This sample repo also fails build on ios as per https://github.com/react-native-mapbox-gl/maps/issues/1097 |
I have the same issue. It would be good to know if there's a solution in the horizon |
This fixed the issue for me. Thanks for posting @ferdicus |
PRs are welcome :) |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hi, everyone!
This is a fresh RN project where I added Edited: complete
|
@matias91, I believe this is still the same issue. You have to mock it in order to run the tests properly. |
Thanks for the answer, @ferdicus! |
Just to be clear, this: jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter.js', () => {
function MockEventEmitter() {}
MockEventEmitter.prototype.addListener = jest.fn(() => ({remove: jest.fn()}));
MockEventEmitter.prototype.removeListener = jest.fn();
return MockEventEmitter;
}); is supposed to mock it |
@ferdicus, sorry for my late answer. |
@matias91 , @agabriele-radius , @ElZombieIsra However, I was able to mock it successfully by adding an extra setup file for jest like this: // jest-setup.js
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter.js', () =>
require('react-native/Libraries/EventEmitter/__mocks__/NativeEventEmitter.js')
); then simply add this to your jest config in "setupFilesAfterEnv": [
"@react-native-mapbox-gl/maps/setup-jest",
"<rootDir>/jest-setup.js"
] Can you try and let me know if that helps? Thanks |
@ferdicus Thank you very much! That mock worked for me! |
I was running into a issue that was blocking me to archive a great test coverage in components that uses a in cases that we have <Mapbox.MapView testID="mapview">
<View testID="view1" />
<View testID="view2" />
<View testID="view3" />
<MapBox.ETC testID="etc" />
</Mapbox.MapView> if we try the result will be a empty array always, as if none of the children existed, of course you also won't be able to catch any of the children by the id like
for now I create this workaround that solved the problem jest.mock('@react-native-mapbox-gl/maps', () => {
const MapBox = jest.requireActual('@react-native-mapbox-gl/maps');
const {View} = jest.requireActual('react-native');
MapBox.default.MapView = jest
.fn()
.mockImplementation(({children, ...rest}) => (
<View {...rest}>{children}</View>
));
return MapBox;
}); maybe some simpler solution could be implemented and sent in a pull request to |
I followed the instructions to setup jest tests for react-native-mapbox-gl (adding '@react-native-mapbox-gl/maps/setup-jest' to setupFilesAfterEnv), but it destroys hundreds of tests written before.
I get
SyntaxError: Unexpected token {
on every test.After adding mapbox-gl to transformIgnorePatterns in the jest config, half of the tests in the project are still failing with the exception
Right-hand side of 'instanceof' is not an object
.When I flip the load order of the setupTests.js file and '@react-native-mapbox-gl/maps/setup-jest' I am getting errors
Invariant Violation: Native module cannot be null
regarding every test which targets components importing MapBoxGL.It should not destroy my tests, nor break tests regarding the components importing MapboxGL when you do the jest setup shown in docs.
The package.json:
The jest.config.js:
The App was once an expo app that then got ejected. I do not know if this might cause any problems.
Or eventually there are known conflicts regarding my versions in package.json?
The text was updated successfully, but these errors were encountered: