Skip to content

Commit

Permalink
chore(deps): update express to v5 & jest deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
Imod7 committed Oct 8, 2024
1 parent cf2b58b commit a4f0d61
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 183 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@substrate/calc": "^0.3.1",
"argparse": "^2.0.1",
"confmgr": "^1.1.0",
"express": "^4.21.0",
"express": "^5.0.0",
"express-winston": "^4.2.0",
"http-errors": "^2.0.0",
"lru-cache": "^11.0.1",
Expand All @@ -68,8 +68,8 @@
"devDependencies": {
"@substrate/dev": "^0.8.0",
"@types/argparse": "2.0.16",
"@types/express": "^4.17.21",
"@types/express-serve-static-core": "^4.19.5",
"@types/express": "^5.0.0",
"@types/express-serve-static-core": "^5.0.0",
"@types/http-errors": "2.0.4",
"@types/lru-cache": "^7.10.10",
"@types/morgan": "1.9.9",
Expand Down
8 changes: 4 additions & 4 deletions src/App.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -97,15 +97,15 @@ export default class App {
*/
private initRoot() {
// Set up a root route
this.app.get('/', (_req: Request, res: Response) =>
this.app.get('/', (_req: Request, res: Response) => {
res.send({
docs: 'https://paritytech.github.io/substrate-api-sidecar/dist',
github: 'https://github.com/paritytech/substrate-api-sidecar',
version: packageJson.version,
listen: `${this.host}:${this.port}`,
routes: this.getRoutes(),
}),
);
});
});
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/controllers/AbstractControllers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -81,8 +81,8 @@ describe('AbstractController', () => {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
AbstractController['catchWrap'](throws)(req, res, next),
).resolves.toBe(undefined);
expect(next).toBeCalledTimes(1);
expect(next).toBeCalledWith('Throwing');
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith('Throwing');
});

it('catches throw from a synchronous functions and calls next with error', async () => {
Expand All @@ -98,27 +98,27 @@ describe('AbstractController', () => {
// eslint-disable-next-line @typescript-eslint/await-thenable
AbstractController['catchWrap'](throws)(req, res, next),
).resolves.toBe(undefined);
expect(next).toBeCalledTimes(1);
expect(next).toBeCalledWith('Throwing');
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith('Throwing');
});

it('handles a successful async callback appropriately', async () => {
const next = jest.fn();
const success = () => Promise.resolve().then(() => 'Great success!');
const success = () => Promise.resolve().then(() => console.log('Great success!'));

await expect(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
AbstractController['catchWrap'](success)(req, res, next),
).resolves.toBe(undefined);
expect(next).not.toBeCalled();
expect(next).not.toHaveBeenCalled();
});

it('handles a successful synchronous callback appropriately', async () => {
const next = jest.fn();
const success = () => 'Great success!';
const success = () => console.log('Great success!');

await expect(AbstractController['catchWrap'](success)(req, res, next)).resolves.toBe(undefined);
expect(next).not.toBeCalled();
expect(next).not.toHaveBeenCalled();
});
});

Expand Down
42 changes: 21 additions & 21 deletions src/middleware/error/testTools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -31,7 +31,7 @@ const mockReq = {} as Request;
export const callsNextWithErr =
(ware: ErrorRequestHandler) =>
(name: string, err: unknown): void => {
it(`calls next on error of type ${name}`, () => {
it(`calls next on error of type ${name}`, async () => {
const next = jest.fn();
const send = jest.fn();
const status = jest.fn((_code: number) => {
Expand All @@ -40,10 +40,10 @@ export const callsNextWithErr =
};
});

ware(err, mockReq, { headersSent: false, status } as unknown as Response, next);
expect(status).not.toBeCalled();
expect(send).not.toBeCalled();
expect(next).toBeCalledTimes(1);
await ware(err, mockReq, { headersSent: false, status } as unknown as Response, next);
expect(status).not.toHaveBeenCalled();
expect(send).not.toHaveBeenCalled();
expect(next).toHaveBeenCalledTimes(1);
});
};

Expand All @@ -59,7 +59,7 @@ export const callsNextWithErr =
export const catchesErrWithStatus =
(ware: ErrorRequestHandler) =>
(name: string, err: unknown, code: number): void => {
it(`catches ${name} and sends status code ${code}`, () => {
it(`catches ${name} and sends status code ${code}`, async () => {
const next = jest.fn();
const send = jest.fn();
const status = jest.fn((_code: number) => {
Expand All @@ -68,11 +68,11 @@ export const catchesErrWithStatus =
};
});

ware(err, mockReq, { headersSent: false, status } as unknown as Response, next);
expect(send).toBeCalledTimes(1);
expect(status).toBeCalledWith<[number]>(code);
expect(status).toBeCalledTimes(1);
expect(next).not.toBeCalled();
await ware(err, mockReq, { headersSent: false, status } as unknown as Response, next);
expect(send).toHaveBeenCalledTimes(1);
expect(status).toHaveBeenCalledWith<[number]>(code);
expect(status).toHaveBeenCalledTimes(1);
expect(next).not.toHaveBeenCalled();
});
};

Expand All @@ -89,7 +89,7 @@ export const catchesErrWithStatus =
export const catchesErrWithResponse =
(ware: ErrorRequestHandler) =>
(name: string, err: unknown, code: number, response: unknown): void => {
it(`catches ${name} and sends status code ${code}`, () => {
it(`catches ${name} and sends status code ${code}`, async () => {
const next = jest.fn();
const send = jest.fn();
const status = jest.fn((_code: number) => {
Expand All @@ -98,17 +98,17 @@ export const catchesErrWithResponse =
};
});

ware(err, mockReq, { headersSent: false, status } as unknown as Response, next);
expect(send).toBeCalledTimes(1);
expect(send).toBeCalledWith(response);
expect(status).toBeCalledWith<[number]>(code);
expect(status).toBeCalledTimes(1);
expect(next).not.toBeCalled();
await ware(err, mockReq, { headersSent: false, status } as unknown as Response, next);
expect(send).toHaveBeenCalledTimes(1);
expect(send).toHaveBeenCalledWith(response);
expect(status).toHaveBeenCalledWith<[number]>(code);
expect(status).toHaveBeenCalledTimes(1);
expect(next).not.toHaveBeenCalled();
});
};

export function callsNextWithSentHeaders(ware: ErrorRequestHandler, err: unknown): void {
it('calls next if the headers have been sent', () => {
it('calls next if the headers have been sent', async () => {
const next = jest.fn();
const send = jest.fn();
const status = jest.fn((_code: number) => {
Expand All @@ -117,7 +117,7 @@ export function callsNextWithSentHeaders(ware: ErrorRequestHandler, err: unknown
};
});

ware(err, mockReq, { headersSent: true, status } as unknown as Response, next);
await ware(err, mockReq, { headersSent: true, status } as unknown as Response, next);
expect(send).not.toBeCalled();
expect(status).not.toBeCalled();
expect(next).toBeCalledTimes(1);
Expand Down
18 changes: 9 additions & 9 deletions src/middleware/validate/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
Expand All @@ -24,11 +24,11 @@ import { RequestHandler } from 'express-serve-static-core';
* @param req Express Request containing thing it errors on.
*/
export const doesNotErrorWith = (name: string, req: Request, middleware: RequestHandler): void => {
it(`does not error with ${name}`, () => {
it(`does not error with ${name}`, async () => {
const next = jest.fn();
middleware(req, null as unknown as Response, next);
expect(next).toBeCalledTimes(1);
expect(next).toBeCalledWith();
await middleware(req, null as unknown as Response, next);
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith();
});
};

Expand All @@ -41,11 +41,11 @@ export const doesNotErrorWith = (name: string, req: Request, middleware: Request
* @param err Expected error that it passes to next.
*/
export const errorsWith = (name: string, req: Request, err: unknown, middleware: RequestHandler): void => {
it(`errors with ${name}`, () => {
it(`errors with ${name}`, async () => {
const next = jest.fn();

middleware(req, null as unknown as Response, next);
expect(next).toBeCalledTimes(1);
expect(next).toBeCalledWith(err);
await middleware(req, null as unknown as Response, next);
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith(err);
});
};
12 changes: 6 additions & 6 deletions src/services/accounts/AccountsBalanceInfoService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ describe('AccountsBalanceInfoService', () => {
).tokenSymbol,
).toEqual('fOoToKeN');

expect(mockTokensLocksAt).toBeCalled();
expect(mockTokenAccountAt).toBeCalled();
expect(mockBalancesLocksAt).not.toBeCalled();
expect(mockTokensLocksAt).toHaveBeenCalled();
expect(mockTokenAccountAt).toHaveBeenCalled();
expect(mockBalancesLocksAt).not.toHaveBeenCalled();
});

it('does not query tokens pallet storage with the native token', async () => {
Expand All @@ -263,9 +263,9 @@ describe('AccountsBalanceInfoService', () => {
).tokenSymbol,
).toEqual('doT');

expect(mockTokensLocksAt).not.toBeCalled();
expect(mockTokenAccountAt).not.toBeCalled();
expect(mockBalancesLocksAt).toBeCalled();
expect(mockTokensLocksAt).not.toHaveBeenCalled();
expect(mockTokenAccountAt).not.toHaveBeenCalled();
expect(mockBalancesLocksAt).toHaveBeenCalled();
});
});
});
Expand Down
Loading

0 comments on commit a4f0d61

Please sign in to comment.