diff --git a/README.md b/README.md index 094ce508..0f51490f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/helper.js b/helper.js index 8950372b..bfb893b9 100644 --- a/helper.js +++ b/helper.js @@ -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 } diff --git a/start.js b/start.js index be93552c..2562efc2 100755 --- a/start.js +++ b/start.js @@ -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') { @@ -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 ) diff --git a/test/helper.test.js b/test/helper.test.js index cdcc5aa2..7150f19e 100644 --- a/test/helper.test.js +++ b/test/helper.test.js @@ -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') @@ -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') +})