I introduce myself currently for the first time in jest and nodejs. I am facing the problem that I have to mock two different values from the nodejs config. the problem is that the second mockReturnValue overwrites the first one. is there any possibility to separate booth mocks from each other? Maybe with something like: Answer Since you would want
Tag: jestjs
Mocking node_modules which return a function with Jest?
I am writing a typeScript program which hits an external API. In the process of writing tests for this program, I have been unable to correctly mock-out the dependency on the external API in a way that allows me to inspect the values passed to the API itself. A simplified version of my code that hits the API is as
String concatenation returning undefined on Jest test snapshot
I have the following react component and I am writing my tests like this Problem is when it is creating the snapshot, the type variable is returning undefined. I assume it has something to do with the string concatenation, because the “role” variable is writing correctly. here is the snapshot. So, I am not sure what I am missing here
Jest Unit test + received undefined
I am using Jest as my unit test framework. I am trying to mock third part npm “request” and executed my test cases, but i am receiving and the test fails The following is my code: spec.js Ctrl.js Please share your ideas. Thanks in adavnce. Answer You don’t need to use await with the callback of request.post, just choose one
How to mock/spy useState hook in jest?
I am trying to spy on useState React hook but i always get the test failed This is my React component: counter.test.js: Unfortunately this doesn’t work and i get the test failed with that message: Answer You need to use React.useState instead of the single import useState. I think is about how the code gets transpiled, as you can see
Jest testcase breaking in React Native Expo
Following is the error I receive on running a Sample testcase: My Testcase: My jest configuration in package.json: My dependencies in package.json: expo version: 37.0.0 react: `16.9.0 react-native: “https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz” Answer Found the solution on the Expo thread mentioning the exact same issue here. It looks like you’re using jest-expo v39 with a project running SDK37. LogBox was released with React
Using Spies and Mocks on complex objects with Jest
I’m fairly new to testing and writing tests for a currently uncovered javaScript codebase using Jest. The code covers some niche use cases, as its conditionally injected and executed by the browser during page load. Anyway, I’m having issues mocking up custom objects. Here’s the function in question: As far as I understand, I need to mock both window.location.pathname to
Express POST API Route not receiving a Supertest request
I am trying to test an Express API POST Route that uses Express Validator for check: This API route expects to receive a request consisting of a body that contains the fields, name, email, and password: In order to test this route, I have a test file using supertest and jest: When I execute this test, each check in the
Jest | TypeError: window.URL.createObjectURL is not a function
This issue happens due to be using mapbox-gl in a React project. I’m aware that there are solutions like this but due to me being a junior I fail to completely comprehend what I should do to solve. I don’t have any setupTest.js or jest.stubs.js Creating them appears to do nothing for my test suites. I’m using ftw-hourly for this
How to test if React component is returning null or its children using React Testing Library?
I have a React component that returns its children to be rendered by React if the prop isTrue is truth-y. If its prop isTrue is false-y, then the component returns null, and React doesn’t render anything. I need to test it as a component, mount it, pass the prop, and test if it’s children is getting rendered when the prop