Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Augment supertest .expect(status) to log the response body when failing #1448

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion test/test-lib/init-tests.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import jsonwebtoken from 'jsonwebtoken';
import * as fixtures from './fixtures';
import { supertest, UserObjectParam } from './supertest';
import {
supertest,
augmentStatusAssertionError,
UserObjectParam,
} from './supertest';
import { version } from './versions';
import {
getContractRepos,
synchronizeContracts,
} from '../../src/features/contracts';

export const preInit = async () => {
augmentStatusAssertionError();
await import('./aws-mock');
await import('./contracts-mock');

Expand Down
38 changes: 37 additions & 1 deletion test/test-lib/supertest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
import { app } from '../../init';
import $supertest from 'supertest';
import { User } from '../../src/infra/auth/jwt-passport';
import { ThisShouldNeverHappenError } from '../../src/infra/error-handling';

export type UserObjectParam = Partial<User & { token: string }>;

export const augmentStatusAssertionError = () => {
const originalExpect: $supertest.Test['expect'] =
$supertest.Test.prototype.expect;
/**
* This enhances `.expect(statusCode, ...)` to also log the response body when
* the statusCode is different than expected, to make the original error more useful.
*/
$supertest.Test.prototype.expect = function (this: $supertest.Test, ...args) {
const [expectedStatus] = args;
let supertestFluentChain = this;
if (typeof expectedStatus === 'number') {
// TODO: Switch `.bind()` to `.call()` once TS is able to pick the correct overload.
supertestFluentChain = originalExpect.bind(supertestFluentChain)(
(res) => {
const error = this._assertStatus(expectedStatus, res);
if (error) {
error.message += `, with response body:\n${JSON.stringify(
res.body,
null,
2,
)}`;
throw error;
}
},
);
}
return originalExpect.apply(supertestFluentChain, args);
} satisfies typeof originalExpect;
};

export const supertest = function (user?: string | UserObjectParam) {
// Can be an object with `token`, a JWT string or an API key string
let token = user;
if (typeof user === 'object' && user.token) {
if (user != null && typeof user === 'object') {
if (user.token == null) {
throw ThisShouldNeverHappenError(
'Heads-up: You provided an object as a parameter to supertest that does not include a token, making requests that require authentication to always return 401!!!',
);
}
token = user.token;
}
// We have to cast `as any` because the types are poorly maintained
Expand Down
10 changes: 10 additions & 0 deletions typings/supertest-extension.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'supertest';

// Augment supertest
declare module 'supertest' {
interface Test {
_assertStatus(status: number, res: Response): Error | undefined;
}

function Test(app: any, method: string, path: string): Test;
}