Skip to content

Commit

Permalink
test: add tests for API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jamacku committed Mar 25, 2023
1 parent 6197370 commit 5081dd0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
9 changes: 1 addition & 8 deletions src/link.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import axios, { AxiosRequestConfig } from 'axios';
import { URL } from 'url';
import { z, ZodSchema } from 'zod';
import { errorResponseSchema } from './schema';

async function performRequest<
TSchema extends ZodSchema,
Expand All @@ -16,16 +15,10 @@ async function performRequest<
},
});

if (response.status != 200) {
const errorResponse = errorResponseSchema.parse(response.data);

throw new Error(JSON.stringify(errorResponse));
}

return schema.parse(response.data);
} catch (e: unknown) {
if (axios.isAxiosError(e)) {
throw new Error(e.message);
throw new Error(JSON.stringify(e.response?.data));
} else {
throw e;
}
Expand Down
8 changes: 8 additions & 0 deletions test/integration/about.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ describe('Test Testing Farm GET /about', () => {
const response = await api.about();
expect(response).toBeDefined();
});

test('error', async () => {
const api = new TestingFarmAPI('https://api.dev.testing-farm.io/v0.0');

await expect(api.about()).rejects.toThrowErrorMatchingInlineSnapshot(
'"\\"Not Found\\""'
);
});
});
43 changes: 42 additions & 1 deletion test/integration/ranch-composes.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test, describe } from 'vitest';

import TestingFarmAPI from '../../src/index';
import TestingFarmAPI, { Ranch } from '../../src/index';

describe('Test Testing Farm GET /composes/{ranch}', () => {
test('ranch `public` - response', async () => {
Expand All @@ -16,4 +16,45 @@ describe('Test Testing Farm GET /composes/{ranch}', () => {
const response = await api.ranchComposes('redhat');
expect(response).toBeDefined();
});

test('non-existent ranch', async () => {
const api = new TestingFarmAPI('https://api.dev.testing-farm.io/v0.1');

await expect(api.ranchComposes('my-ranch' as Ranch)).rejects
.toThrowErrorMatchingInlineSnapshot(`
"[
{
\\"code\\": \\"invalid_union\\",
\\"unionErrors\\": [
{
\\"issues\\": [
{
\\"received\\": \\"my-ranch\\",
\\"code\\": \\"invalid_literal\\",
\\"expected\\": \\"public\\",
\\"path\\": [],
\\"message\\": \\"Invalid literal value, expected \\\\\\"public\\\\\\"\\"
}
],
\\"name\\": \\"ZodError\\"
},
{
\\"issues\\": [
{
\\"received\\": \\"my-ranch\\",
\\"code\\": \\"invalid_literal\\",
\\"expected\\": \\"redhat\\",
\\"path\\": [],
\\"message\\": \\"Invalid literal value, expected \\\\\\"redhat\\\\\\"\\"
}
],
\\"name\\": \\"ZodError\\"
}
],
\\"path\\": [],
\\"message\\": \\"Invalid input\\"
}
]"
`);
});
});
10 changes: 10 additions & 0 deletions test/integration/request-details.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,14 @@ describe('Test Testing Farm GET /requests/{request_id}', () => {
}
`);
});

test('non-existent request_id', async () => {
const api = new TestingFarmAPI('https://api.dev.testing-farm.io/v0.1');

await expect(
api.requestDetails('request_id')
).rejects.toThrowErrorMatchingInlineSnapshot(
'"{\\"code\\":404,\\"message\\":\\"No such entity\\"}"'
);
});
});

0 comments on commit 5081dd0

Please sign in to comment.