Skip to content

Commit 676c150

Browse files
committed
restructure some tests
1 parent 922567d commit 676c150

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

test/hooks.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,15 @@ test('catch all added schema', async t => {
126126

127127
fastify.addSchema({ $id: 'Root', type: 'object', properties: {} })
128128

129-
fastify.register(function (instance, _, done) {
129+
fastify.register(async function (instance) {
130130
instance.addSchema({ $id: 'Instance', type: 'object', properties: {} })
131131

132-
instance.register(function (instance, _, done) {
132+
await instance.register(async function (instance) {
133133
instance.addSchema({ $id: 'Sub-Instance', type: 'object', properties: {} })
134-
done()
135134
})
136-
done()
137135
})
138136

139137
await fastify.ready()
140-
const openapi = await fastify.swagger()
138+
const openapi = fastify.swagger()
141139
t.same(Object.keys(openapi.components.schemas), ['Root', 'Instance', 'Sub-Instance'])
142140
})

test/integration.test.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ test('fastify will response swagger csp', async (t) => {
1818

1919
await fastify.register(fastifySwagger)
2020
await fastify.register(fastifySwaggerUi)
21-
await fastify.register(fastifyHelmet, instance => {
22-
return {
23-
contentSecurityPolicy: {
24-
directives: {
25-
defaultSrc: ["'self'"],
26-
imgSrc: ["'self'", 'data:', 'validator.swagger.io'],
27-
scriptSrc: ["'self'"].concat(instance.swaggerCSP.script),
28-
styleSrc: ["'self'", 'https:'].concat(instance.swaggerCSP.style)
29-
}
21+
22+
const scriptSrc = ["'self'"].concat(fastify.swaggerCSP.script)
23+
const styleSrc = ["'self'", 'https:'].concat(fastify.swaggerCSP.style)
24+
await fastify.register(fastifyHelmet, {
25+
contentSecurityPolicy: {
26+
directives: {
27+
defaultSrc: ["'self'"],
28+
imgSrc: ["'self'", 'data:', 'validator.swagger.io'],
29+
scriptSrc,
30+
styleSrc
3031
}
3132
}
3233
})

test/static.test.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ const readFileSync = require('node:fs').readFileSync
99
const resolve = require('node:path').resolve
1010

1111
test('swagger route returns yaml', async (t) => {
12+
t.plan(3)
13+
1214
const config = {
1315
mode: 'static',
1416
specification: {
1517
path: './examples/example-static-specification.yaml'
1618
}
1719
}
1820

19-
t.plan(3)
2021
const fastify = Fastify()
2122
await fastify.register(fastifySwagger, config)
2223
await fastify.register(fastifySwaggerUi)
@@ -34,14 +35,15 @@ test('swagger route returns yaml', async (t) => {
3435
})
3536

3637
test('swagger route returns json', async (t) => {
38+
t.plan(3)
39+
3740
const config = {
3841
mode: 'static',
3942
specification: {
4043
path: './examples/example-static-specification.json'
4144
}
4245
}
4346

44-
t.plan(3)
4547
const fastify = Fastify()
4648
await fastify.register(fastifySwagger, config)
4749
await fastify.register(fastifySwaggerUi)
@@ -59,6 +61,8 @@ test('swagger route returns json', async (t) => {
5961
})
6062

6163
test('postProcessor works, swagger route returns updated yaml', async (t) => {
64+
t.plan(4)
65+
6266
const config = {
6367
mode: 'static',
6468
specification: {
@@ -70,7 +74,6 @@ test('postProcessor works, swagger route returns updated yaml', async (t) => {
7074
}
7175
}
7276

73-
t.plan(4)
7477
const fastify = Fastify()
7578
await fastify.register(fastifySwagger, config)
7679
await fastify.register(fastifySwaggerUi)
@@ -89,6 +92,8 @@ test('postProcessor works, swagger route returns updated yaml', async (t) => {
8992
})
9093

9194
test('swagger route returns explicitly passed doc', async (t) => {
95+
t.plan(2)
96+
9297
const document = {
9398
info: {
9499
title: 'Test swagger',
@@ -104,7 +109,6 @@ test('swagger route returns explicitly passed doc', async (t) => {
104109
}
105110
}
106111

107-
t.plan(2)
108112
const fastify = Fastify()
109113
await fastify.register(fastifySwagger, config)
110114

@@ -120,6 +124,8 @@ test('swagger route returns explicitly passed doc', async (t) => {
120124
})
121125

122126
test('/documentation/:file should serve static file from the location of main specification file', async (t) => {
127+
t.plan(4)
128+
123129
const config = {
124130
mode: 'static',
125131
specification: {
@@ -131,7 +137,6 @@ test('/documentation/:file should serve static file from the location of main sp
131137
baseDir: resolve(__dirname, '..', 'examples')
132138
}
133139

134-
t.plan(4)
135140
const fastify = Fastify()
136141
await fastify.register(fastifySwagger, config)
137142
await fastify.register(fastifySwaggerUi, uiConfig)
@@ -172,14 +177,15 @@ test('/documentation/:file should serve static file from the location of main sp
172177
})
173178

174179
test('/documentation/non-existing-file calls custom NotFoundHandler', async (t) => {
180+
t.plan(1)
181+
175182
const config = {
176183
mode: 'static',
177184
specification: {
178185
path: './examples/example-static-specification.yaml'
179186
}
180187
}
181188

182-
t.plan(1)
183189
const fastify = Fastify()
184190
await fastify.register(fastifySwagger, config)
185191
await fastify.register(fastifySwaggerUi)
@@ -196,6 +202,8 @@ test('/documentation/non-existing-file calls custom NotFoundHandler', async (t)
196202
})
197203

198204
test('/documentation/:file should be served from custom location', async (t) => {
205+
t.plan(2)
206+
199207
const config = {
200208
mode: 'static',
201209
specification: {
@@ -208,7 +216,6 @@ test('/documentation/:file should be served from custom location', async (t) =>
208216
baseDir: resolve(__dirname, '..', 'static')
209217
}
210218

211-
t.plan(2)
212219
const fastify = Fastify()
213220
await fastify.register(fastifySwagger, config)
214221
await fastify.register(fastifySwaggerUi, uiConfig)
@@ -227,6 +234,8 @@ test('/documentation/:file should be served from custom location', async (t) =>
227234
})
228235

229236
test('/documentation/:file should be served from custom location with trailing slash(es)', async (t) => {
237+
t.plan(2)
238+
230239
const config = {
231240
mode: 'static',
232241
specification: {
@@ -238,7 +247,6 @@ test('/documentation/:file should be served from custom location with trailing s
238247
baseDir: resolve(__dirname, '..', 'static') + '/'
239248
}
240249

241-
t.plan(2)
242250
const fastify = Fastify()
243251
await fastify.register(fastifySwagger, config)
244252
await fastify.register(fastifySwaggerUi, uiConfig)
@@ -256,14 +264,15 @@ test('/documentation/:file should be served from custom location with trailing s
256264
})
257265

258266
test('/documentation/yaml returns cache.swaggerString on second request in static mode', async (t) => {
267+
t.plan(6)
268+
259269
const config = {
260270
mode: 'static',
261271
specification: {
262272
path: './examples/example-static-specification.yaml'
263273
}
264274
}
265275

266-
t.plan(6)
267276
const fastify = Fastify()
268277
await fastify.register(fastifySwagger, config)
269278
await fastify.register(fastifySwaggerUi)
@@ -294,14 +303,15 @@ test('/documentation/yaml returns cache.swaggerString on second request in stati
294303
})
295304

296305
test('/documentation/json returns cache.swaggerObject on second request in static mode', async (t) => {
306+
t.plan(6)
307+
297308
const config = {
298309
mode: 'static',
299310
specification: {
300311
path: './examples/example-static-specification.json'
301312
}
302313
}
303314

304-
t.plan(6)
305315
const fastify = Fastify()
306316
await fastify.register(fastifySwagger, config)
307317
await fastify.register(fastifySwaggerUi)
@@ -330,13 +340,14 @@ test('/documentation/json returns cache.swaggerObject on second request in stati
330340
})
331341

332342
test('/documentation/yaml returns cache.swaggerString on second request in dynamic mode', async (t) => {
343+
t.plan(6)
344+
333345
const config = {
334346
specification: {
335347
path: './examples/example-static-specification.yaml'
336348
}
337349
}
338350

339-
t.plan(6)
340351
const fastify = Fastify()
341352
await fastify.register(fastifySwagger, config)
342353
await fastify.register(fastifySwaggerUi)
@@ -367,13 +378,14 @@ test('/documentation/yaml returns cache.swaggerString on second request in dynam
367378
})
368379

369380
test('/documentation/json returns cache.swaggerObject on second request in dynamic mode', async (t) => {
381+
t.plan(6)
382+
370383
const config = {
371384
specification: {
372385
path: './examples/example-static-specification.json'
373386
}
374387
}
375388

376-
t.plan(6)
377389
const fastify = Fastify()
378390
await fastify.register(fastifySwagger, config)
379391
await fastify.register(fastifySwaggerUi)

0 commit comments

Comments
 (0)