Esto es fácil, pero llevo 5 horas luchando y aún no tengo una solución.
app.ts import { apiClient, getCustomFieldMapping } from './apiClient' const otherFunction = () => { // just to show that getCustomFieldMapping it's used getCustomFieldMapping() } // route /ext/:userId const callApi = async (path: string,) => { const url = apiClient.defaults.baseURL + path return apiClient.get(url); }
apiClient.ts import axios from "axios"; export const apiClient = axios.create({ baseURL: "http://localhost:3000", auth: { username: `${config.zendeskUsername}/token`, password: config.zendeskToken, }, }); export const getCustomFieldMapping = ( from: "fieldId" | "name", value: string ): TicketFieldMapping => { const fieldMap = fieldMappings.find((mapping) => mapping[from] === value); if (!fieldMap) { throw { message: `Zendesk custom field's mapping from "${from}" "${value}" does not exists.`, }; } return fieldMap; };
Lo que realmente está sucediendo es que no he podido simular solo la llamada GET api desde el módulo apiClient.
He intentado muchas cosas (enumeradas después de esto) como:
test.ts // first try import axios from 'axios' jest.mock("axios", () => ({ create: jest.fn(), })); test("Get user", async () => { (axios.create as jest.Mock).mockImplementation(() => { return { defaults: { baseURL: "https://localhost:3000", }, get: jest .fn() .mockResolvedValueOnce({}) }; }); return request(app) .get("/ext/121") .expect(StatusCodes.NOT_FOUND) .then((response) => { console.log(response.body); }); }); // I get that apiClient is undefined // another one is const mockGet = jest.fn(); jest.mock("./apiClient", () => { const actual = jest.requireActual("./apiClient"); actual.apiClient.get = jest.fn().mockImplementation(() => mockGet); return actual; }); test("Get user", async () => { mockGet.returnResolvedValueOnce({}) return request(app) .get("/ext/121") .expect(StatusCodes.NOT_FOUND) .then((response) => { console.log(response.body); }); }); // here I have two things.. one is that defualts is undefiend and it breaks the run // otherone it's that getCustomFieldMapping is undefined and can't be called
Quiero burlarme de la función GET de apiClient. eso es todo
Gracias por adelantado