Skip to content

Commit

Permalink
Error name (#84)
Browse files Browse the repository at this point in the history
* Update index.js

* fix tests

* fix code

* fix options null
  • Loading branch information
Eomm committed Feb 24, 2020
1 parent 7d78d57 commit 03cdeee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
12 changes: 7 additions & 5 deletions index.js
Expand Up @@ -15,8 +15,10 @@ function plugin (fn, options = {}) {

fn[Symbol.for('skip-override')] = true

const pluginName = (options && options.name) || checkName(fn)

if (typeof options === 'string') {
checkVersion(options)
checkVersion(options, pluginName)
options = {}
}

Expand All @@ -25,13 +27,13 @@ function plugin (fn, options = {}) {
}

if (!options.name) {
options.name = checkName(fn)
options.name = pluginName
}

fn[Symbol.for('fastify.display-name')] = options.name

if (options.fastify) {
checkVersion(options.fastify)
checkVersion(options.fastify, pluginName)
}

fn[Symbol.for('plugin-meta')] = options
Expand All @@ -49,7 +51,7 @@ function checkName (fn) {
}
}

function checkVersion (version) {
function checkVersion (version, pluginName) {
if (typeof version !== 'string') {
throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof version}'`)
}
Expand All @@ -62,7 +64,7 @@ function checkVersion (version) {
}

if (fastifyVersion && !semver.satisfies(fastifyVersion, version)) {
throw new Error(`fastify-plugin - expected '${version}' fastify version, '${fastifyVersion}' is installed`)
throw new Error(`fastify-plugin: ${pluginName} - expected '${version}' fastify version, '${fastifyVersion}' is installed`)
}
}

Expand Down
20 changes: 18 additions & 2 deletions test/test.js
Expand Up @@ -108,7 +108,7 @@ test('should throw if the fastify version does not satisfies the plugin requeste
fp(plugin, { fastify: '1000.1000.1000' })
t.fail()
} catch (e) {
t.is(e.message, `fastify-plugin - expected '1000.1000.1000' fastify version, '${v}' is installed`)
t.is(e.message, `fastify-plugin: plugin - expected '1000.1000.1000' fastify version, '${v}' is installed`)
}
})

Expand Down Expand Up @@ -182,7 +182,23 @@ test('should throw if the fastify version does not satisfies the plugin requeste
fp(plugin, { fastify: '1000.1000.1000' })
t.fail()
} catch (e) {
t.is(e.message, `fastify-plugin - expected '1000.1000.1000' fastify version, '${v}' is installed`)
t.is(e.message, `fastify-plugin: plugin - expected '1000.1000.1000' fastify version, '${v}' is installed`)
}
})

test('should throw if the fastify version does not satisfies the plugin requested version - plugin name', t => {
t.plan(1)

function plugin (fastify, opts, next) {
next()
}

const v = require('fastify/package.json').version.replace(/-rc\.\d+/, '')
try {
fp(plugin, { name: 'this-is-an-awesome-name', fastify: '1000.1000.1000' })
t.fail()
} catch (e) {
t.is(e.message, `fastify-plugin: this-is-an-awesome-name - expected '1000.1000.1000' fastify version, '${v}' is installed`)
}
})

Expand Down

0 comments on commit 03cdeee

Please sign in to comment.