Skip to content

Commit

Permalink
feat: added serverOptions to test helper.js (#552)
Browse files Browse the repository at this point in the history
* feat: added serverOptions next to pluginOptions to enable custom logging during testing

During testing, the log output through stdout is consumed by tap and supressed. To enable logging during testing, the logger needs to be configured to log to stderr. Specifying serverOptions allows to configure the server in the test helper.js

* chore: add serverOptions to readme

Co-authored-by: Manuel Spigolon <[email protected]>
  • Loading branch information
mccare and Eomm authored Sep 27, 2022
1 parent 9c1eded commit e23d2d2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,11 @@ There are two utilities provided:
- `build`: builds your application and returns the `fastify` instance without calling the `listen` method.
- `listen`: starts your application and returns the `fastify` instance listening on the configured port.

Both of these utilities have the `function(arg, pluginOptions)` parameters:
Both of these utilities have the `function(arg, pluginOptions, serverOptions)` parameters:

- `cliArgs`: is a string or a string array within the same arguments passed to the `fastify-cli` command.
- `pluginOptions`: is an object containing the options provided to the started plugin (eg: `app.js`).
- `serverOptions`: is an object containing the additional options provided to fastify server, similar to the `--options` command line argument

```js
// load the utility helper functions
Expand All @@ -369,6 +370,31 @@ test('test my application', async t => {
})
```

Log output is consumed by tap. If log messages should be logged to the console
the logger needs to be configured to output to stderr instead of stdout.

```js
const logger = {
transport: {
target: 'pino-pretty',
options: {
destination: 2,
},
},
}
const argv = ['app.js']
test('test my application with logging enabled', async t => {
const app = await build(argv, {}, { logger })
t.teardown(() => app.close())

// test your application here:
const res = await app.inject('/')
t.same(res.json(), { hello: 'one' })
})
```




## Contributing
If you feel you can help in any way, be it with examples, extra testing, or new features please open a pull request or open an issue.
Expand Down
4 changes: 2 additions & 2 deletions helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
const { runFastify } = require('./start')

module.exports = {
build (args, additionalOptions = {}) {
build (args, additionalOptions = {}, serverOptions = {}) {
Object.defineProperty(additionalOptions, 'ready', {
value: true,
enumerable: false,
writable: false
})
return runFastify(args, additionalOptions)
return runFastify(args, additionalOptions, serverOptions)
},
listen: runFastify
}
6 changes: 5 additions & 1 deletion start.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function stop (message) {
exit(message)
}

async function runFastify (args, additionalOptions) {
async function runFastify (args, additionalOptions, serverOptions) {
const opts = parseArgs(args)
if (opts.require) {
if (typeof opts.require === 'string') {
Expand Down Expand Up @@ -126,6 +126,10 @@ async function runFastify (args, additionalOptions) {
}
}

if (serverOptions) {
Object.assign(options, serverOptions)
}

const fastify = Fastify(
opts.options ? Object.assign(options, file.options) : options
)
Expand Down
25 changes: 25 additions & 0 deletions test/helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const util = require('util')
const fs = require('fs')
const path = require('path')
const { test } = require('tap')
const stream = require('stream')

const helper = require('../helper')

Expand Down Expand Up @@ -99,3 +100,27 @@ test('should start fastify', async t => {
t.teardown(() => app.close())
t.ok(app.server.listening)
})

test('should start fastify with custom logger configuration', async t => {
const argv = ['./examples/plugin.js']
const lines = []
const dest = new stream.Writable({
write: function (chunk, enc, cb) {
lines.push(JSON.parse(chunk))
cb()
}
})

const app = await helper.listen(argv, {}, {
logger: {
level: 'warn',
stream: dest
}
})
t.teardown(() => app.close())
app.log.info('test')
t.same(lines.length, 0)
app.log.warn('test')
t.same(lines.length, 1)
t.same(app.log.level, 'warn')
})

0 comments on commit e23d2d2

Please sign in to comment.