100% test coverage

master
Tudor Stanciu 2023-02-11 04:01:15 +02:00
parent 238e7010b4
commit 322979a0d8
3 changed files with 42 additions and 7 deletions

View File

@ -4,7 +4,7 @@
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
"collectCoverage": false,
"collectCoverage": true,
"coverageThreshold": {
"global": {
"branches": 50,

View File

@ -30,3 +30,39 @@ test("Tuitio client authentication", async () => {
const emptyStorage = fetch();
expect(emptyStorage.userName).toBeNull();
});
test("Tuitio client failed authentication", async () => {
(axios.request as jest.Mock).mockRejectedValueOnce({
response: {
status: 500,
data: { title: "Internal server error" }
}
});
const client = new TuitioClient("https://test.com/api");
try {
await client.authenticate("user", "pass");
} catch (error: any) {
expect(error.title).toBe("Internal server error");
expect(error.message).toBe("Internal server error");
}
});
test("Tuitio client failed authentication with unhandled exception", async () => {
(axios.request as jest.Mock).mockRejectedValueOnce({
response: {
status: 500,
error: { message: "Internal server error" }
}
});
const client = new TuitioClient("https://test.com/api");
try {
await client.authenticate("user", "pass");
} catch (error: any) {
expect(error.response.status).toBe(500);
expect(error.response.error.message).toBe("Internal server error");
}
});

View File

@ -11,12 +11,11 @@ async function request(url: string, method: string) {
return res.data;
} catch (error: any) {
if (error.response && error.response.data) {
throw (
{
const { detail, title } = error.response.data;
throw {
...error.response.data,
message: error.response.data.detail || error.response.data.title
} || error
);
message: detail || title
};
}
throw error;
}