Skip to content

Commit

Permalink
chore: move tests to axios
Browse files Browse the repository at this point in the history
  • Loading branch information
yannick-bonnefond authored and ybonnefond committed Mar 7, 2023
1 parent a7df9bb commit c60f641
Show file tree
Hide file tree
Showing 9 changed files with 527 additions and 425 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@types/hapi__accept": "^5.0.0",
"@types/jest": "^29.4.0",
"@types/lodash": "^4.14.191",
"got": "^11.3.0",
"axios": "^1.3.4",
"husky": "^8.0.3",
"jest": "^29.5.0",
"jest-diff": "^29.5.0",
Expand Down
52 changes: 52 additions & 0 deletions test/helpers/httpClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { Stubborn } from '../../src';

export interface HttpClientRequest {
path?: string;
headers?: Record<string, string>;
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
responseType?: 'json' | 'text' | 'arraybuffer';
data?: string | Record<string, unknown>;
query?: Record<string, string> | URLSearchParams;
}

export interface HttpClientResponse {
status: number;
data: string | Record<string, unknown> | Buffer;
headers: Record<string, unknown>;
}

export class HttpClient {
constructor(private readonly sb: Stubborn) {}

public async request(req: HttpClientRequest): Promise<HttpClientResponse> {
const res = await axios(this.toAxiosRequest(req));

return this.toHttpClientResponse(res);
}

private toAxiosRequest(req: HttpClientRequest): AxiosRequestConfig {
return {
baseURL: this.sb.getOrigin(),
url: req.path || '/',
method: req.method || 'GET',
data: req.data || '',
responseType: req.responseType || 'json',
params: req.query,
headers: {
accept: 'application/json',
'content-type': null,
...(req.headers || {}),
},
validateStatus: () => true,
};
}

private toHttpClientResponse(res: AxiosResponse) {
return {
status: res.status,
data: res.data,
headers: JSON.parse(JSON.stringify(res.headers)),
};
}
}
1 change: 0 additions & 1 deletion test/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './init';
export * from './ansi';
27 changes: 16 additions & 11 deletions test/matchers/toReplyWith.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { diff } from 'jest-diff';
import { HttpClientResponse } from '../helpers/httpClient';

declare global {
namespace jest {
Expand All @@ -21,10 +22,16 @@ declare global {

export function toReplyWith(
this: any,
response: any,
statusCode: any,
body?: any,
headers?: any,
response: HttpClientResponse,
{
status,
body,
headers,
}: {
status: number;
body?: unknown;
headers?: Record<string, string>;
},
) {
const fail = (message: string, { expected, received }: any = {}) => {
const extra = expected
Expand All @@ -34,23 +41,21 @@ export function toReplyWith(
pass: false,
message: () =>
`${message}\n${extra}\nRequest received:\n${JSON.stringify(
response.body,
response.data,
null,
2,
)}`, // eslint-disable-line no-magic-numbers
};
};

if (statusCode !== response.statusCode) {
return fail(
`expects ${statusCode} status code, got ${response.statusCode}`,
);
if (status !== response.status) {
return fail(`expects ${status} status code, got ${response.status}`);
}

if (body && !this.equals(body, response.body)) {
if (body && !this.equals(body, response.data)) {
return fail('Body does not match', {
expected: body,
received: response.body,
received: response.data,
});
}

Expand Down
5 changes: 5 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export * from './matchers';
import { test } from './test';

beforeAll(async () => await test.setup());
afterAll(async () => await test.tearDown());
beforeEach(async () => await test.clean());
Loading

0 comments on commit c60f641

Please sign in to comment.