All Questions
Tagged with react-native jestjs
1,422
questions
167
votes
3
answers
231k
views
how to change jest mock function return value in each test?
I have a mock module like this in my component test file
jest.mock('../../../magic/index', () => ({
navigationEnabled: () => true,
guidanceEnabled: () => true
}));
these ...
134
votes
24
answers
159k
views
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down
I have a component that makes use of Animated component from react native. I started writing a test case to simulate onPress of a component, which calls a function that has Animated.timing in it, and ...
86
votes
4
answers
38k
views
How to determine if JEST is running the code or not?
I am creating a JS test on my react-native project. I'm specifically using firebase for react native, in which I would like to replace firebase instance with a mockfirebase instance if JS is running ...
60
votes
8
answers
48k
views
How do I configure absolute paths for imports in TypeScript based React Native apps?
In order to avoid '../../../../' style relative imports in a TypeScript based React Native app, I would like to configure the app so that I can use absolute imports instead.
It is important that the ...
54
votes
8
answers
66k
views
How to unit test API calls with mocked fetch() in react-native with Jest
In React Native I use fetch to perform network requests, however fetch is not an explicitly required module, so it is seemingly impossible to mock in Jest.
Even trying to call a method which uses ...
53
votes
5
answers
49k
views
jest snapshot testing: how to ignore part of the snapshot file in jest test results
Problem: ignore some part of the .snap file test results
the question here: there are some components in my test that have a random values and i don't really care about testing them. is there any way ...
52
votes
21
answers
43k
views
Mocking platform detection in Jest and React Native
Some of the code I am trying to test detects the platform, using, e.g.:
import { Platform } from 'react-native';
...
if (Platform.OS === 'android') {
...
} else {
...
}
Is there a sensible way ...
52
votes
6
answers
65k
views
How to mock React Navigation's navigation prop for unit tests with TypeScript in React Native?
I'm building a React Native app with TypeScript. For my navigation I use React Navigation and for my unit testing I use Jest and Enzyme.
Here is the (stripped down) code for one of my screen (...
46
votes
3
answers
15k
views
Is it possible to use Jest with multiple presets at the same time?
Is it possible to use Jest with multiple presets, say jsdom and react-native?
I'd like to test a React component that can work both on Web and in a React Native environment. The problem is that the ...
45
votes
2
answers
48k
views
Jest : Mock import of JSON file
I am currently testing one of my React component like this:
it('renders correctly', () => {
const tree = renderer.create(<Scene {...props} />).toJSON();
expect(tree).toMatchSnapshot();
});...
38
votes
3
answers
28k
views
How to resolve "Animated: `useNativeDriver` is not supported because the native animated module is missing." in react-navigation?
I am working on expo SDK Version: 36, and on all platforms (ios/android/web)
When installing react-navigation for the first time as described in the documentation and running jest --watch while doing ...
38
votes
4
answers
18k
views
jest config is throwing "type ErrorHandler = (error: mixed, isFatal: boolean) => void" after update to 26.x
i don't know why this is suddenly not working but this is my jest.config.js:
module.exports = {
preset: 'react-native',
verbose: true,
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', '...
37
votes
7
answers
31k
views
How to test Async Storage with Jest?
I'm building an app with React Native. I want to minimize how often I communicate to the database, so I make heavy use of AsyncStorage. There's a lot of room for bugs in the translation between DB and ...
37
votes
1
answer
78k
views
Jest: How to correctly mock a node module?
I want to mock the node_module 'React Native Keychain' in React Native with Jest.
Following the docs I created a folder called __mocks__ and created a file called react-native-keychain.js in it.
...
30
votes
3
answers
11k
views
How to configure source maps when using Jest within Visual Studio Code debugger
I am writing an app using react native and I want to be able to test my code using the jest framework and use the visual studio code editor debugger to set breakpoints. The problem I am currently ...
28
votes
3
answers
28k
views
How do I move jest tests to a /test folder and get them to run?
edit: I'm pretty sure this question is out of date and Jest has changed since then. Don't assume the answers will work or even be relevant now.
Jest expects tests to be in _tests_ folder or to be ...
27
votes
3
answers
59k
views
Jest TransformIgnorePatterns all node_modules for React-Native Preset
I'm New to jest,
After setting-up jest config in my project's - package.json,
Package.json
"jest": {
"preset": "react-native",
"verbose": true,
"moduleDirectories": ["node_modules", "...
25
votes
5
answers
21k
views
How to test a React Native component that imports a custom native module with Jest?
Here is a simple component that I am trying to test using React Native 0.39 and Jest 18:
// index.ios.js
import React, { Component } from 'react';
import { AppRegistry, NativeModules, View } from '...
24
votes
3
answers
65k
views
Why do I get the error "Received value must be an HTMLElement or an SVGElement" when using react-native?
I'm new to unit testing and I'm trying to render a component to learn more about the library.
I'm trying to follow this guide.
Component
<TouchableOpacity
style={style}
onPress={onPress}
...
22
votes
1
answer
20k
views
Passing out-of-scope variable with jest.mock [duplicate]
I have a mock object that I am using to mock react-native:
const MyMock = {
MockA: {
methodA: jest.genMockFn()
},
MockB: {
ObjectB: {
methodA: jest.genMockFn(),...
22
votes
6
answers
11k
views
React Native + Jest EMFILE: too many open files error
I am trying to run Jest tests, but I'm getting the following error:
Error reading file: /Users/mike/dev/react/TestTest/node_modules/react-native/node_modules/yeoman-environment/node_modules/globby/...
22
votes
2
answers
78k
views
Is it possible to wait for a component to render? React Testing Library/Jest
I have a component. It has a button. Upon pressing the button, I am changing the style of the button text (color) using setState function. When I am testing the changed component, the test is failing ...
22
votes
2
answers
15k
views
fbBatchedBridgeConfig is not set when running jest tests
I have come late to the party on writing tests (I am still learning!), and have already had a few issues running jest tests on my app. In particular, with react-navigation.
Now I am having an issue as ...
21
votes
4
answers
13k
views
How can I stop my React Native tests bombing out on Flow types in Node Modules?
I am trying to set up testing on an existing React Native project. When I run a test with Jest, the test bombs out with the following error:
Test suite failed to run
...etc/__app__/node_modules/...
21
votes
11
answers
30k
views
React Native DuplicateError: Duplicated files or mocks
After updating xCode I am facing the below error from a week. I am not able to run app on device. I dont think the issue is with xcode because even if I try to run on android device the same issue ...
21
votes
3
answers
10k
views
How would you mock 'onPress' within Alert?
I'm able to mock out the Alert to test that it's alert method is being called, but what I really want to test is pressing the ok button within the alert.
import { Alert } from 'react-native';
it('...
21
votes
2
answers
2k
views
Cannot find module 'ReactNative' from 'react-native.js' w/ Jest
I'm attempting to use jest (v20.0.0) w/ my React Native application (v0.42.0) however when I run yarn jest I get the following error:
yarn jest v0.27.5
$ jest
FAIL __tests__/routing/router-test.js
...
20
votes
1
answer
26k
views
Testing component that uses react-navigation with Jest
I'm working on a React Native application that also use Redux and I want to write tests with Jest. I'm not able to mock the "navigation" prop that is added by react-navigation.
Here is my component:
...
20
votes
2
answers
10k
views
[React-Native][Jest]SyntaxError: Unexpected token import
It seems babel transform works in my testcase code, but es6 syntax in node_modules does not.
Error
environment
npm 4.5
MacOS Sierra
jest config
{
"preset": "react-native",
&...
20
votes
2
answers
39k
views
SyntaxError: Missing initializer in const declaration
I am writing a simple test using react-native-testing-library (my first steps with that library) in my react native expo app. But I am getting a confused error coming from somewhere inside react-...
20
votes
3
answers
8k
views
How to use Jest with React Native
The testing section of the docs for React Native suggest that Jest is the official way to do unit tests. However, they don't explain how to get it setup. Running Jest without any setup gives syntax ...
20
votes
1
answer
48k
views
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax
I'm using ignite to create an expo react native app. I'm using this guide https://ignitecookbook.com/docs/recipes/GeneratorComponentTests to create component test files
Here is the first test file
...
18
votes
4
answers
32k
views
Test suite failed to run TypeError: Cannot read property 'default' of undefined
I'm trying to setup Jest on my react-native project but it hasn't been playing nice with bugsnag-react-native.
With my current test configuration I'm seeing errors related to bugsnag's ...
18
votes
2
answers
18k
views
Jest encountered an unexpected token with react-native
So I'm trying to write tests on my React Native app with Jest and TypeScript. While using old babel version everything worked fine, but because of some project problems we had to upgrade babel to 7.0....
18
votes
2
answers
4k
views
Testing React Native apps with Jest
I've been trying to get Jest working with my react native project without much luck. Seems most threads are hacked solutions to get things up and running and I can't seem to get over the last hurdle ...
18
votes
1
answer
2k
views
jest-haste-map: Haste module naming collision
On our project, we have react-native installed in two workspaces (root folder and libraries folder for testing purposes).
This throws the typical error "jest-haste-map: Haste module naming collision" ...
17
votes
6
answers
22k
views
Jest test Animated.View for React-Native app
I try to test an Animated.View with Jest for React-Native. When I set a property visible to true, it supposed to animate my view from opacity 0 to opacity 1.
This is what my component renders:
<...
17
votes
3
answers
8k
views
React Native with Typescript and Jest is broken after 0.57 Update: Couldn't find preset "module:metro-react-native-babel-preset" relative to directory
If you integrate test with Jest and Enzyme in the new React Version 0.57 and TypeScript, they won't work. Here are the steps to reproduce:
Create a new React Native project:
react-native init MyApp -...
17
votes
1
answer
16k
views
How to really call fetch in Jest test
Is there a way to call fetch in a Jest test? I just want to call the live API to make sure it is still working. If there are 500 errors or the data is not what I expect than the test should report ...
17
votes
5
answers
18k
views
Cannot find module '@babel/runtime/helpers/interopRequireDefault' from 'node_modules/react-native/jest/setup.js' when I run tests
I'm trying to build a mobile application with typescript, react-native, expo and jest. When I'm trying to run tests:
FAIL src/components/Button/tests/Button.test.tsx
● Test suite failed to run
...
17
votes
4
answers
6k
views
How to wait for Redux Persist to rehydrate before ending test
I have the following code:
const App = () => {
return (
<Provider store={store}>
<PersistGate persistor={persistor} loading={<Text>Loading!</Text>}>
...
16
votes
9
answers
13k
views
react-navigation-hooks: How to test useFocusEffect
As I understand it should be done so that useFocusEffect would work as useEffect for testing (mock). I use useFocusEffect for fetchData:
useFocusEffect(
useCallback(() => {
fetchData();
}, ...
16
votes
1
answer
1k
views
redux-observable Promise is not getting resolved in unit test
I am trying to test this epic https://github.com/zarcode/unsplashapp/blob/master/src/epics/photos.js . Problem is that map never happens when I run test (which I assume means that Promise never ...
15
votes
4
answers
20k
views
Invariant Violation: `new NativeEventEmitter()` requires a non-null argument
I tried to run default test in react-native using jest and I found this problem:
Test suite failed to run
Invariant Violation: `new NativeEventEmitter()` requires a non-null argument.
at invariant ...
15
votes
3
answers
23k
views
How to simulate an event on a unit test with Jest, Enzyme for React-Native
I'm trying to figure out how to test an "onPress" event with Jest in a React-Native app so I can make sure the right function is called.
I went through the documentation and Google but couldn't find ...
15
votes
1
answer
10k
views
What is --env=jsdom
I was running my test suite for my react-native application with the command jest.
The tests would fail in the file jest-runtime/build/index.js on the line
const wrapper = this._environment....
15
votes
3
answers
28k
views
Jest React - New snapshot was not written. The update flag must be explicitly passed to write a new snapshot
I have a asp.net core project created with React template, trying to unit test a simple component with Jest snapshot and I am receiving below error.Can any one suggest how to fix it.
index.js
import ...
15
votes
4
answers
3k
views
Test platform specific extension code for React Native
Currently, React Native dynamically requires a file for the running platform with an specific file extension, *.ios.js or *.android.js. However, when running this code inside a test environment, we ...
15
votes
2
answers
6k
views
Detox + Jest - App has not responded to the network requests below
I am currently setting up Detox for an existing project and running into snags.
I am using Detox + Jest and am receiving an error
[Client.js/PENDING_REQUESTS] App has not responded to the network ...
14
votes
4
answers
7k
views
SyntaxError - node_modules/react-native/Libraries/polyfills/error-guard.js: Missing semicolon. (14:4) on running jest in react native library
I'm trying to setup jest for a react native library but I'm getting the below error
SyntaxError: react-native/Libraries/polyfills/error-guard.js: Missing semicolon. (14:4)
12 | let _inGuard = 0;...