Background: I have a series of methods that will together make multiple axios calls. In my scenario I must make the first axios call to get some data that I can then use to generate the second axios call. After that I have the third axios call that may be unrelated to the first two.
In my test I must validate that the URL used by the last axios call be a certain URL
Problem: When I validate the URL, I get only the first axios call. From some troubleshooting on my end, it seems as if Jest only executes the first axios call or that my resolved values are not being interpreted properly.
Ask: Is there a way what I'm trying to do in my scenario can work? Any help is greatly appreciate.
Here is some of the code that I've sanitized the best I can.
Main program:
interface IPrerequisite {
// Some implementation
}
class Prerequisite implements IPrerequisite {
error = "";
async checkStatus(token: string) {
return Promise.resolve(true);
}
async getAxiosResponse(url: string, token: string) {
const headers: AxiosRequestConfig = {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
timeout: timeoutDuration,
};
return axios
.post(url, {}, headers)
.then((res: AxiosResponse) => {
return Promise.resolve(res.data);
})
.catch((err: AxiosError) => {
return Promise.resolve("");
});
}
}
class ListKeysPrereq extends Prerequisite {
constructor() {
super();
}
async checkStatus(token: string) {
// First axios request
const firstResponse = await this.getAxiosResponse(url, token);
if (firstResponse === "") {
return Promise.resolve(false);
}
// Second axios request
// Do some stuff with first response to pass to second request
const secondResponse = await this.getAxiosResponse(url, token);
if (secondResponse === "") {
return Promise.resolve(false);
}
// Do some stuff with second response
return Promise.resolve(true);
}
}
class PrerequisiteFactory {
static getInstance(prerequisite: string): IPrerequisite {
switch (prerequisite) {
case "LIST_KEYS_PREREQ":
return new ListKeysPrereq();
default:
return null;
}
}
}
const checkConnectionPrerequisites = async (token: string) => {
enum Prerequisites {
LIST_KEYS_PREREQ,
}
for (const prereq in Prerequisites) {
if (isNaN(Number(prereq))) {
const prerequisite = PrerequisiteFactory.getInstance(prereq);
const prerequisiteStatus = await prerequisite.checkStatus(token);
if (!prerequisiteStatus) {
return Promise.resolve(prerequisite.error);
}
}
}
return Promise.resolve("");
};
const getConnectionString = async (url: string, token: string, location: string): Promise<string> => {
try {
const prereqStatus = await checkPrerequisites(token);
// Do some stuff
} catch (err) {
// Handle exception
}
// Last axios request
return axios
.post(url, {}, headers)
.then((response: AxiosResponse) => {
// Do something with the last response
return Promise.resolve("sometext");
})
.catch((err: AxiosError) => {
handleError(operationName, err);
return Promise.resolve("");
});
};
export const startProgram = async (token: string) => {
connectionString = await getConnectionString(url, token, location);
};
Jest Test
const postMock: jest.Mock = jest
.fn()
.mockResolvedValueOnce({
status: 200,
data: {
someUri: "https://somefakestorageaccount.blob.core.contoso.com:443/somefakecontainer/somefakeblob",
},
})
.mockResolvedValueOnce({ status: 200, data: { connectionString: "wss://thisfakestring/" } })
.mockResolvedValueOnce({ status: 400 })
.mockResolvedValue({ status: 200, data: { connectionString: "wss://thisfakestring/" } });
jest.mock("axios", () => {
return {
post: postMock,
};
});
test("Sample Test", () => {
const assertedUrl = "https://assertedurl.constoso.com/somecontainer/someblob";
main.startprogram("token").then(() => {
expect(postMock).toHaveBeenCalledTimes(1);
expect(postMock.mock.calls[0][0]).toBe(assertedUrl); // This returns the call from only the first axios call
expect(loadMock).toHaveBeenCalledTimes(4);
expect(openMock).toHaveBeenCalledTimes(2);
});
});