Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting: TypeError: _axios.default.get.mockResolvedValue is not a function

Here's the error:

    TypeError: _axios.default.get.mockResolvedValue is not a function

      20 |         ]
      21 |     }
    > 22 |     Axios.get.mockResolvedValue(response);
         |               ^
      23 | 
      24 |     const component = renderer.create(
      25 |         <BookList />

Here's my code:

import React from "react";
import Axios from "axios";
import {shallow, mount, render} from "enzyme";

import BookList from "../BookList";

import renderer from "react-test-renderer";

jest.mock('Axios');

test("<BookList /> snapshot test", () => {
    const response = {
        data: [
            {
                title: "foo"
            },
            {
                title: "bar"
            }
        ]
    }
    Axios.get.mockResolvedValue(response);

    const component = renderer.create(
        <BookList />
    )
    let tree = component.toJSON();
    expect(tree).toMatchSnapshot();
})
like image 338
Blaine Lafreniere Avatar asked Oct 20 '25 04:10

Blaine Lafreniere


1 Answers

You are getting this error because mockResolvedValue doesn't exist in Axios.post. You'll have to type case it as necessary

(Axios.get as jest.Mock<{}>).mockResolvedValue(response);

Also, make sure you initialise the value of Axios.get as necessary. Like

Axios.get = jest.fn()

Also

jest.mock('Axios');

must be

jest.mock('axios');
like image 159
Karthick Vinod Avatar answered Oct 22 '25 20:10

Karthick Vinod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!