Skip to content

Commit e23d2d2

Browse files
mccareEomm
andauthored
feat: added serverOptions to test helper.js (#552)
* 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 <behemoth89@gmail.com>
1 parent 9c1eded commit e23d2d2

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,11 @@ There are two utilities provided:
345345
- `build`: builds your application and returns the `fastify` instance without calling the `listen` method.
346346
- `listen`: starts your application and returns the `fastify` instance listening on the configured port.
347347

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

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

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

373+
Log output is consumed by tap. If log messages should be logged to the console
374+
the logger needs to be configured to output to stderr instead of stdout.
375+
376+
```js
377+
const logger = {
378+
transport: {
379+
target: 'pino-pretty',
380+
options: {
381+
destination: 2,
382+
},
383+
},
384+
}
385+
const argv = ['app.js']
386+
test('test my application with logging enabled', async t => {
387+
const app = await build(argv, {}, { logger })
388+
t.teardown(() => app.close())
389+
390+
// test your application here:
391+
const res = await app.inject('/')
392+
t.same(res.json(), { hello: 'one' })
393+
})
394+
```
395+
396+
397+
372398

373399
## Contributing
374400
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.

helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
const { runFastify } = require('./start')
44

55
module.exports = {
6-
build (args, additionalOptions = {}) {
6+
build (args, additionalOptions = {}, serverOptions = {}) {
77
Object.defineProperty(additionalOptions, 'ready', {
88
value: true,
99
enumerable: false,
1010
writable: false
1111
})
12-
return runFastify(args, additionalOptions)
12+
return runFastify(args, additionalOptions, serverOptions)
1313
},
1414
listen: runFastify
1515
}

start.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function stop (message) {
5555
exit(message)
5656
}
5757

58-
async function runFastify (args, additionalOptions) {
58+
async function runFastify (args, additionalOptions, serverOptions) {
5959
const opts = parseArgs(args)
6060
if (opts.require) {
6161
if (typeof opts.require === 'string') {
@@ -126,6 +126,10 @@ async function runFastify (args, additionalOptions) {
126126
}
127127
}
128128

129+
if (serverOptions) {
130+
Object.assign(options, serverOptions)
131+
}
132+
129133
const fastify = Fastify(
130134
opts.options ? Object.assign(options, file.options) : options
131135
)

test/helper.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const util = require('util')
44
const fs = require('fs')
55
const path = require('path')
66
const { test } = require('tap')
7+
const stream = require('stream')
78

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

@@ -99,3 +100,27 @@ test('should start fastify', async t => {
99100
t.teardown(() => app.close())
100101
t.ok(app.server.listening)
101102
})
103+
104+
test('should start fastify with custom logger configuration', async t => {
105+
const argv = ['./examples/plugin.js']
106+
const lines = []
107+
const dest = new stream.Writable({
108+
write: function (chunk, enc, cb) {
109+
lines.push(JSON.parse(chunk))
110+
cb()
111+
}
112+
})
113+
114+
const app = await helper.listen(argv, {}, {
115+
logger: {
116+
level: 'warn',
117+
stream: dest
118+
}
119+
})
120+
t.teardown(() => app.close())
121+
app.log.info('test')
122+
t.same(lines.length, 0)
123+
app.log.warn('test')
124+
t.same(lines.length, 1)
125+
t.same(app.log.level, 'warn')
126+
})

0 commit comments

Comments
 (0)