Blaine Lafreniere
Blaine Lafreniere

Reputation: 3600

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();
})

Upvotes: 4

Views: 12360

Answers (1)

Karthick Vinod
Karthick Vinod

Reputation: 1437

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');

Upvotes: 8

Related Questions