All Questions

Tagged with
Filter by
Sorted by
Tagged with
108votes
3answers
125kviews

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 ...
user avatar
  • 3,050
98votes
5answers
71kviews

How to change the behaviour of a mocked import?

I am quite confused with mocking in Jest an how to unit test the implementations. The thing is i want to mock different expected behaviours. Is there any way to achieve this? as imports can be only ...
user avatar
  • 2,374
62votes
10answers
63kviews

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 ...
user avatar
  • 2,857
61votes
4answers
19kviews

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 ...
user avatar
  • 2,297
49votes
8answers
50kviews

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 ...
user avatar
  • 534
42votes
18answers
25kviews

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 ...
user avatar
  • 5,034
42votes
4answers
28kviews

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 ...
user avatar
  • 421
40votes
2answers
29kviews

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(); });...
user avatar
40votes
5answers
31kviews

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 (...
user avatar
  • 10.4k
38votes
6answers
23kviews

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 ...
user avatar
34votes
3answers
8kviews

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 ...
user avatar
31votes
1answer
47kviews

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. ...
user avatar
  • 10.4k
29votes
7answers
21kviews

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 ...
user avatar
25votes
3answers
7kviews

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 ...
user avatar
  • 1,869
24votes
3answers
24kviews

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 ...
user avatar
  • 39.4k

15 30 50 per page
1
2 3 4 5
80