100% test coverage
parent
238e7010b4
commit
322979a0d8
|
@ -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,
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
});
|
||||
|
|
|
@ -11,12 +11,11 @@ async function request(url: string, method: string) {
|
|||
return res.data;
|
||||
} catch (error: any) {
|
||||
if (error.response && error.response.data) {
|
||||
throw (
|
||||
{
|
||||
...error.response.data,
|
||||
message: error.response.data.detail || error.response.data.title
|
||||
} || error
|
||||
);
|
||||
const { detail, title } = error.response.data;
|
||||
throw {
|
||||
...error.response.data,
|
||||
message: detail || title
|
||||
};
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue