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

chore(deps): update express to v5 & jest deprecations #1502

Merged
merged 3 commits into from
Oct 8, 2024
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
10 changes: 10 additions & 0 deletions guides/MAINTENANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ Review the security alerts raised by Dependabot [here](https://github.com/parity

**Frequency**: Monthly or longer.

### Express version
- Check if there is a new major version of [Express](https://github.com/expressjs/express).
- Update the version by running the command `yarn add express@^X.X.X`, e.g. `yarn add express@^5.0.0`.
- After upgrading, we can do the usual sanity checks (e.g. `yarn`, `yarn dedupe`, `yarn build`, `yarn lint --fix`).

**Frequency**: Yearly or longer.

### Keep `substrate-js-dev` repository up to date
Sidecar is using [substrate-js-dev](https://github.com/paritytech/substrate-js-dev) repository as a dependency. For instance, running `yarn lint` in sidecar triggers the `substrate-dev-run-lint` script which is defined [here](https://github.com/paritytech/substrate-js-dev/blob/58fe47c55610a797c2e780c5cfef0f3dd2fae7ca/scripts/substrate-dev-run-lint.cjs). Therefore, it is important to keep this repository's dependencies up to date as well. This can be done by running `yarn upgrade-interactive` and following the process mentioned in the previous step. Last, we would need to make a new release in that repository and then update the package version in Sidecar.

Expand Down Expand Up @@ -113,3 +120,6 @@ The frequency is somewhat flexible.
- Check Public instances of Sidecar.
- Docker images of Sidecar.
- Review repository branches.

- Yearly
- Express version.
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
Loading