Aktueller Stand
This commit is contained in:
357
backend/node_modules/fastify/test/plugin.4.test.js
generated
vendored
357
backend/node_modules/fastify/test/plugin.4.test.js
generated
vendored
@@ -1,14 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
/* eslint no-prototype-builtins: 0 */
|
||||
|
||||
const t = require('tap')
|
||||
const test = t.test
|
||||
const { test, describe } = require('node:test')
|
||||
const Fastify = require('../fastify')
|
||||
const fp = require('fastify-plugin')
|
||||
const fakeTimer = require('@sinonjs/fake-timers')
|
||||
const { FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER } = require('../lib/errors')
|
||||
|
||||
test('pluginTimeout', t => {
|
||||
test('pluginTimeout', (t, testDone) => {
|
||||
t.plan(5)
|
||||
const fastify = Fastify({
|
||||
pluginTimeout: 10
|
||||
@@ -17,16 +15,17 @@ test('pluginTimeout', t => {
|
||||
// to no call done on purpose
|
||||
})
|
||||
fastify.ready((err) => {
|
||||
t.ok(err)
|
||||
t.equal(err.message,
|
||||
t.assert.ok(err)
|
||||
t.assert.strictEqual(err.message,
|
||||
"fastify-plugin: Plugin did not start in time: 'function (app, opts, done) { -- // to no call done on purpose'. You may have forgotten to call 'done' function or to resolve a Promise")
|
||||
t.equal(err.code, 'FST_ERR_PLUGIN_TIMEOUT')
|
||||
t.ok(err.cause)
|
||||
t.equal(err.cause.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')
|
||||
t.assert.strictEqual(err.code, 'FST_ERR_PLUGIN_TIMEOUT')
|
||||
t.assert.ok(err.cause)
|
||||
t.assert.strictEqual(err.cause.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('pluginTimeout - named function', t => {
|
||||
test('pluginTimeout - named function', (t, testDone) => {
|
||||
t.plan(5)
|
||||
const fastify = Fastify({
|
||||
pluginTimeout: 10
|
||||
@@ -35,16 +34,17 @@ test('pluginTimeout - named function', t => {
|
||||
// to no call done on purpose
|
||||
})
|
||||
fastify.ready((err) => {
|
||||
t.ok(err)
|
||||
t.equal(err.message,
|
||||
t.assert.ok(err)
|
||||
t.assert.strictEqual(err.message,
|
||||
"fastify-plugin: Plugin did not start in time: 'nameFunction'. You may have forgotten to call 'done' function or to resolve a Promise")
|
||||
t.equal(err.code, 'FST_ERR_PLUGIN_TIMEOUT')
|
||||
t.ok(err.cause)
|
||||
t.equal(err.cause.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')
|
||||
t.assert.strictEqual(err.code, 'FST_ERR_PLUGIN_TIMEOUT')
|
||||
t.assert.ok(err.cause)
|
||||
t.assert.strictEqual(err.cause.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('pluginTimeout default', t => {
|
||||
test('pluginTimeout default', (t, testDone) => {
|
||||
t.plan(5)
|
||||
const clock = fakeTimer.install({ shouldClearNativeTimers: true })
|
||||
|
||||
@@ -55,18 +55,19 @@ test('pluginTimeout default', t => {
|
||||
})
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.ok(err)
|
||||
t.equal(err.message,
|
||||
t.assert.ok(err)
|
||||
t.assert.strictEqual(err.message,
|
||||
"fastify-plugin: Plugin did not start in time: 'function (app, opts, done) { -- // default time elapsed without calling done'. You may have forgotten to call 'done' function or to resolve a Promise")
|
||||
t.equal(err.code, 'FST_ERR_PLUGIN_TIMEOUT')
|
||||
t.ok(err.cause)
|
||||
t.equal(err.cause.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')
|
||||
t.assert.strictEqual(err.code, 'FST_ERR_PLUGIN_TIMEOUT')
|
||||
t.assert.ok(err.cause)
|
||||
t.assert.strictEqual(err.cause.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')
|
||||
testDone()
|
||||
})
|
||||
|
||||
t.teardown(clock.uninstall)
|
||||
t.after(clock.uninstall)
|
||||
})
|
||||
|
||||
test('plugin metadata - version', t => {
|
||||
test('plugin metadata - version', (t, testDone) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -79,7 +80,8 @@ test('plugin metadata - version', t => {
|
||||
fastify.register(plugin)
|
||||
|
||||
fastify.ready(() => {
|
||||
t.pass('everything right')
|
||||
t.assert.ok('everything right')
|
||||
testDone()
|
||||
})
|
||||
|
||||
function plugin (instance, opts, done) {
|
||||
@@ -87,7 +89,7 @@ test('plugin metadata - version', t => {
|
||||
}
|
||||
})
|
||||
|
||||
test('plugin metadata - version range', t => {
|
||||
test('plugin metadata - version range', (t, testDone) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -100,7 +102,8 @@ test('plugin metadata - version range', t => {
|
||||
fastify.register(plugin)
|
||||
|
||||
fastify.ready(() => {
|
||||
t.pass('everything right')
|
||||
t.assert.ok('everything right')
|
||||
testDone()
|
||||
})
|
||||
|
||||
function plugin (instance, opts, done) {
|
||||
@@ -108,7 +111,7 @@ test('plugin metadata - version range', t => {
|
||||
}
|
||||
})
|
||||
|
||||
test('plugin metadata - version not matching requirement', t => {
|
||||
test('plugin metadata - version not matching requirement', (t, testDone) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -121,8 +124,9 @@ test('plugin metadata - version not matching requirement', t => {
|
||||
fastify.register(plugin)
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.ok(err)
|
||||
t.equal(err.code, 'FST_ERR_PLUGIN_VERSION_MISMATCH')
|
||||
t.assert.ok(err)
|
||||
t.assert.strictEqual(err.code, 'FST_ERR_PLUGIN_VERSION_MISMATCH')
|
||||
testDone()
|
||||
})
|
||||
|
||||
function plugin (instance, opts, done) {
|
||||
@@ -130,9 +134,12 @@ test('plugin metadata - version not matching requirement', t => {
|
||||
}
|
||||
})
|
||||
|
||||
test('plugin metadata - version not matching requirement 2', t => {
|
||||
test('plugin metadata - version not matching requirement 2', (t, testDone) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
Object.defineProperty(fastify, 'version', {
|
||||
value: '99.0.0'
|
||||
})
|
||||
|
||||
plugin[Symbol.for('skip-override')] = true
|
||||
plugin[Symbol.for('plugin-meta')] = {
|
||||
@@ -143,8 +150,9 @@ test('plugin metadata - version not matching requirement 2', t => {
|
||||
fastify.register(plugin)
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.ok(err)
|
||||
t.equal(err.code, 'FST_ERR_PLUGIN_VERSION_MISMATCH')
|
||||
t.assert.ok(err)
|
||||
t.assert.strictEqual(err.code, 'FST_ERR_PLUGIN_VERSION_MISMATCH')
|
||||
testDone()
|
||||
})
|
||||
|
||||
function plugin (instance, opts, done) {
|
||||
@@ -152,7 +160,7 @@ test('plugin metadata - version not matching requirement 2', t => {
|
||||
}
|
||||
})
|
||||
|
||||
test('plugin metadata - version not matching requirement 3', t => {
|
||||
test('plugin metadata - version not matching requirement 3', (t, testDone) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -165,8 +173,9 @@ test('plugin metadata - version not matching requirement 3', t => {
|
||||
fastify.register(plugin)
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.ok(err)
|
||||
t.equal(err.code, 'FST_ERR_PLUGIN_VERSION_MISMATCH')
|
||||
t.assert.ok(err)
|
||||
t.assert.strictEqual(err.code, 'FST_ERR_PLUGIN_VERSION_MISMATCH')
|
||||
testDone()
|
||||
})
|
||||
|
||||
function plugin (instance, opts, done) {
|
||||
@@ -174,7 +183,7 @@ test('plugin metadata - version not matching requirement 3', t => {
|
||||
}
|
||||
})
|
||||
|
||||
test('plugin metadata - release candidate', t => {
|
||||
test('plugin metadata - release candidate', (t, testDone) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
Object.defineProperty(fastify, 'version', {
|
||||
@@ -189,8 +198,9 @@ test('plugin metadata - release candidate', t => {
|
||||
fastify.register(plugin)
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.error(err)
|
||||
t.pass('everything right')
|
||||
t.assert.ifError(err)
|
||||
t.assert.ok('everything right')
|
||||
testDone()
|
||||
})
|
||||
|
||||
function plugin (instance, opts, done) {
|
||||
@@ -198,43 +208,80 @@ test('plugin metadata - release candidate', t => {
|
||||
}
|
||||
})
|
||||
|
||||
test('fastify-rc loads prior version plugins', t => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
Object.defineProperty(fastify, 'version', {
|
||||
value: '99.0.0-rc.1'
|
||||
describe('fastify-rc loads prior version plugins', async () => {
|
||||
test('baseline (rc)', (t, testDone) => {
|
||||
t.plan(1)
|
||||
|
||||
const fastify = Fastify()
|
||||
Object.defineProperty(fastify, 'version', {
|
||||
value: '99.0.0-rc.1'
|
||||
})
|
||||
|
||||
plugin[Symbol.for('plugin-meta')] = {
|
||||
name: 'plugin',
|
||||
fastify: '^98.1.0'
|
||||
}
|
||||
plugin2[Symbol.for('plugin-meta')] = {
|
||||
name: 'plugin2',
|
||||
fastify: '98.x'
|
||||
}
|
||||
|
||||
fastify.register(plugin)
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.assert.ifError(err)
|
||||
testDone()
|
||||
})
|
||||
|
||||
function plugin (instance, opts, done) {
|
||||
done()
|
||||
}
|
||||
|
||||
function plugin2 (instance, opts, done) {
|
||||
done()
|
||||
}
|
||||
})
|
||||
|
||||
plugin[Symbol.for('plugin-meta')] = {
|
||||
name: 'plugin',
|
||||
fastify: '^98.1.0'
|
||||
}
|
||||
plugin2[Symbol.for('plugin-meta')] = {
|
||||
name: 'plugin2',
|
||||
fastify: '98.x'
|
||||
}
|
||||
test('pre', (t, testDone) => {
|
||||
t.plan(1)
|
||||
|
||||
fastify.register(plugin)
|
||||
const fastify = Fastify()
|
||||
Object.defineProperty(fastify, 'version', { value: '99.0.0-pre.1' })
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.error(err)
|
||||
t.pass('everything right')
|
||||
plugin[Symbol.for('plugin-meta')] = { name: 'plugin', fastify: '^98.x' }
|
||||
|
||||
fastify.register(plugin)
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.assert.ifError(err)
|
||||
testDone()
|
||||
})
|
||||
|
||||
function plugin (instance, opts, done) { done() }
|
||||
})
|
||||
|
||||
function plugin (instance, opts, done) {
|
||||
done()
|
||||
}
|
||||
test('alpha', (t, testDone) => {
|
||||
t.plan(1)
|
||||
|
||||
function plugin2 (instance, opts, done) {
|
||||
done()
|
||||
}
|
||||
const fastify = Fastify()
|
||||
Object.defineProperty(fastify, 'version', { value: '99.0.0-pre.1' })
|
||||
|
||||
plugin[Symbol.for('plugin-meta')] = { name: 'plugin', fastify: '^98.x' }
|
||||
|
||||
fastify.register(plugin)
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.assert.ifError(err)
|
||||
testDone()
|
||||
})
|
||||
|
||||
function plugin (instance, opts, done) { done() }
|
||||
})
|
||||
})
|
||||
|
||||
test('hasPlugin method exists as a function', t => {
|
||||
t.plan(1)
|
||||
|
||||
test('hasPlugin method exists as a function', (t) => {
|
||||
const fastify = Fastify()
|
||||
t.equal(typeof fastify.hasPlugin, 'function')
|
||||
t.assert.strictEqual(typeof fastify.hasPlugin, 'function')
|
||||
})
|
||||
|
||||
test('hasPlugin returns true if the specified plugin has been registered', async t => {
|
||||
@@ -243,60 +290,59 @@ test('hasPlugin returns true if the specified plugin has been registered', async
|
||||
const fastify = Fastify()
|
||||
|
||||
function pluginA (fastify, opts, done) {
|
||||
t.ok(fastify.hasPlugin('plugin-A'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-A'))
|
||||
done()
|
||||
}
|
||||
pluginA[Symbol.for('fastify.display-name')] = 'plugin-A'
|
||||
fastify.register(pluginA)
|
||||
|
||||
fastify.register(function pluginB (fastify, opts, done) {
|
||||
t.ok(fastify.hasPlugin('pluginB'))
|
||||
t.assert.ok(fastify.hasPlugin('pluginB'))
|
||||
done()
|
||||
})
|
||||
|
||||
fastify.register(function (fastify, opts, done) {
|
||||
// one line
|
||||
t.ok(fastify.hasPlugin('function (fastify, opts, done) { -- // one line'))
|
||||
t.assert.ok(fastify.hasPlugin('function (fastify, opts, done) { -- // one line'))
|
||||
done()
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
t.ok(fastify.hasPlugin('fastify'))
|
||||
t.assert.ok(fastify.hasPlugin('fastify'))
|
||||
})
|
||||
|
||||
test('hasPlugin returns false if the specified plugin has not been registered', t => {
|
||||
t.plan(1)
|
||||
|
||||
test('hasPlugin returns false if the specified plugin has not been registered', (t) => {
|
||||
const fastify = Fastify()
|
||||
t.notOk(fastify.hasPlugin('pluginFoo'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginFoo'))
|
||||
})
|
||||
|
||||
test('hasPlugin returns false when using encapsulation', async t => {
|
||||
t.plan(25)
|
||||
|
||||
const fastify = Fastify()
|
||||
t.after(() => fastify.close())
|
||||
|
||||
fastify.register(function pluginA (fastify, opts, done) {
|
||||
t.ok(fastify.hasPlugin('pluginA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAAA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAB'))
|
||||
t.notOk(fastify.hasPlugin('pluginB'))
|
||||
t.assert.ok(fastify.hasPlugin('pluginA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAB'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginB'))
|
||||
|
||||
fastify.register(function pluginAA (fastify, opts, done) {
|
||||
t.notOk(fastify.hasPlugin('pluginA'))
|
||||
t.ok(fastify.hasPlugin('pluginAA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAAA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAB'))
|
||||
t.notOk(fastify.hasPlugin('pluginB'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginA'))
|
||||
t.assert.ok(fastify.hasPlugin('pluginAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAB'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginB'))
|
||||
|
||||
fastify.register(function pluginAAA (fastify, opts, done) {
|
||||
t.notOk(fastify.hasPlugin('pluginA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAA'))
|
||||
t.ok(fastify.hasPlugin('pluginAAA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAB'))
|
||||
t.notOk(fastify.hasPlugin('pluginB'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAA'))
|
||||
t.assert.ok(fastify.hasPlugin('pluginAAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAB'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginB'))
|
||||
|
||||
done()
|
||||
})
|
||||
@@ -305,11 +351,11 @@ test('hasPlugin returns false when using encapsulation', async t => {
|
||||
})
|
||||
|
||||
fastify.register(function pluginAB (fastify, opts, done) {
|
||||
t.notOk(fastify.hasPlugin('pluginA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAAA'))
|
||||
t.ok(fastify.hasPlugin('pluginAB'))
|
||||
t.notOk(fastify.hasPlugin('pluginB'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAAA'))
|
||||
t.assert.ok(fastify.hasPlugin('pluginAB'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginB'))
|
||||
|
||||
done()
|
||||
})
|
||||
@@ -318,11 +364,11 @@ test('hasPlugin returns false when using encapsulation', async t => {
|
||||
})
|
||||
|
||||
fastify.register(function pluginB (fastify, opts, done) {
|
||||
t.notOk(fastify.hasPlugin('pluginA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAAA'))
|
||||
t.notOk(fastify.hasPlugin('pluginAB'))
|
||||
t.ok(fastify.hasPlugin('pluginB'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('pluginAB'))
|
||||
t.assert.ok(fastify.hasPlugin('pluginB'))
|
||||
|
||||
done()
|
||||
})
|
||||
@@ -336,26 +382,26 @@ test('hasPlugin returns true when using no encapsulation', async t => {
|
||||
const fastify = Fastify()
|
||||
|
||||
fastify.register(fp((fastify, opts, done) => {
|
||||
t.equal(fastify.pluginName, 'fastify -> plugin-AA')
|
||||
t.ok(fastify.hasPlugin('plugin-AA'))
|
||||
t.notOk(fastify.hasPlugin('plugin-A'))
|
||||
t.notOk(fastify.hasPlugin('plugin-AAA'))
|
||||
t.notOk(fastify.hasPlugin('plugin-AB'))
|
||||
t.notOk(fastify.hasPlugin('plugin-B'))
|
||||
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-AA')
|
||||
t.assert.ok(fastify.hasPlugin('plugin-AA'))
|
||||
t.assert.ok(!fastify.hasPlugin('plugin-A'))
|
||||
t.assert.ok(!fastify.hasPlugin('plugin-AAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('plugin-AB'))
|
||||
t.assert.ok(!fastify.hasPlugin('plugin-B'))
|
||||
|
||||
fastify.register(fp((fastify, opts, done) => {
|
||||
t.ok(fastify.hasPlugin('plugin-AA'))
|
||||
t.ok(fastify.hasPlugin('plugin-A'))
|
||||
t.notOk(fastify.hasPlugin('plugin-AAA'))
|
||||
t.notOk(fastify.hasPlugin('plugin-AB'))
|
||||
t.notOk(fastify.hasPlugin('plugin-B'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-AA'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-A'))
|
||||
t.assert.ok(!fastify.hasPlugin('plugin-AAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('plugin-AB'))
|
||||
t.assert.ok(!fastify.hasPlugin('plugin-B'))
|
||||
|
||||
fastify.register(fp((fastify, opts, done) => {
|
||||
t.ok(fastify.hasPlugin('plugin-AA'))
|
||||
t.ok(fastify.hasPlugin('plugin-A'))
|
||||
t.ok(fastify.hasPlugin('plugin-AAA'))
|
||||
t.notOk(fastify.hasPlugin('plugin-AB'))
|
||||
t.notOk(fastify.hasPlugin('plugin-B'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-AA'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-A'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-AAA'))
|
||||
t.assert.ok(!fastify.hasPlugin('plugin-AB'))
|
||||
t.assert.ok(!fastify.hasPlugin('plugin-B'))
|
||||
|
||||
done()
|
||||
}, { name: 'plugin-AAA' }))
|
||||
@@ -364,11 +410,11 @@ test('hasPlugin returns true when using no encapsulation', async t => {
|
||||
}, { name: 'plugin-A' }))
|
||||
|
||||
fastify.register(fp((fastify, opts, done) => {
|
||||
t.ok(fastify.hasPlugin('plugin-AA'))
|
||||
t.ok(fastify.hasPlugin('plugin-A'))
|
||||
t.ok(fastify.hasPlugin('plugin-AAA'))
|
||||
t.ok(fastify.hasPlugin('plugin-AB'))
|
||||
t.notOk(fastify.hasPlugin('plugin-B'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-AA'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-A'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-AAA'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-AB'))
|
||||
t.assert.ok(!fastify.hasPlugin('plugin-B'))
|
||||
|
||||
done()
|
||||
}, { name: 'plugin-AB' }))
|
||||
@@ -377,11 +423,11 @@ test('hasPlugin returns true when using no encapsulation', async t => {
|
||||
}, { name: 'plugin-AA' }))
|
||||
|
||||
fastify.register(fp((fastify, opts, done) => {
|
||||
t.ok(fastify.hasPlugin('plugin-AA'))
|
||||
t.ok(fastify.hasPlugin('plugin-A'))
|
||||
t.ok(fastify.hasPlugin('plugin-AAA'))
|
||||
t.ok(fastify.hasPlugin('plugin-AB'))
|
||||
t.ok(fastify.hasPlugin('plugin-B'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-AA'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-A'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-AAA'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-AB'))
|
||||
t.assert.ok(fastify.hasPlugin('plugin-B'))
|
||||
|
||||
done()
|
||||
}, { name: 'plugin-B' }))
|
||||
@@ -403,37 +449,40 @@ test('hasPlugin returns true when using encapsulation', async t => {
|
||||
fastify.register(plugin)
|
||||
|
||||
fastify.register(async (server) => {
|
||||
t.ok(server.hasPlugin(pluginName))
|
||||
t.assert.ok(server.hasPlugin(pluginName))
|
||||
})
|
||||
|
||||
fastify.register(async function foo (server) {
|
||||
server.register(async function bar (server) {
|
||||
t.ok(server.hasPlugin(pluginName))
|
||||
t.assert.ok(server.hasPlugin(pluginName))
|
||||
})
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
})
|
||||
|
||||
test('registering plugins with mixed style should return a warning', async t => {
|
||||
t.plan(12)
|
||||
test('registering anonymous plugin with mixed style should throw', async t => {
|
||||
t.plan(2)
|
||||
|
||||
const pluginNames = ['error-plugin', 'anonymous', 'anotherPlugin', 'anotherPluginNamed']
|
||||
const fastify = Fastify()
|
||||
|
||||
const oldWarnings = process.listeners('warning')
|
||||
process.removeAllListeners('warning')
|
||||
process.on('warning', onWarning)
|
||||
function onWarning (warning) {
|
||||
t.match(warning.message, new RegExp(`.*${pluginNames.shift()} plugin being registered mixes async and callback styles.*`))
|
||||
t.equal(warning.name, 'FastifyWarning')
|
||||
t.equal(warning.code, 'FSTWRN002')
|
||||
const anonymousPlugin = async (app, opts, done) => {
|
||||
done()
|
||||
}
|
||||
t.teardown(() => {
|
||||
process.removeListener('warning', onWarning)
|
||||
for (const warning of oldWarnings) {
|
||||
process.on('warning', warning)
|
||||
}
|
||||
})
|
||||
|
||||
fastify.register(anonymousPlugin)
|
||||
|
||||
try {
|
||||
await fastify.ready()
|
||||
t.fail('should throw')
|
||||
} catch (error) {
|
||||
t.assert.ok(error instanceof FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER)
|
||||
t.assert.strictEqual(error.message, 'The anonymousPlugin plugin being registered mixes async and callback styles. Async plugin should not mix async and callback style.')
|
||||
}
|
||||
})
|
||||
|
||||
test('registering named plugin with mixed style should throw', async t => {
|
||||
t.plan(2)
|
||||
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -441,23 +490,15 @@ test('registering plugins with mixed style should return a warning', async t =>
|
||||
const errorPlugin = async (app, opts, done) => {
|
||||
done()
|
||||
}
|
||||
|
||||
const namedPlugin = fp(errorPlugin, { name: pluginName })
|
||||
|
||||
async function anotherPlugin (app, opts, done) {
|
||||
done()
|
||||
}
|
||||
|
||||
const anotherPluginNamed = async function (app, opts, done) {
|
||||
done()
|
||||
}
|
||||
|
||||
fastify.register(namedPlugin)
|
||||
fastify.register(async (app, opts, done) => {
|
||||
done()
|
||||
})
|
||||
fastify.register(anotherPlugin)
|
||||
fastify.register(anotherPluginNamed)
|
||||
|
||||
await fastify.ready()
|
||||
try {
|
||||
await fastify.ready()
|
||||
t.fail('should throw')
|
||||
} catch (error) {
|
||||
t.assert.ok(error instanceof FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER)
|
||||
t.assert.strictEqual(error.message, 'The error-plugin plugin being registered mixes async and callback styles. Async plugin should not mix async and callback style.')
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user