I can't seem to figure out why my jest tests are throwing this error.
TypeError: axios.get.mockImplementationOnce is not a function
Here is my code:
./event.test.js
import axios from "axios";
import {jest} from '@jest/globals';
import ip from '../utils/ipv4.js';
import dotenv from 'dotenv';
import getEventByTitle from './eventTesting'
const result = dotenv.config({path: "../.env"});
if (result.error) throw result.error
const localhost = (process.env.IS_LOCAL == 'true') ? true : false;
const url = (localhost) ? `http://${ip}:${process.env.PORT}` : process.env.API_URL;
// Mock axios function
/*
jest.mock("axios", () => ({
get: jest.fn(() => Promise.resolve({
data: {}
}))
}))
*/
jest.mock("axios")
// ^^ BOTH MOCK ATTEMPTS DONT WORK ^^
// TEST SUITE
test("Test imports", () => {
expect(dotenv).toBeDefined()
expect(ip).toBeDefined()
expect(axios).toBeDefined()
})
describe('Axios test with mocking', () => {
test("Test GET event", async () => {
const fakeResponse = {
title: "Mitchell's Dorm Party",
host: 2,
description: "Come to my party tonight in Leavey",
location_id: 1,
time: new Date("2021-11-06 10:50:00 PST")
}
// TypeError: axios.get.mockImplementationOnce is not a function
axios.get.mockImplementationOnce(() =>
Promise.resolve({
data: fakeResponse
})
)
const result = await getEventByTitle("Mitchell's Dorm Party")
expect(mockAxios.get).toHaveBeenCalledTimes(1);
console.log(result)
})
})
I'm not sure what I could be doing wrong and none of the documentation posted online has helped so far. Any advice would really be appreciated. Thanks!