Skip to content
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

feat: added serverOptions to test helper.js #552

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ Both of these utilities have the `function(arg, pluginOptions)` 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')
})