-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
fix: remove console output from tests #939
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Windows build needs to pass. Seems like a random AppVeyor failure though.
@virkt25 The Windows failure on Node 6 (https://ci.appveyor.com/project/bajtos/loopback-next/build/1.0.3452-master/job/xs23p54blwivxy61) has been happening a lot recently. It seems to be a |
Yea I've seen AppVeyor failing a lot more on recent PRs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@raymondfeng While I appreciate your effort on cleaning the console output of npm test
, I am afraid your changes severely reduced developer experience while troubleshooting loopback-next tests. See my comment below.
In the future, I would really appreciate if pull requests like this one could be left open for longer period of time, allowing me to post my review before the changes are merged
break; | ||
} | ||
} | ||
} | ||
|
||
log(msg: string) { | ||
debug(msg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes our example-log-extensions sort of useless. The point of the extension is to let applications produce logs that are useful in production. Now we are logging via debug
which is almost always turned off in production environments :(
IMO, the log component should:
- print to console by default
- allow tests to choose a different target (e.g.
debug
).
Since the problem of cluttered test output is not specific to us only, but will apply to anybody building an application with a logger component, I think it's even more important for our example log component to show an elegant solution for suppressing console logs in tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a slab.
@@ -10,6 +10,8 @@ export class LogLevelProvider implements Provider<number> { | |||
constructor() {} | |||
|
|||
value(): number { | |||
const level = Number(process.env.LOG_LEVEL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1000
Internal implementation should not access process.env
directly, because it introduces hidden coupling that's difficult to discover (learn about its existence) and make it difficult to customize the behavior in specific places only, without affecting everybody else living in the same Node.js process.
If we want to access process.env.LOG_LEVEL
, the such code should live somewhere in the main application file where we are constructing and booting an Application
instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To illustrate the provider
concept for logging level, we need to have a variable
. Let me see what else we can use instead of env var.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provider returns a value that is bound via the component. The user can override the log-level but re-binding the value to a different value explicitly in the app.
} | ||
|
||
function restoreConsoleSpy() { | ||
function restoreLogSpy() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is duplicated here and in p
ackages/example-log-extension/test/unit/providers/log-action.provider.unit.ts, please move both
createand
restore` helpers to a shared file.
return; | ||
} | ||
|
||
console.error( | ||
debug( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1
Printing failed HTTP requests to stderr
is intentional behavior that needs to be preserved as the default mode of LoopBack4+ applications.
This is important for our unit-tests too, because when a request fails with an HTTP status code we did not expect, then we do want to see full stack trace of the server error in the test output. For example, in a test checking that a request for unknown path returns 404, we want to see server stack trace when the request failed with 500 Internal Error caused e.g. by TypeError
or ReferenceError
.
We already have a solution for suppressing these messages you can use for inspiration, see here:
@@ -521,26 +527,11 @@ describe('HttpHandler', () => { | |||
function givenClient() { | |||
client = createClientForHandler((req, res) => { | |||
handler.handleRequest(req, res).catch(err => { | |||
console.error('Request failed.', err.stack); | |||
debug('Request failed.', err.stack); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This console log must be preserved, see my comment above. If there is a test that's triggering this code path as part of the expected operation, then please use a mechanism similar to skipStatusCode
above to allow tests to control which errors to silently ignore.
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
This PR is a follow-up to the following issue based on review comments: #939 - Demonstrate how to plug in a different log (console vs memory) - Switch to @loopback/metadata for decorator implementation - Remove log level provider as it's supposed to be contributed by the app The exercise also exposes a few needs: 1. Support for optional dependency so that we can fall back a default implementation (or a better way) 2. Allow a component to bind artifacts beyond providers
See #935
Checklist
npm test
passes on your machinepackages/cli
were updatedpackages/example-*
were updated