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

Fix a bug where no default config name set by default #56

Merged
merged 2 commits into from
Jul 2, 2019
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const envSchema = require('env-schema')
function loadAndValidateEnvironment (fastify, opts, done) {
try {
const config = envSchema(opts)
const confKey = opts.confKey
const confKey = opts.confKey || 'config'
fastify.decorate(confKey, config)
done()
} catch (err) {
Expand Down
23 changes: 22 additions & 1 deletion test/fastify-env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const fastifyEnv = require('../index')

function makeTest (t, options, isOk, confExpected, errorMessage) {
t.plan(isOk ? 2 : 1)
options = Object.assign({ confKey: 'config' }, options)

const fastify = Fastify()
fastify.register(fastifyEnv, options)
Expand Down Expand Up @@ -259,3 +258,25 @@ tests.forEach(function (testConf) {
makeTest(t, options, testConf.isOk, testConf.confExpected, testConf.errorMessage)
})
})

t.test('should use custom config key name', async t => {
const schema = {
type: 'object',
required: [ 'PORT' ],
properties: {
PORT: {
type: 'integer',
default: 6666
}
}
}

const fastify = Fastify()
await fastify.register(fastifyEnv, {
Eomm marked this conversation as resolved.
Show resolved Hide resolved
schema: schema,
confKey: 'customConfigKeyName'
})
.ready(() => {
t.strictSame(fastify.customConfigKeyName, { PORT: 6666 })
})
})