-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fxtest): Add WithTestLogger option (#1159)
fxtest.New has this really nice behavior where by default, it uses the `testing.TB` as the destination for log output. Unfortunately, fxtest is not good for testing failure cases because it fails the test if the container failed. app := fxtest.New(t, fx.Invoke(func() error { return errors.New("fail") }), ) err := app.Start(ctx) // We never get here because fxtest.New has already failed the test. So the expectation there is to use `fx.New(..)` which, by default, logs to stderr. This can be addressed by using the `fx.WithLogger` option in combination with `fxtest.NewTestLogger`, but it ends up being a mouthful: app := fx.New( fx.Invoke(func() error { return errors.New("fail") }), fx.WithLogger(func() fxevent.Logger { return fxtest.NewTestLogger(t) }), ) This PR is a proposal and implementation of a new `fxtest.WithTestLogger` option that shortens the above to: app := fx.New( fx.Invoke(func() error { return errors.New("fail") }), fxtest.WithTestLogger(t), ) As an example, a couple tests in Fx itself become more readable with this new API. Co-authored-by: Jacob Oaks <[email protected]>
- Loading branch information
Showing
4 changed files
with
16 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters