Questions tagged [jest-mock-axios]

33 questions
14
votes
0 answers

UnhandledPromiseRejectionWarning: TypeError: Caught error after test environment was torn down

Get Error when test fails case in post request image for Error when running test Using Reactjs framework version 16 axios-hooks for api requests react-testing-library for test UnhandledPromiseRejectionWarning: TypeError: Caught error after…
3
votes
0 answers

How do you test chained API calls in React with Jest?

I have a save workflow in my react/redux app that requires two API calls. The second call only happens once the first call is completed successfully. The app is working great and I'm adding some automated end-to-end testing now. I'm able to trigger…
SomethingOn
  • 9,813
  • 17
  • 68
  • 107
2
votes
1 answer

Avoid calling `expect` conditionally: jest testing error

I'm trying to test an async code with a negative case when the response is rejected. As per the docs, we can use try, catch with async await while trying to implement mock rejection https://jestjs.io/docs/tutorial-async // Or using…
Bharati
  • 21
  • 1
  • 3
2
votes
0 answers

get request parameter from axios mock in test

I create dynamically a request string in my react application, passing it to axios.get to request some data from REST interface. In my react test, I mock axios with axios.get.mockResolvedValue({ data: { list: [ { name: "Name 1", id: 0…
2
votes
1 answer

unable to mock the instance function of class (function from new instance of class) in nodejs using jest

In component.js, Need to mock the line: const accessValue = await objValueClass.getValue(); While running component.test.js, in console.log accessValue is coming as undefined component.test.js describe('** Handler unit tests **', () => { …
sing
  • 23
  • 2
2
votes
0 answers

How does one use jest-mock-axios to test a function that calls axios.get?

I wrote a function I'm trying to test. I followed an example on https://www.npmjs.com/package/jest-mock-axios#user-content-using-await-and-async. The only main difference structurally is that I'm using axios.get instead of axios.post. When I run my…
Colin
  • 331
  • 3
  • 19
2
votes
0 answers

Axios mock - multiple, nested axios request - issue and how to test it?

I'm trying to test async service that is returning response object after few intertwined API calls (axios with interceptors). Right now I'm using jest-mock-axios lib but I'm open to any alternatives or pure Jest. (I removed irrelevant parts of code,…
Bartek Ł.
  • 21
  • 3
1
vote
0 answers

How to write unit tests for http server upgrade and handle connection to websocket server?

I have following class and methods - //A.ts export abstract class A { } // B.ts import http from 'http'; import { Socket } from 'net'; import url, { URL } from 'url'; import { Server as WebSocketServer } from 'ws'; import { authenticateToken } from…
1
vote
0 answers

axios interceptor code is not covered in jest

When running jest test having Axios interceptor mocks the code is not covered for interceptor The Axios code: jest tests The code coverage I want 100% code coverage but interceptor code is not covered here
Prasad Parab
  • 437
  • 1
  • 7
  • 26
1
vote
1 answer

Jest-mock-axios mock interceptors

I have the following axios file: /* eslint-disable no-param-reassign */ import axios from 'axios'; import { baseURL } from './apiClient'; export default function authenticatedApiClient(jwt: string) { const apiClient = axios.create({ …
Joel Suter
  • 83
  • 1
  • 2
  • 10
1
vote
0 answers

UnhandledPromiseRejection error when mocking Axios in React

I've got a basic component which submits a form. This is the segments where the form submission is declared:
and the function that handles the data is: const onSubmit = async data => { …
MrCujo
  • 1,218
  • 3
  • 31
  • 56
1
vote
1 answer

Mock axios twice with react-testing-library

I'm testing the component Checkout which has two axios.get calls inside its useEffect, like so: useEffect(() => { let isMounted = true; if (isMounted) { axios .get("/cart/get-total") // 1st call …
Fer Toasted
  • 1,274
  • 1
  • 11
  • 27
1
vote
1 answer

React Jest TypeError: Cannot read property 'baseUrl' of undefined

I have a simple login api call where the base url is in config file, code below api.js export const login = (username, password) => { Axios.post(`${config.loginApi.baseUrl}/login`, { username, password }) .then(res =>…
dee
  • 2,244
  • 3
  • 13
  • 33
1
vote
2 answers

Testing Global Vue.prototype.$http method in Jest which uses axios in Vue

I have a global prototype defined in my Vue project in main.js as follows //main.js import axios from 'axios'; import Vue from 'vue'; Vue.prototype.$http=axios.create({ baseURL: 'https://example.com/api/' }); Various vue components directly…
Anand Thakkar
  • 43
  • 1
  • 5
1
vote
1 answer

[unit testing]: how can I get updated props after a click event that has a mock axios call

Code let basicProps = { inCart: false, quantity: 0, min: 1, max: 10, productData: mockProductData }; const mockData = { inCart: true, quantity: 1, min: 1 }; const mockOnAddToCart = jest.fn(async (data, props) => { const newProps…
1
2 3