Skip to content

Commit

Permalink
test: silence unnecessary logging for tests (#916)
Browse files Browse the repository at this point in the history
* silence the logger in test mode

* when test:watch set node_env

* silence middleware errors for testing

* lint

* clarification comments
  • Loading branch information
TarikGul committed Jun 5, 2022
1 parent 6e70018 commit 9633480
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"start:log-rpc": "yarn run build && NODE_ENV=test yarn run main ",
"dev": "tsc-watch --onSuccess \"yarn run main\"",
"test": "NODE_ENV=test substrate-exec-jest --detectOpenHandles",
"test:watch": "substrate-exec-jest --watch",
"test:watch": "NODE_ENV=test substrate-exec-jest --watch",
"test:cov": "NODE_ENV=test substrate-exec-jest --detectOpenHandles --coverage",
"lint:e2e-tests": "cd e2e-tests && substrate-dev-run-lint",
"build:e2e-tests": "substrate-exec-tsc --project e2e-tests/tsconfig.json",
Expand Down
2 changes: 1 addition & 1 deletion src/logging/transports/consoleTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ export function consoleTransport(): transports.ConsoleTransportInstance {
handleExceptions: true,
format: format.combine(...transformers),
// Silence using `jest --silent`
silent: process.argv.indexOf('--silent') >= 0,
silent: process.env.NODE_ENV === 'test',
});
}
5 changes: 5 additions & 0 deletions src/middleware/error/errorMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const errorMiddlewareCatchesErrWithStatus =
catchesErrWithStatus(errorMiddleware);

describe('errorMiddleware', () => {
// Necessary since the consolveOverride is called after the getter for the logger is launced
beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(() => ({}));
});

errorMiddlewareCallsNextWithErr('ILegacyError', {
error: 'legacy error',
statusCode: 500,
Expand Down
5 changes: 5 additions & 0 deletions src/middleware/error/httpErrorMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const httpErrorMiddlewareCatchesErrWithStatus =
catchesErrWithStatus(httpErrorMiddleware);

describe('httpErrorMiddleware', () => {
// Necessary since the consolveOverride is called after the getter for the logger is launced
beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(() => ({}));
});

httpErrorMiddlewareCallsNextWithErr('Error', new Error('This is an error'));

httpErrorMiddlewareCallsNextWithErr('IBasicError', {
Expand Down
5 changes: 5 additions & 0 deletions src/middleware/error/internalErrorMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const internalErrorMiddlewareCatchesErrWithResponse = catchesErrWithResponse(
);

describe('internalErrorMiddleware', () => {
// Necessary since the consolveOverride is called after the getter for the logger is launced
beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(() => ({}));
});

internalErrorMiddlewareCatchesErrWithResponse(
'ITxLegacyError',
{
Expand Down
5 changes: 5 additions & 0 deletions src/middleware/error/legacyErrorMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ const legacyErrorMiddlewareCatchesErrWithResponse = catchesErrWithResponse(
);

describe('legacyErrorMiddleware', () => {
// Necessary since the consolveOverride is called after the getter for the logger is launced
beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(() => ({}));
});

legacyErrorMiddlewareCallsNextWithErr('Error', new Error('This is an error'));

legacyErrorMiddlewareCallsNextWithErr(
Expand Down
5 changes: 5 additions & 0 deletions src/middleware/error/txErrorMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const txErrorMiddlewareCatchesErrWithResponse =
catchesErrWithResponse(txErrorMiddleware);

describe('txErrorMiddleware', () => {
// Necessary since the consolveOverride is called after the getter for the logger is launced
beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(() => ({}));
});

txErrorMiddlewareCallsNextWithErr('Error', new Error('This is an error'));

txErrorMiddlewareCallsNextWithErr(
Expand Down

0 comments on commit 9633480

Please sign in to comment.