diff --git a/fastify.js b/fastify.js index 4efb4c2816..354a94158a 100644 --- a/fastify.js +++ b/fastify.js @@ -47,7 +47,7 @@ const { buildRouting, validateBodyLimitOption } = require('./lib/route') const build404 = require('./lib/fourOhFour') const getSecuredInitialConfig = require('./lib/initialConfigValidation') const override = require('./lib/pluginOverride') -const warning = require('./lib/warnings') +const { FSTDEP009 } = require('./lib/warnings') const noopSet = require('./lib/noop-set') const { appendStackTrace, @@ -152,7 +152,7 @@ function fastify (options) { let constraints = options.constraints if (options.versioning) { - warning.emit('FSTDEP009') + FSTDEP009() constraints = { ...constraints, version: { diff --git a/lib/decorate.js b/lib/decorate.js index 2181c1c460..3fe7cc7b3f 100644 --- a/lib/decorate.js +++ b/lib/decorate.js @@ -16,7 +16,7 @@ const { FST_ERR_DEC_DEPENDENCY_INVALID_TYPE } = require('./errors') -const warning = require('./warnings') +const { FSTDEP006 } = require('./warnings') function decorate (instance, name, fn, dependencies) { if (Object.prototype.hasOwnProperty.call(instance, name)) { @@ -58,7 +58,7 @@ function decorateConstructor (konstructor, name, fn, dependencies) { function checkReferenceType (name, fn) { if (typeof fn === 'object' && fn && !(typeof fn.getter === 'function' || typeof fn.setter === 'function')) { - warning.emit('FSTDEP006', name) + FSTDEP006(name) } } diff --git a/lib/pluginUtils.js b/lib/pluginUtils.js index 9429a40c7d..28ae84dbad 100644 --- a/lib/pluginUtils.js +++ b/lib/pluginUtils.js @@ -11,7 +11,7 @@ const { FST_ERR_PLUGIN_VERSION_MISMATCH, FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE } = require('./errors') -const warning = require('./warnings.js') +const { FSTWRN002 } = require('./warnings.js') function getMeta (fn) { return fn[Symbol.for('plugin-meta')] @@ -138,7 +138,7 @@ function registerPluginName (fn) { function checkPluginHealthiness (fn, pluginName = 'anonymous') { if (fn.constructor.name === 'AsyncFunction' && fn.length === 3) { - warning.emit('FSTWRN002', pluginName) + FSTWRN002(pluginName) } } diff --git a/lib/reply.js b/lib/reply.js index b831bfca44..dfecb7ebd8 100644 --- a/lib/reply.js +++ b/lib/reply.js @@ -53,7 +53,7 @@ const { FST_ERR_MISSING_SERIALIZATION_FN, FST_ERR_MISSING_CONTENTTYPE_SERIALIZATION_FN } = require('./errors') -const warning = require('./warnings') +const { FSTDEP010, FSTDEP013, FSTDEP019 } = require('./warnings') function Reply (res, request, log) { this.raw = res @@ -80,7 +80,7 @@ Object.defineProperties(Reply.prototype, { // Is temporary to avoid constant conflicts between `next` and `main` context: { get () { - warning.emit('FSTDEP019') + FSTDEP019() return this.request[kRouteContext] } }, @@ -96,7 +96,7 @@ Object.defineProperties(Reply.prototype, { return (this[kReplyHijacked] || this.raw.writableEnded) === true }, set (value) { - warning.emit('FSTDEP010') + FSTDEP010() if (value !== true) { throw new FST_ERR_REP_SENT_VALUE() @@ -748,7 +748,7 @@ function sendTrailer (payload, res, reply) { result.then((v) => cb(null, v), cb) } else if (result !== null && result !== undefined) { // TODO: should be removed in fastify@5 - warning.emit('FSTDEP013') + FSTDEP013() cb(null, result) } } diff --git a/lib/request.js b/lib/request.js index 46c3174c23..d334099f0d 100644 --- a/lib/request.js +++ b/lib/request.js @@ -2,7 +2,14 @@ const proxyAddr = require('proxy-addr') const semver = require('semver') -const warning = require('./warnings') +const { + FSTDEP005, + FSTDEP012, + FSTDEP015, + FSTDEP016, + FSTDEP017, + FSTDEP018 +} = require('./warnings') const { kHasBeenDecorated, kSchemaBody, @@ -166,13 +173,13 @@ Object.defineProperties(Request.prototype, { }, context: { get () { - warning.emit('FSTDEP012') + FSTDEP012() return this[kRouteContext] } }, routerPath: { get () { - warning.emit('FSTDEP017') + FSTDEP017() return this[kRouteContext].config?.url } }, @@ -208,19 +215,19 @@ Object.defineProperties(Request.prototype, { }, routerMethod: { get () { - warning.emit('FSTDEP018') + FSTDEP018() return this[kRouteContext].config?.method } }, routeConfig: { get () { - warning.emit('FSTDEP016') + FSTDEP016() return this[kRouteContext][kPublicRouteContext]?.config } }, routeSchema: { get () { - warning.emit('FSTDEP015') + FSTDEP015() return this[kRouteContext][kPublicRouteContext].schema } }, @@ -233,7 +240,7 @@ Object.defineProperties(Request.prototype, { get () { /* istanbul ignore next */ if (semver.gte(process.versions.node, '13.0.0')) { - warning.emit('FSTDEP005') + FSTDEP005() } return this.raw.connection } diff --git a/lib/route.js b/lib/route.js index c1e36e6d99..61f47cd114 100644 --- a/lib/route.js +++ b/lib/route.js @@ -7,7 +7,11 @@ const { onRequestAbortHookRunner, lifecycleHooks, preParsingHookRunner, onTimeou const { supportedMethods } = require('./httpMethods') const { normalizeSchema } = require('./schemas') const { parseHeadOnSendHandlers } = require('./headRoute') -const warning = require('./warnings') +const { + FSTDEP007, + FSTDEP008, + FSTDEP014 +} = require('./warnings') const { compileSchemasForValidation, @@ -100,11 +104,11 @@ function buildRouting (options) { hasRoute, prepareRoute, getDefaultRoute: function () { - warning.emit('FSTDEP014') + FSTDEP014() return router.defaultRoute }, setDefaultRoute: function (defaultRoute) { - warning.emit('FSTDEP014') + FSTDEP014() if (typeof defaultRoute !== 'function') { throw new FST_ERR_DEFAULT_ROUTE_INVALID_TYPE() } @@ -324,7 +328,7 @@ function buildRouting (options) { }) if (opts.version) { - warning.emit('FSTDEP008') + FSTDEP008() constraints.version = opts.version } @@ -417,7 +421,7 @@ function buildRouting (options) { const onSendHandlers = parseHeadOnSendHandlers(headOpts.onSend) prepareRoute.call(this, { method: 'HEAD', url: path, options: { ...headOpts, onSend: onSendHandlers }, isFastify: true }) } else if (hasHEADHandler && exposeHeadRoute) { - warning.emit('FSTDEP007') + FSTDEP007() } } } diff --git a/lib/server.js b/lib/server.js index ed516e7b3f..1d7dec8210 100644 --- a/lib/server.js +++ b/lib/server.js @@ -4,7 +4,7 @@ const http = require('node:http') const https = require('node:https') const dns = require('node:dns') -const warnings = require('./warnings') +const { FSTDEP011 } = require('./warnings') const { kState, kOptions, kServerBindings } = require('./symbols') const { onListenHookRunner } = require('./hooks') const { @@ -36,12 +36,12 @@ function createServer (options, httpHandler) { if (arguments.length === 0) { listenOptions = normalizeListenArgs([]) } else if (arguments.length > 0 && (firstArgType !== '[object Object]' && firstArgType !== '[object Function]')) { - warnings.emit('FSTDEP011') + FSTDEP011() listenOptions = normalizeListenArgs(Array.from(arguments)) cb = listenOptions.cb } else if (args.length > 1) { // `.listen(obj, a, ..., n, callback )` - warnings.emit('FSTDEP011') + FSTDEP011() // Deal with `.listen(port, host, backlog, [cb])` const hostPath = listenOptions.path ? [listenOptions.path] : [listenOptions.port ?? 0, listenOptions.host ?? 'localhost'] Object.assign(listenOptions, normalizeListenArgs([...hostPath, ...args])) diff --git a/lib/validation.js b/lib/validation.js index fe684dff30..a83d948cbf 100644 --- a/lib/validation.js +++ b/lib/validation.js @@ -13,7 +13,7 @@ const { FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX } = require('./errors') -const warning = require('./warnings') +const { FSTWRN001 } = require('./warnings') function compileSchemasForSerialization (context, compile) { if (!context.schema || !context.schema.response) { @@ -83,25 +83,25 @@ function compileSchemasForValidation (context, compile, isCustom) { } context[headersSchema] = compile({ schema: headersSchemaLowerCase, method, url, httpPart: 'headers' }) } else if (Object.prototype.hasOwnProperty.call(schema, 'headers')) { - warning.emit('FSTWRN001', 'headers', method, url) + FSTWRN001('headers', method, url) } if (schema.body) { context[bodySchema] = compile({ schema: schema.body, method, url, httpPart: 'body' }) } else if (Object.prototype.hasOwnProperty.call(schema, 'body')) { - warning.emit('FSTWRN001', 'body', method, url) + FSTWRN001('body', method, url) } if (schema.querystring) { context[querystringSchema] = compile({ schema: schema.querystring, method, url, httpPart: 'querystring' }) } else if (Object.prototype.hasOwnProperty.call(schema, 'querystring')) { - warning.emit('FSTWRN001', 'querystring', method, url) + FSTWRN001('querystring', method, url) } if (schema.params) { context[paramsSchema] = compile({ schema: schema.params, method, url, httpPart: 'params' }) } else if (Object.prototype.hasOwnProperty.call(schema, 'params')) { - warning.emit('FSTWRN001', 'params', method, url) + FSTWRN001('params', method, url) } } diff --git a/lib/warnings.js b/lib/warnings.js index 5ce1ad452e..609f73b5d7 100644 --- a/lib/warnings.js +++ b/lib/warnings.js @@ -1,60 +1,112 @@ 'use strict' -const warning = require('process-warning')() - -/** - * Deprecation codes: - * - FSTDEP005 - * - FSTDEP006 - * - FSTDEP007 - * - FSTDEP008 - * - FSTDEP009 - * - FSTDEP010 - * - FSTDEP011 - * - FSTDEP012 - * - FSTDEP013 - * - FSTDEP014 - * - FSTDEP015 - * - FSTDEP016 - * - FSTDEP017 - * - FSTDEP018 - * - FSTDEP019 - * - FSTWRN001 - * - FSTWRN002 - */ - -warning.createDeprecation('FSTDEP005', 'You are accessing the deprecated "request.connection" property. Use "request.socket" instead.') - -warning.createDeprecation('FSTDEP006', 'You are decorating Request/Reply with a reference type. This reference is shared amongst all requests. Use onRequest hook instead. Property: %s') - -warning.createDeprecation('FSTDEP007', 'You are trying to set a HEAD route using "exposeHeadRoute" route flag when a sibling route is already set. See documentation for more info.') - -warning.createDeprecation('FSTDEP008', 'You are using route constraints via the route { version: "..." } option, use { constraints: { version: "..." } } option instead.') - -warning.createDeprecation('FSTDEP009', 'You are using a custom route versioning strategy via the server { versioning: "..." } option, use { constraints: { version: "..." } } option instead.') - -warning.createDeprecation('FSTDEP010', 'Modifying the "reply.sent" property is deprecated. Use the "reply.hijack()" method instead.') - -warning.createDeprecation('FSTDEP011', 'Variadic listen method is deprecated. Please use ".listen(optionsObject)" instead. The variadic signature will be removed in `fastify@5`.') - -warning.createDeprecation('FSTDEP012', 'request.context property access is deprecated. Please use "request.routeOptions.config" or "request.routeOptions.schema" instead for accessing Route settings. The "request.context" will be removed in `fastify@5`.') - -warning.createDeprecation('FSTDEP013', 'Direct return of "trailers" function is deprecated. Please use "callback" or "async-await" for return value. The support of direct return will removed in `fastify@5`.') - -warning.createDeprecation('FSTDEP014', 'You are trying to set/access the default route. This property is deprecated. Please, use setNotFoundHandler if you want to custom a 404 handler or the wildcard (*) to match all routes.') - -warning.createDeprecation('FSTDEP015', 'You are accessing the deprecated "request.routeSchema" property. Use "request.routeOptions.schema" instead. Property "req.routeSchema" will be removed in `fastify@5`.') - -warning.createDeprecation('FSTDEP016', 'You are accessing the deprecated "request.routeConfig" property. Use "request.routeOptions.config" instead. Property "req.routeConfig" will be removed in `fastify@5`.') - -warning.createDeprecation('FSTDEP017', 'You are accessing the deprecated "request.routerPath" property. Use "request.routeOptions.url" instead. Property "req.routerPath" will be removed in `fastify@5`.') - -warning.createDeprecation('FSTDEP018', 'You are accessing the deprecated "request.routerMethod" property. Use "request.routeOptions.method" instead. Property "req.routerMethod" will be removed in `fastify@5`.') - -warning.createDeprecation('FSTDEP019', 'reply.context property access is deprecated. Please use "request.routeOptions.config" or "request.routeOptions.schema" instead for accessing Route settings. The "reply.context" will be removed in `fastify@5`.') - -warning.create('FastifyWarning', 'FSTWRN001', 'The %s schema for %s: %s is missing. This may indicate the schema is not well specified.', { unlimited: true }) - -warning.create('FastifyWarning', 'FSTWRN002', 'The %s plugin being registered mixes async and callback styles, which will result in an error in `fastify@5`', { unlimited: true }) - -module.exports = warning +const { createDeprecation, createWarning } = require('process-warning') + +const FSTDEP005 = createDeprecation({ + code: 'FSTDEP005', + message: 'You are accessing the deprecated "request.connection" property. Use "request.socket" instead.' +}) + +const FSTDEP006 = createDeprecation({ + code: 'FSTDEP006', + message: 'You are decorating Request/Reply with a reference type. This reference is shared amongst all requests. Use onRequest hook instead. Property: %s' +}) + +const FSTDEP007 = createDeprecation({ + code: 'FSTDEP007', + message: 'You are trying to set a HEAD route using "exposeHeadRoute" route flag when a sibling route is already set. See documentation for more info.' +}) + +const FSTDEP008 = createDeprecation({ + code: 'FSTDEP008', + message: 'You are using route constraints via the route { version: "..." } option, use { constraints: { version: "..." } } option instead.' +}) + +const FSTDEP009 = createDeprecation({ + code: 'FSTDEP009', + message: 'You are using a custom route versioning strategy via the server { versioning: "..." } option, use { constraints: { version: "..." } } option instead.' +}) + +const FSTDEP010 = createDeprecation({ + code: 'FSTDEP010', + message: 'Modifying the "reply.sent" property is deprecated. Use the "reply.hijack()" method instead.' +}) + +const FSTDEP011 = createDeprecation({ + code: 'FSTDEP011', + message: 'Variadic listen method is deprecated. Please use ".listen(optionsObject)" instead. The variadic signature will be removed in `fastify@5`.' +}) + +const FSTDEP012 = createDeprecation({ + code: 'FSTDEP012', + message: 'request.context property access is deprecated. Please use "request.routeOptions.config" or "request.routeOptions.schema" instead for accessing Route settings. The "request.context" will be removed in `fastify@5`.' +}) + +const FSTDEP013 = createDeprecation({ + code: 'FSTDEP013', + message: 'Direct return of "trailers" function is deprecated. Please use "callback" or "async-await" for return value. The support of direct return will removed in `fastify@5`.' +}) + +const FSTDEP014 = createDeprecation({ + code: 'FSTDEP014', + message: 'You are trying to set/access the default route. This property is deprecated. Please, use setNotFoundHandler if you want to custom a 404 handler or the wildcard (*) to match all routes.' +}) + +const FSTDEP015 = createDeprecation({ + code: 'FSTDEP015', + message: 'You are accessing the deprecated "request.routeSchema" property. Use "request.routeOptions.schema" instead. Property "req.routeSchema" will be removed in `fastify@5`.' +}) + +const FSTDEP016 = createDeprecation({ + code: 'FSTDEP016', + message: 'You are accessing the deprecated "request.routeConfig" property. Use "request.routeOptions.config" instead. Property "req.routeConfig" will be removed in `fastify@5`.' +}) + +const FSTDEP017 = createDeprecation({ + code: 'FSTDEP017', + message: 'You are accessing the deprecated "request.routerPath" property. Use "request.routeOptions.url" instead. Property "req.routerPath" will be removed in `fastify@5`.' +}) + +const FSTDEP018 = createDeprecation({ + code: 'FSTDEP018', + message: 'You are accessing the deprecated "request.routerMethod" property. Use "request.routeOptions.method" instead. Property "req.routerMethod" will be removed in `fastify@5`.' +}) + +const FSTDEP019 = createDeprecation({ + code: 'FSTDEP019', + message: 'reply.context property access is deprecated. Please use "request.routeOptions.config" or "request.routeOptions.schema" instead for accessing Route settings. The "reply.context" will be removed in `fastify@5`.' +}) + +const FSTWRN001 = createWarning({ + name: 'FastifyWarning', + code: 'FSTWRN001', + message: 'The %s schema for %s: %s is missing. This may indicate the schema is not well specified.', + unlimited: true +}) + +const FSTWRN002 = createWarning({ + name: 'FastifyWarning', + code: 'FSTWRN002', + message: 'The %s plugin being registered mixes async and callback styles, which will result in an error in `fastify@5`', + unlimited: true +}) + +module.exports = { + FSTDEP005, + FSTDEP006, + FSTDEP007, + FSTDEP008, + FSTDEP009, + FSTDEP010, + FSTDEP011, + FSTDEP012, + FSTDEP013, + FSTDEP014, + FSTDEP015, + FSTDEP016, + FSTDEP017, + FSTDEP018, + FSTDEP019, + FSTWRN001, + FSTWRN002 +} diff --git a/package.json b/package.json index 3fd2e80cf7..84863318a8 100644 --- a/package.json +++ b/package.json @@ -198,7 +198,7 @@ "find-my-way": "^7.7.0", "light-my-request": "^5.11.0", "pino": "^8.16.0", - "process-warning": "^2.3.0", + "process-warning": "^3.0.0", "proxy-addr": "^2.0.7", "rfdc": "^1.3.0", "secure-json-parse": "^2.7.0", diff --git a/test/decorator.test.js b/test/decorator.test.js index d36e756839..f16ba772fb 100644 --- a/test/decorator.test.js +++ b/test/decorator.test.js @@ -790,31 +790,33 @@ test('decorate* should throw if called after ready', async t => { }) test('decorate* should emit warning if an array is passed', t => { - t.plan(2) - function onWarning (code, name) { + t.plan(1) + + function onWarning (name) { t.equal(name, 'test_array') - t.equal(code, 'FSTDEP006') - } - const warning = { - emit: onWarning } - const decorate = proxyquire('../lib/decorate', { './warnings': warning }) + const decorate = proxyquire('../lib/decorate', { + './warnings': { + FSTDEP006: onWarning + } + }) const fastify = proxyquire('..', { './lib/decorate.js': decorate })() fastify.decorateRequest('test_array', []) }) test('decorate* should emit warning if object type is passed', t => { - t.plan(2) - function onWarning (code, name) { + t.plan(1) + + function onWarning (name) { t.equal(name, 'test_object') - t.equal(code, 'FSTDEP006') - } - const warning = { - emit: onWarning } - const decorate = proxyquire('../lib/decorate', { './warnings': warning }) + const decorate = proxyquire('../lib/decorate', { + './warnings': { + FSTDEP006: onWarning + } + }) const fastify = proxyquire('..', { './lib/decorate.js': decorate })() fastify.decorateRequest('test_object', { foo: 'bar' }) }) @@ -823,10 +825,12 @@ test('decorate* should not emit warning if object with getter/setter is passed', function onWarning (warning) { t.fail('Should not call a warn') } - const warning = { - emit: onWarning - } - const decorate = proxyquire('../lib/decorate', { './warnings': warning }) + + const decorate = proxyquire('../lib/decorate', { + './warnings': { + FSTDEP006: onWarning + } + }) const fastify = proxyquire('..', { './lib/decorate.js': decorate })() fastify.decorateRequest('test_getter_setter', { @@ -844,11 +848,12 @@ test('decorate* should not emit warning if string,bool,numbers are passed', t => function onWarning (warning) { t.fail('Should not call a warn') } - const warning = { - emit: onWarning - } - const decorate = proxyquire('../lib/decorate', { './warnings': warning }) + const decorate = proxyquire('../lib/decorate', { + './warnings': { + FSTDEP006: onWarning + } + }) const fastify = proxyquire('..', { './lib/decorate.js': decorate })() fastify.decorateRequest('test_str', 'foo') diff --git a/test/default-route.test.js b/test/default-route.test.js index fda35b940f..78557385e7 100644 --- a/test/default-route.test.js +++ b/test/default-route.test.js @@ -3,7 +3,7 @@ const t = require('tap') const test = t.test const Fastify = require('..') -const warning = require('../lib/warnings') +const { FSTDEP014 } = require('../lib/warnings') // Silence the standard warning logs. We will test the messages explicitly. process.removeAllListeners('warning') @@ -19,12 +19,12 @@ test('setDefaultRoute should emit a deprecation warning', t => { process.on('warning', onWarning) function onWarning (warning) { t.equal(warning.name, 'DeprecationWarning') - t.equal(warning.code, 'FSTDEP014') + t.equal(warning.code, FSTDEP014.code) } t.teardown(() => { process.removeListener('warning', onWarning) - warning.emitted.set('FSTDEP014', false) + FSTDEP014.emitted = false }) fastify.setDefaultRoute(defaultRoute) @@ -38,12 +38,12 @@ test('getDefaultRoute should emit a deprecation warning', t => { process.on('warning', onWarning) function onWarning (warning) { t.equal(warning.name, 'DeprecationWarning') - t.equal(warning.code, 'FSTDEP014') + t.equal(warning.code, FSTDEP014.code) } t.teardown(() => { process.removeListener('warning', onWarning) - warning.emitted.set('FSTDEP014', false) + FSTDEP014.emitted = false }) fastify.getDefaultRoute() diff --git a/test/internals/reply.test.js b/test/internals/reply.test.js index 72aff8aa3b..cfd40c015e 100644 --- a/test/internals/reply.test.js +++ b/test/internals/reply.test.js @@ -19,7 +19,7 @@ const { } = require('../../lib/symbols') const fs = require('node:fs') const path = require('node:path') -const warning = require('../../lib/warnings') +const { FSTDEP019, FSTDEP010 } = require('../../lib/warnings') const agent = new http.Agent({ keepAlive: false }) @@ -1466,14 +1466,13 @@ test('should emit deprecation warning when trying to modify the reply.sent prope t.plan(4) const fastify = Fastify() - const deprecationCode = 'FSTDEP010' - warning.emitted.delete(deprecationCode) + FSTDEP010.emitted = false process.removeAllListeners('warning') process.on('warning', onWarning) function onWarning (warning) { t.equal(warning.name, 'DeprecationWarning') - t.equal(warning.code, deprecationCode) + t.equal(warning.code, FSTDEP010.code) } fastify.get('/', (req, reply) => { @@ -1494,14 +1493,13 @@ test('should emit deprecation warning when trying to use the reply.context.confi t.plan(4) const fastify = Fastify() - const deprecationCode = 'FSTDEP019' - warning.emitted.delete(deprecationCode) + FSTDEP019.emitted = false process.removeAllListeners('warning') process.on('warning', onWarning) function onWarning (warning) { t.equal(warning.name, 'DeprecationWarning') - t.equal(warning.code, deprecationCode) + t.equal(warning.code, FSTDEP019.code) } fastify.get('/', (req, reply) => { diff --git a/test/route.7.test.js b/test/route.7.test.js index aeacecf2a6..536eec5670 100644 --- a/test/route.7.test.js +++ b/test/route.7.test.js @@ -178,14 +178,15 @@ test('Set a custom HEAD route before GET one without disabling exposeHeadRoutes test('Set a custom HEAD route before GET one without disabling exposeHeadRoutes (route)', t => { t.plan(7) - function onWarning (code) { - t.equal(code, 'FSTDEP007') - } - const warning = { - emit: onWarning + function onWarning () { + t.pass('warning emitted') } - const route = proxyquire('../lib/route', { './warnings': warning }) + const route = proxyquire('../lib/route', { + './warnings': { + FSTDEP007: onWarning + } + }) const fastify = proxyquire('..', { './lib/route.js': route })() const resBuffer = Buffer.from('I am a coffee!') diff --git a/test/schema-feature.test.js b/test/schema-feature.test.js index 6cc9df2949..8bf208ed31 100644 --- a/test/schema-feature.test.js +++ b/test/schema-feature.test.js @@ -6,7 +6,7 @@ const fp = require('fastify-plugin') const deepClone = require('rfdc')({ circles: true, proto: false }) const Ajv = require('ajv') const { kSchemaController } = require('../lib/symbols.js') -const warning = require('../lib/warnings') +const { FSTWRN001 } = require('../lib/warnings') const echoParams = (req, reply) => { reply.send(req.params) } const echoBody = (req, reply) => { reply.send(req.body) } @@ -261,12 +261,12 @@ test('Should emit warning if the schema headers is undefined', t => { process.on('warning', onWarning) function onWarning (warning) { t.equal(warning.name, 'FastifyWarning') - t.equal(warning.code, 'FSTWRN001') + t.equal(warning.code, FSTWRN001.code) } t.teardown(() => { process.removeListener('warning', onWarning) - warning.emitted.set('FSTWRN001', false) + FSTWRN001.emitted = false }) fastify.post('/:id', { @@ -292,12 +292,12 @@ test('Should emit warning if the schema body is undefined', t => { process.on('warning', onWarning) function onWarning (warning) { t.equal(warning.name, 'FastifyWarning') - t.equal(warning.code, 'FSTWRN001') + t.equal(warning.code, FSTWRN001.code) } t.teardown(() => { process.removeListener('warning', onWarning) - warning.emitted.set('FSTWRN001', false) + FSTWRN001.emitted = false }) fastify.post('/:id', { @@ -323,12 +323,12 @@ test('Should emit warning if the schema query is undefined', t => { process.on('warning', onWarning) function onWarning (warning) { t.equal(warning.name, 'FastifyWarning') - t.equal(warning.code, 'FSTWRN001') + t.equal(warning.code, FSTWRN001.code) } t.teardown(() => { process.removeListener('warning', onWarning) - warning.emitted.set('FSTWRN001', false) + FSTWRN001.emitted = false }) fastify.post('/:id', { @@ -354,12 +354,12 @@ test('Should emit warning if the schema params is undefined', t => { process.on('warning', onWarning) function onWarning (warning) { t.equal(warning.name, 'FastifyWarning') - t.equal(warning.code, 'FSTWRN001') + t.equal(warning.code, FSTWRN001.code) } t.teardown(() => { process.removeListener('warning', onWarning) - warning.emitted.set('FSTWRN001', false) + FSTWRN001.emitted = false }) fastify.post('/:id', { @@ -390,14 +390,14 @@ test('Should emit a warning for every route with undefined schema', t => { // => 3 x 4 assertions = 12 assertions function onWarning (warning) { t.equal(warning.name, 'FastifyWarning') - t.equal(warning.code, 'FSTWRN001') + t.equal(warning.code, FSTWRN001.code) t.equal(runs++, expectedWarningEmitted.shift()) } process.on('warning', onWarning) t.teardown(() => { process.removeListener('warning', onWarning) - warning.emitted.set('FSTWRN001', false) + FSTWRN001.emitted = false }) fastify.get('/undefinedParams/:id', { diff --git a/test/versioned-routes.test.js b/test/versioned-routes.test.js index 25954dcb03..7d1e39ae67 100644 --- a/test/versioned-routes.test.js +++ b/test/versioned-routes.test.js @@ -663,14 +663,15 @@ test('Vary header check (for documentation example)', t => { test('Should trigger a warning when a versioned route is registered via version option', t => { t.plan(4) - function onWarning (code) { - t.equal(code, 'FSTDEP008') - } - const warning = { - emit: onWarning + function onWarning () { + t.pass('FSTDEP008 has been emitted') } - const route = proxyquire('../lib/route', { './warnings': warning }) + const route = proxyquire('../lib/route', { + './warnings': { + FSTDEP008: onWarning + } + }) const fastify = proxyquire('..', { './lib/route.js': route })({ exposeHeadRoutes: false }) fastify.route({