diff --git a/test/hooks.test.js b/test/hooks.test.js index 32cbf2a..536140c 100644 --- a/test/hooks.test.js +++ b/test/hooks.test.js @@ -126,17 +126,15 @@ test('catch all added schema', async t => { fastify.addSchema({ $id: 'Root', type: 'object', properties: {} }) - fastify.register(function (instance, _, done) { + fastify.register(async function (instance) { instance.addSchema({ $id: 'Instance', type: 'object', properties: {} }) - instance.register(function (instance, _, done) { + await instance.register(async function (instance) { instance.addSchema({ $id: 'Sub-Instance', type: 'object', properties: {} }) - done() }) - done() }) await fastify.ready() - const openapi = await fastify.swagger() + const openapi = fastify.swagger() t.same(Object.keys(openapi.components.schemas), ['Root', 'Instance', 'Sub-Instance']) }) diff --git a/test/integration.test.js b/test/integration.test.js index f273c8e..18d47fd 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -18,15 +18,16 @@ test('fastify will response swagger csp', async (t) => { await fastify.register(fastifySwagger) await fastify.register(fastifySwaggerUi) - await fastify.register(fastifyHelmet, instance => { - return { - contentSecurityPolicy: { - directives: { - defaultSrc: ["'self'"], - imgSrc: ["'self'", 'data:', 'validator.swagger.io'], - scriptSrc: ["'self'"].concat(instance.swaggerCSP.script), - styleSrc: ["'self'", 'https:'].concat(instance.swaggerCSP.style) - } + + const scriptSrc = ["'self'"].concat(fastify.swaggerCSP.script) + const styleSrc = ["'self'", 'https:'].concat(fastify.swaggerCSP.style) + await fastify.register(fastifyHelmet, { + contentSecurityPolicy: { + directives: { + defaultSrc: ["'self'"], + imgSrc: ["'self'", 'data:', 'validator.swagger.io'], + scriptSrc, + styleSrc } } }) diff --git a/test/static.test.js b/test/static.test.js index 25f40d6..163a13a 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -9,6 +9,8 @@ const readFileSync = require('node:fs').readFileSync const resolve = require('node:path').resolve test('swagger route returns yaml', async (t) => { + t.plan(3) + const config = { mode: 'static', specification: { @@ -16,7 +18,6 @@ test('swagger route returns yaml', async (t) => { } } - t.plan(3) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi) @@ -34,6 +35,8 @@ test('swagger route returns yaml', async (t) => { }) test('swagger route returns json', async (t) => { + t.plan(3) + const config = { mode: 'static', specification: { @@ -41,7 +44,6 @@ test('swagger route returns json', async (t) => { } } - t.plan(3) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi) @@ -59,6 +61,8 @@ test('swagger route returns json', async (t) => { }) test('postProcessor works, swagger route returns updated yaml', async (t) => { + t.plan(4) + const config = { mode: 'static', specification: { @@ -70,7 +74,6 @@ test('postProcessor works, swagger route returns updated yaml', async (t) => { } } - t.plan(4) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi) @@ -89,6 +92,8 @@ test('postProcessor works, swagger route returns updated yaml', async (t) => { }) test('swagger route returns explicitly passed doc', async (t) => { + t.plan(2) + const document = { info: { title: 'Test swagger', @@ -104,7 +109,6 @@ test('swagger route returns explicitly passed doc', async (t) => { } } - t.plan(2) const fastify = Fastify() await fastify.register(fastifySwagger, config) @@ -120,6 +124,8 @@ test('swagger route returns explicitly passed doc', async (t) => { }) test('/documentation/:file should serve static file from the location of main specification file', async (t) => { + t.plan(4) + const config = { mode: 'static', specification: { @@ -131,7 +137,6 @@ test('/documentation/:file should serve static file from the location of main sp baseDir: resolve(__dirname, '..', 'examples') } - t.plan(4) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi, uiConfig) @@ -172,6 +177,8 @@ test('/documentation/:file should serve static file from the location of main sp }) test('/documentation/non-existing-file calls custom NotFoundHandler', async (t) => { + t.plan(1) + const config = { mode: 'static', specification: { @@ -179,7 +186,6 @@ test('/documentation/non-existing-file calls custom NotFoundHandler', async (t) } } - t.plan(1) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi) @@ -196,6 +202,8 @@ test('/documentation/non-existing-file calls custom NotFoundHandler', async (t) }) test('/documentation/:file should be served from custom location', async (t) => { + t.plan(2) + const config = { mode: 'static', specification: { @@ -208,7 +216,6 @@ test('/documentation/:file should be served from custom location', async (t) => baseDir: resolve(__dirname, '..', 'static') } - t.plan(2) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi, uiConfig) @@ -227,6 +234,8 @@ test('/documentation/:file should be served from custom location', async (t) => }) test('/documentation/:file should be served from custom location with trailing slash(es)', async (t) => { + t.plan(2) + const config = { mode: 'static', specification: { @@ -238,7 +247,6 @@ test('/documentation/:file should be served from custom location with trailing s baseDir: resolve(__dirname, '..', 'static') + '/' } - t.plan(2) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi, uiConfig) @@ -256,6 +264,8 @@ test('/documentation/:file should be served from custom location with trailing s }) test('/documentation/yaml returns cache.swaggerString on second request in static mode', async (t) => { + t.plan(6) + const config = { mode: 'static', specification: { @@ -263,7 +273,6 @@ test('/documentation/yaml returns cache.swaggerString on second request in stati } } - t.plan(6) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi) @@ -294,6 +303,8 @@ test('/documentation/yaml returns cache.swaggerString on second request in stati }) test('/documentation/json returns cache.swaggerObject on second request in static mode', async (t) => { + t.plan(6) + const config = { mode: 'static', specification: { @@ -301,7 +312,6 @@ test('/documentation/json returns cache.swaggerObject on second request in stati } } - t.plan(6) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi) @@ -330,13 +340,14 @@ test('/documentation/json returns cache.swaggerObject on second request in stati }) test('/documentation/yaml returns cache.swaggerString on second request in dynamic mode', async (t) => { + t.plan(6) + const config = { specification: { path: './examples/example-static-specification.yaml' } } - t.plan(6) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi) @@ -367,13 +378,14 @@ test('/documentation/yaml returns cache.swaggerString on second request in dynam }) test('/documentation/json returns cache.swaggerObject on second request in dynamic mode', async (t) => { + t.plan(6) + const config = { specification: { path: './examples/example-static-specification.json' } } - t.plan(6) const fastify = Fastify() await fastify.register(fastifySwagger, config) await fastify.register(fastifySwaggerUi)