-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[Question] How i can test my functions with jest? #3035
Comments
I am having a similar problem with imports. `
|
Add it to transformIgnorePattern perhaps - I'm still on v5 but I imagine I'll have to add it as an extra entry in the 'or' part of the pattern match like this (base) mike@kunashir:~/work/Kullki/ksocialscore/packages/public-app (onboarding) % more jest.config.js
module.exports = {
rootDir: './dist/tests/rnapp/js',
roots: ['<rootDir>'],
// transform: {
// '^.+\\.tsx?$': 'ts-jest',
// },
transformIgnorePatterns: [
'node_modules/(?!@react-native-community/google-signin|react-native|reactxp-webview|react-pose-core|animated-pose|@react-native-community/async-storage|@invertase/react-native-apple-authentication)',
],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
preset: 'react-native',
testEnvironment: 'jsdom',
collectCoverage: true,
setupFilesAfterEnv: [
'./__mocks__/mockFirebase.js',
'./__mocks__/mockReactNative.js',
'./__mocks__/mockAsyncStorage.js',
],
};
// module.exports = {
// testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
// }; |
@mikehardy I have a transformIgnorePattern setup like below in the package.json and I'm on v6 {
"scripts": {
"postinstall": "jetify",
"android": "react-native run-android",
"ios": "react-native run-ios",
"web": "expo start --web",
"start": "react-native start",
"test": "jest"
},
"dependencies": {
"@react-native-community/async-storage": "^1.7.1",
"@react-native-firebase/app": "^6.2.0",
"@react-native-firebase/auth": "^6.2.0",
"@react-native-firebase/crashlytics": "^6.2.0",
"@react-native-firebase/firestore": "^6.2.0",
"expo": "~36.0.0",
"expo-asset": "^8.0.0",
"expo-font": "^8.0.0",
"expo-linear-gradient": "^8.0.0",
"expo-location": "^8.0.0",
"expo-permissions": "^8.0.0",
"formik": "^2.0.8",
"geofirestore": "^3.3.1",
"geolib": "^3.2.0",
"haversine": "^1.1.1",
"js-sha256": "^0.9.0",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"moment-duration-format": "^2.3.2",
"native-base": "^2.13.8",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native": "~0.61.4",
"react-native-base64": "^0.0.2",
"react-native-dotenv": "^0.2.0",
"react-native-elements": "^1.2.7",
"react-native-gesture-handler": "~1.5.0",
"react-native-keyboard-aware-scroll-view": "^0.9.1",
"react-native-maps": "^0.26.1",
"react-native-maps-animated-polyline": "^1.0.6",
"react-native-maps-super-cluster": "^1.6.0",
"react-native-reanimated": "~1.4.0",
"react-native-safe-area-context": "0.6.0",
"react-native-safe-area-view": "^1.0.0",
"react-native-scalable-image": "^1.0.0",
"react-native-screens": "~2.0.0-alpha.12",
"react-native-splash-screen": "^3.2.0",
"react-native-svg": "^9.13.6",
"react-native-unimodules": "~0.7.0",
"react-native-web": "~0.11.7",
"react-navigation": "^4.0.10",
"react-navigation-drawer": "^2.2.2",
"react-navigation-header-buttons": "^3.0.4",
"react-navigation-stack": "^1.9.4",
"react-navigation-tabs": "^2.5.6",
"react-redux": "^7.1.1",
"redux": "^4.0.4",
"redux-thunk": "^2.3.0",
"reselect": "^4.0.0",
"throttle-debounce": "^2.1.0"
},
"devDependencies": {
"@babel/core": "~7.6.0",
"babel-jest": "~24.9.0",
"jest": "~24.9.0",
"jest-expo": "^36.0.0",
"jetifier": "~1.6.4",
"metro-react-native-babel-preset": "~0.56.0",
"react-native-svg-transformer": "^0.14.3",
"react-test-renderer": "~16.9.0",
"redux-devtools-extension": "^2.13.8"
},
"jest": {
"projects": [
{
"preset": "jest-expo/ios"
},
{
"preset": "jest-expo/android"
}
],
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|@react-native-firebase/app)"
]
},
"private": true
} |
why just |
@mikehardy I tried and thanks a lot for taking the time to try and help |
Well I have the same problem. Do anyone have a solution? |
Hello 👋, to help manage issues we automatically close stale issues.
|
I still have the same problem. Any updates? Please don't close this |
I also have this problem, and couldn't solve it with the suggested |
I added my
Looking around similar issues it seems like it can be mocked - just gotta figure out how. |
I'm also on v6 and Crashlytics were causing the issue in my case but managed to solve it.
And added the following to my
I actually didn't need the |
This worked for me.
|
Hello 👋, to help manage issues we automatically close stale issues.
|
Facing the same problem with v6 even though I've tried the suggested workarounds. Any thoughts ? |
@januarioprime to fix the problem: Create a // __mocks__/@react-native-firebase/app.js
export default () => ({
currentUser: { idToken: 'mocked-id-token' },
}); // __mocks__/@react-native-firebase/auth.js
export default () => ({
signInWithEmailAndPassword: jest.fn(),
createUserWithEmailAndPassword: jest.fn(),
signOut: jest.fn(),
sendPasswordResetEmail: jest.fn(),
}); For me it has solved the problem. |
Hey all, We use jest testing now on this mono-repo; our config file can be seen here: https://github.com/invertase/react-native-firebase/blob/master/jest.config.js To get JS imports working correctly; the transform ignore pattern @mikehardy mentioned above is probably the way to go. In terms of mocking you could do this via a jest setup file; module.exports = {
// ...
setupFiles: ['./jest.setup.ts'],
// ...
}; For our current testing cases this is what we've done in our Hopefully this helps. |
Well @mikehardy told me that i cannot laught, so i show my problem here. Im still facing the issue
in my setup-jest.js
But im still getting
Idk what more mock |
I dunno, laughing while helping is a great combo. May be something missing still @Salakar how would you stub out the native emitter stuff? Why is that even being called in a jest environment (are there events to emit?) |
My friend @JulioOrellana find the solution to my problem It was necesary create a mock file for every lib used
And in each of those file make the correct export |
I linked above what you need to mock for Native modules e.g. the event emitter; https://github.com/invertase/react-native-firebase/blob/master/jest.setup.ts If you mock the underlying react native native modules rather than the RNFB packages themselves you'll have a much better/accurate mock - or at least in our testing for RNFB itself that was the best way. |
I also did the
for the test
|
Hello 👋, to help manage issues we automatically close stale issues.
|
Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information. |
I am running into the same issue and have been following mikehardy's lead and still get this error: and my package.json entry looks like this: @mikehardy any ideas how to proceed? Garry |
You'll need to mock out RNFBEventEmitter from app as well |
The solution, that works for me:
|
@hrdyjan1 If you want to do a PR to our main jest setup file here react-native-firebase/jest.setup.ts Line 47 in 3eb888b
Then you could just reference it, and everyone would benefit (including yourself as whatever future modules you wanted to mock would also work...) |
The suggested workarounds disable any possibility to actually test the code. What is the purpose of jest tests, if I cannot run them against an emulator? Instead of these mocks, I think one should write the code so that |
even the emulator is a network round trip, it's external to your code and running an actual function on the other side, just not costing you a function execution in the cloud. I believe that's the definition of an integration test. For jest testing react-native-firebase code against functions you do want to mock out the function itself and assert the function was called with expected arguments, and that your code then handled expected return values correctly. To test the actual function code is another thing - that's the firebase node sdk and is a completely different thing, in my project I have that in another package in my monorepo, next to the react-native app but separate except for some shared model typings |
What if a want to mock Firebase/storage? what is the export in this case |
great |
Hi@all,
How i can test my functions with jest? I become follow error:
My jest.config.js
Has anybody a idea? In the documentation i don't found help.
Think
react-native-firebase
is great? Please consider supporting all of the project maintainers and contributors by donating via our Open Collective where all contributors can submit expenses. [Learn More]React Native Firebase
andInvertase
on Twitter for updates on the library.The text was updated successfully, but these errors were encountered: