diff --git a/e2e/custom.test.js b/e2e/custom.spec.js similarity index 100% rename from e2e/custom.test.js rename to e2e/custom.spec.js diff --git a/package.json b/package.json index 19e332a..ad5a627 100644 --- a/package.json +++ b/package.json @@ -6,20 +6,17 @@ "type": "commonjs", "types": "types/index.d.ts", "scripts": { - "coverage": "npm run unit -- --coverage-report=lcovonly", "lint": "standard", "lint:fix": "npm run lint -- --fix", "prepare": "node scripts/prepare-swagger-ui", "prepublishOnly": "npm run prepare", - "test": "npm run prepare && npm run coverage && npm run typescript", + "test": "npm run prepare && npm run unit && npm run typescript", "test:dev": "npm run lint && npm run unit && npm run typescript", "test:e2e:command": "node ./examples/example-e2e.js", "test:e2e": "npx playwright test", "test:e2e:ui": "npx playwright test --ui", "typescript": "tsd", - "unit": "tap", - "unit:report": "npm run unit -- --coverage-report=html", - "unit:verbose": "npm run unit -- -Rspec" + "unit": "c8 --100 node --test" }, "repository": { "type": "git", @@ -52,15 +49,14 @@ "@fastify/pre-commit": "^2.1.0", "@fastify/swagger": "^9.0.0-pre.fv5.1", "@playwright/test": "^1.43.1", - "@tapjs/snapshot": "^4.0.0", "@types/node": "^22.0.0", "ajv": "^8.12.0", + "c8": "^10.1.2", "fastify": "5.0.0-alpha.3", "fs-extra": "^11.2.0", "qs": "^6.12.1", "standard": "^17.1.0", "swagger-ui-dist": "5.17.14", - "tap": "^19.2.5", "tsd": "^0.31.0" }, "dependencies": { diff --git a/tap-snapshots/test/static.test.js.test.cjs b/tap-snapshots/test/static.test.js.test.cjs deleted file mode 100644 index fd6a831..0000000 --- a/tap-snapshots/test/static.test.js.test.cjs +++ /dev/null @@ -1,49 +0,0 @@ -/* IMPORTANT - * This snapshot file is auto-generated, but designed for humans. - * It should be checked into source control and tracked carefully. - * Re-generate by setting TAP_SNAPSHOT=1 and running tests. - * Make sure to inspect the output below. Do not ignore changes! - */ -'use strict' -exports['test/static.test.js > TAP > postProcessor works, swagger route returns updated yaml > must match snapshot 1'] = ` -openapi: 3.0.0 -info: - description: Test swagger specification - version: 1.0.0 - title: Test swagger specification - contact: - email: super.developer@gmail.com -servers: - - url: http://localhost:4000/ - description: Localhost (uses test data) -paths: - /status: - get: - description: Status route, so we can check if server is alive - tags: - - Status - responses: - "200": - description: Server is alive - content: - application/json: - schema: - type: object - properties: - health: - type: boolean - date: - type: string - example: - health: true - date: 2018-02-19T15:36:46.758Z - -` - -exports['test/static.test.js > TAP > swagger route returns explicitly passed doc > must match snapshot 1'] = ` -{ - "message": "Route GET:/documentation/json not found", - "error": "Not Found", - "statusCode": 404 -} -` diff --git a/test/csp.test.js b/test/csp.test.js index af3d650..38d0c7d 100644 --- a/test/csp.test.js +++ b/test/csp.test.js @@ -1,6 +1,6 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const Fastify = require('fastify') const fastifyHelmet = require('@fastify/helmet') const fastifySwagger = require('@fastify/swagger') @@ -33,9 +33,9 @@ test('staticCSP = undefined', async (t) => { method: 'GET', url: '/documentation' }) - t.equal(res.statusCode, 200) - t.equal(typeof res.headers['content-security-policy'], 'undefined') - t.equal(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(typeof res.headers['content-security-policy'], 'undefined') + t.assert.deepStrictEqual(typeof res.payload, 'string') }) test('staticCSP = true', async (t) => { @@ -59,9 +59,9 @@ test('staticCSP = true', async (t) => { method: 'GET', url: '/documentation' }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-security-policy'], `default-src 'self'; base-uri 'self'; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data: validator.swagger.io; object-src 'none'; script-src 'self' ${csp.script.join(' ')}; script-src-attr 'none'; style-src 'self' https: ${csp.style.join(' ')}; upgrade-insecure-requests;`) - t.equal(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-security-policy'], `default-src 'self'; base-uri 'self'; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data: validator.swagger.io; object-src 'none'; script-src 'self' ${csp.script.join(' ')}; script-src-attr 'none'; style-src 'self' https: ${csp.style.join(' ')}; upgrade-insecure-requests;`) + t.assert.deepStrictEqual(typeof res.payload, 'string') } { @@ -69,8 +69,8 @@ test('staticCSP = true', async (t) => { method: 'GET', url: '/' }) - t.equal(res.statusCode, 200) - t.equal(typeof res.headers['content-security-policy'], 'undefined') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(typeof res.headers['content-security-policy'], 'undefined') } }) @@ -95,9 +95,9 @@ test('staticCSP = "default-src \'self\';"', async (t) => { method: 'GET', url: '/documentation' }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-security-policy'], "default-src 'self';") - t.equal(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-security-policy'], "default-src 'self';") + t.assert.deepStrictEqual(typeof res.payload, 'string') } { @@ -105,8 +105,8 @@ test('staticCSP = "default-src \'self\';"', async (t) => { method: 'GET', url: '/' }) - t.equal(res.statusCode, 200) - t.equal(typeof res.headers['content-security-policy'], 'undefined') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(typeof res.headers['content-security-policy'], 'undefined') } }) @@ -134,9 +134,9 @@ test('staticCSP = object', async (t) => { method: 'GET', url: '/documentation' }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';") - t.equal(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';") + t.assert.deepStrictEqual(typeof res.payload, 'string') } { @@ -144,8 +144,8 @@ test('staticCSP = object', async (t) => { method: 'GET', url: '/' }) - t.equal(res.statusCode, 200) - t.equal(typeof res.headers['content-security-policy'], 'undefined') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(typeof res.headers['content-security-policy'], 'undefined') } }) @@ -157,7 +157,7 @@ test('transformStaticCSP = function', async (t) => { await fastify.register(fastifySwaggerUi, { staticCSP: "default-src 'self';", transformStaticCSP: function (header) { - t.equal(header, "default-src 'self';") + t.assert.deepStrictEqual(header, "default-src 'self';") return "default-src 'self'; script-src 'self';" } }) @@ -174,9 +174,9 @@ test('transformStaticCSP = function', async (t) => { method: 'GET', url: '/documentation' }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';") - t.equal(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';") + t.assert.deepStrictEqual(typeof res.payload, 'string') } { @@ -184,8 +184,8 @@ test('transformStaticCSP = function', async (t) => { method: 'GET', url: '/' }) - t.equal(res.statusCode, 200) - t.equal(typeof res.headers['content-security-policy'], 'undefined') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(typeof res.headers['content-security-policy'], 'undefined') } }) @@ -197,7 +197,7 @@ test('transformStaticCSP = function, with @fastify/helmet', async (t) => { await fastify.register(fastifySwagger, swaggerOption) await fastify.register(fastifySwaggerUi, { transformStaticCSP: function (header) { - t.equal(header, "default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests") + t.assert.deepStrictEqual(header, "default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests") return "default-src 'self'; script-src 'self';" } }) @@ -214,9 +214,9 @@ test('transformStaticCSP = function, with @fastify/helmet', async (t) => { method: 'GET', url: '/documentation' }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';") - t.equal(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';") + t.assert.deepStrictEqual(typeof res.payload, 'string') } { @@ -224,7 +224,7 @@ test('transformStaticCSP = function, with @fastify/helmet', async (t) => { method: 'GET', url: '/' }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-security-policy'], "default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests") + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-security-policy'], "default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests") } }) diff --git a/test/decorator.test.js b/test/decorator.test.js index 9dc486d..d0eda9b 100644 --- a/test/decorator.test.js +++ b/test/decorator.test.js @@ -1,6 +1,6 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const Fastify = require('fastify') const fastifySwagger = require('@fastify/swagger') const fastifySwaggerUi = require('../index') @@ -12,7 +12,7 @@ test('fastify.swaggerCSP should exist', async (t) => { await fastify.register(fastifySwagger) await fastify.register(fastifySwaggerUi) - t.ok(fastify.swaggerCSP) - t.ok(Array.isArray(fastify.swaggerCSP.script)) - t.ok(Array.isArray(fastify.swaggerCSP.style)) + t.assert.ok(fastify.swaggerCSP) + t.assert.ok(Array.isArray(fastify.swaggerCSP.script)) + t.assert.ok(Array.isArray(fastify.swaggerCSP.style)) }) diff --git a/test/hooks.test.js b/test/hooks.test.js index 536140c..34fc8d7 100644 --- a/test/hooks.test.js +++ b/test/hooks.test.js @@ -1,6 +1,6 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const Fastify = require('fastify') const yaml = require('yaml') @@ -39,36 +39,36 @@ test('hooks on static swagger', async t => { }) let res = await fastify.inject('/documentation') - t.equal(res.statusCode, 401, 'root auth required') + t.assert.deepStrictEqual(res.statusCode, 401, 'root auth required') res = await fastify.inject('/documentation/yaml') - t.equal(res.statusCode, 401, 'auth required yaml') + t.assert.deepStrictEqual(res.statusCode, 401, 'auth required yaml') res = await fastify.inject({ method: 'GET', url: '/documentation/yaml', headers: { authorization: basicAuthEncode('admin', 'admin') } }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-type'], 'application/x-yaml') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml') try { yaml.parse(res.payload) - t.pass('valid swagger yaml') + t.assert.ok(true, 'valid swagger yaml') } catch (err) { - t.fail(err) + t.assert.fail(err) } res = await fastify.inject('/documentation/json') - t.equal(res.statusCode, 401, 'auth required json') + t.assert.deepStrictEqual(res.statusCode, 401, 'auth required json') res = await fastify.inject({ method: 'GET', url: '/documentation/json', headers: { authorization: basicAuthEncode('admin', 'admin') } }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/json; charset=utf-8') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8') try { yaml.parse(res.payload) - t.pass('valid swagger json') + t.assert.ok(true, 'valid swagger json') } catch (err) { t.fail(err) } @@ -91,24 +91,24 @@ test('hooks on dynamic swagger', async t => { fastify.post('/fooBar123', schemaBody, () => {}) let res = await fastify.inject('/documentation') - t.equal(res.statusCode, 401, 'root auth required') + t.assert.deepStrictEqual(res.statusCode, 401, 'root auth required') res = await fastify.inject('/documentation/yaml') - t.equal(res.statusCode, 401, 'auth required yaml') + t.assert.deepStrictEqual(res.statusCode, 401, 'auth required yaml') res = await fastify.inject('/documentation/json') - t.equal(res.statusCode, 401, 'auth required json') + t.assert.deepStrictEqual(res.statusCode, 401, 'auth required json') res = await fastify.inject({ method: 'GET', url: '/documentation/json', headers: { authorization: basicAuthEncode('admin', 'admin') } }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/json; charset=utf-8') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8') const swaggerObject = res.json() - t.ok(swaggerObject.paths) - t.ok(swaggerObject.paths['/fooBar123']) + t.assert.ok(swaggerObject.paths) + t.assert.ok(swaggerObject.paths['/fooBar123']) }) test('catch all added schema', async t => { @@ -136,5 +136,5 @@ test('catch all added schema', async t => { await fastify.ready() const openapi = fastify.swagger() - t.same(Object.keys(openapi.components.schemas), ['Root', 'Instance', 'Sub-Instance']) + t.assert.deepStrictEqual(Object.keys(openapi.components.schemas), ['Root', 'Instance', 'Sub-Instance']) }) diff --git a/test/integration.test.js b/test/integration.test.js index 18d47fd..8f89792 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -1,6 +1,6 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const Fastify = require('fastify') const fastifySwagger = require('@fastify/swagger') const fastifyHelmet = require('@fastify/helmet') @@ -43,5 +43,5 @@ test('fastify will response swagger csp', async (t) => { method: 'GET', url: '/' }) - t.same(res.headers['content-security-policy'], csp) + t.assert.deepStrictEqual(res.headers['content-security-policy'], csp) }) diff --git a/test/prepare.test.js b/test/prepare.test.js index 54c7fed..7af5eea 100644 --- a/test/prepare.test.js +++ b/test/prepare.test.js @@ -1,6 +1,6 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const Fastify = require('fastify') const fastifySwagger = require('@fastify/swagger') const fastifySwaggerUi = require('../index') @@ -17,8 +17,8 @@ test('Swagger source does not contain sourceMaps', async (t) => { }) const includesSourceMap = res.payload.includes('sourceMappingURL') - t.equal(includesSourceMap, false) - t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8') + t.assert.deepStrictEqual(includesSourceMap, false) + t.assert.deepStrictEqual(res.headers['content-type'], 'application/javascript; charset=UTF-8') }) test('Swagger css does not contain sourceMaps', async (t) => { @@ -33,6 +33,6 @@ test('Swagger css does not contain sourceMaps', async (t) => { }) const includesSourceMap = res.payload.includes('sourceMappingURL') - t.equal(includesSourceMap, false) - t.equal(res.headers['content-type'], 'text/css; charset=UTF-8') + t.assert.deepStrictEqual(includesSourceMap, false) + t.assert.deepStrictEqual(res.headers['content-type'], 'text/css; charset=UTF-8') }) diff --git a/test/route.test.js b/test/route.test.js index 86d9b31..b9be134 100644 --- a/test/route.test.js +++ b/test/route.test.js @@ -1,6 +1,6 @@ 'use strict' -const t = require('tap') +const t = require('node:test') const test = t.test const Fastify = require('fastify') const Swagger = require('@apidevtools/swagger-parser') @@ -71,7 +71,7 @@ test('/documentation/json route', async (t) => { const payload = JSON.parse(res.payload) await Swagger.validate(payload) - t.pass('valid swagger object') + t.assert.ok(true, 'valid swagger object') }) test('fastify.swagger should return a valid swagger yaml', async (t) => { @@ -94,10 +94,10 @@ test('fastify.swagger should return a valid swagger yaml', async (t) => { url: '/documentation/yaml' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/x-yaml') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml') yaml.parse(res.payload) - t.pass('valid swagger yaml') + t.assert.ok(true, 'valid swagger yaml') }) test('/documentation should display index html', async (t) => { @@ -117,10 +117,10 @@ test('/documentation should display index html', async (t) => { method: 'GET', url: '/documentation' }) - t.equal(res.statusCode, 200) - t.equal(res.headers.location, undefined) - t.equal(typeof res.payload, 'string') - t.equal('text/html; charset=utf-8', res.headers['content-type']) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers.location, undefined) + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type']) }) test('/documentation/ should display index html ', async (t) => { @@ -140,10 +140,10 @@ test('/documentation/ should display index html ', async (t) => { method: 'GET', url: '/documentation/' }) - t.equal(res.statusCode, 200) - t.equal(res.headers.location, undefined) - t.equal(typeof res.payload, 'string') - t.equal('text/html; charset=utf-8', res.headers['content-type']) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers.location, undefined) + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type']) }) test('/v1/documentation should display index html', async (t) => { @@ -163,10 +163,10 @@ test('/v1/documentation should display index html', async (t) => { method: 'GET', url: '/v1/documentation' }) - t.equal(res.statusCode, 200) - t.equal(res.headers.location, undefined) - t.equal(typeof res.payload, 'string') - t.equal('text/html; charset=utf-8', res.headers['content-type']) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers.location, undefined) + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type']) }) test('/v1/documentation/ should display index html', async (t) => { @@ -186,10 +186,10 @@ test('/v1/documentation/ should display index html', async (t) => { method: 'GET', url: '/v1/documentation/' }) - t.equal(res.statusCode, 200) - t.equal(res.headers.location, undefined) - t.equal(typeof res.payload, 'string') - t.equal('text/html; charset=utf-8', res.headers['content-type']) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers.location, undefined) + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type']) }) test('/v1/foobar should display index html', async (t) => { @@ -212,10 +212,10 @@ test('/v1/foobar should display index html', async (t) => { method: 'GET', url: '/v1/foobar' }) - t.equal(res.statusCode, 200) - t.equal(res.headers.location, undefined) - t.equal(typeof res.payload, 'string') - t.equal('text/html; charset=utf-8', res.headers['content-type']) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers.location, undefined) + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type']) }) test('/v1/foobar/ should display index html', async (t) => { @@ -238,10 +238,10 @@ test('/v1/foobar/ should display index html', async (t) => { method: 'GET', url: '/v1/foobar/' }) - t.equal(res.statusCode, 200) - t.equal(res.headers.location, undefined) - t.equal(typeof res.payload, 'string') - t.equal('text/html; charset=utf-8', res.headers['content-type']) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers.location, undefined) + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type']) }) test('with routePrefix: \'/\' should display index html', async (t) => { @@ -257,10 +257,10 @@ test('with routePrefix: \'/\' should display index html', async (t) => { method: 'GET', url: '/' }) - t.equal(res.statusCode, 200) - t.equal(res.headers.location, undefined) - t.equal(typeof res.payload, 'string') - t.equal('text/html; charset=utf-8', res.headers['content-type']) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers.location, undefined) + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type']) }) test('/documentation/static/:file should send back the correct file', async (t) => { @@ -284,8 +284,8 @@ test('/documentation/static/:file should send back the correct file', async (t) method: 'GET', url: '/documentation/static/index.html' }) - t.equal(res.statusCode, 302) - t.equal(res.headers.location, '/documentation/') + t.assert.deepStrictEqual(res.statusCode, 302) + t.assert.deepStrictEqual(res.headers.location, '/documentation/') } { @@ -293,16 +293,16 @@ test('/documentation/static/:file should send back the correct file', async (t) method: 'GET', url: '/documentation/static/' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'text/html; charset=UTF-8') - t.equal( + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=UTF-8') + t.assert.deepStrictEqual( readFileSync( resolve(__dirname, '..', 'static', 'index.html'), 'utf8' ), res.payload ) - t.ok(res.payload.indexOf('swagger-initializer.js') !== -1) + t.assert.ok(res.payload.indexOf('swagger-initializer.js') !== -1) } { @@ -310,9 +310,9 @@ test('/documentation/static/:file should send back the correct file', async (t) method: 'GET', url: '/documentation/static/swagger-initializer.js' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/javascript; charset=utf-8') - t.ok(res.payload.indexOf('resolveUrl') !== -1) + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/javascript; charset=utf-8') + t.assert.ok(res.payload.indexOf('resolveUrl') !== -1) } { @@ -320,9 +320,9 @@ test('/documentation/static/:file should send back the correct file', async (t) method: 'GET', url: '/documentation/static/oauth2-redirect.html' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'text/html; charset=UTF-8') - t.equal( + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=UTF-8') + t.assert.deepStrictEqual( readFileSync( resolve(__dirname, '..', 'static', 'oauth2-redirect.html'), 'utf8' @@ -336,9 +336,9 @@ test('/documentation/static/:file should send back the correct file', async (t) method: 'GET', url: '/documentation/static/swagger-ui.css' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'text/css; charset=UTF-8') - t.equal( + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'text/css; charset=UTF-8') + t.assert.deepStrictEqual( readFileSync( resolve(__dirname, '..', 'static', 'swagger-ui.css'), 'utf8' @@ -352,9 +352,9 @@ test('/documentation/static/:file should send back the correct file', async (t) method: 'GET', url: '/documentation/static/swagger-ui-bundle.js' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8') - t.equal( + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/javascript; charset=UTF-8') + t.assert.deepStrictEqual( readFileSync( resolve(__dirname, '..', 'static', 'swagger-ui-bundle.js'), 'utf8' @@ -368,9 +368,9 @@ test('/documentation/static/:file should send back the correct file', async (t) method: 'GET', url: '/documentation/static/swagger-ui-standalone-preset.js' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8') - t.equal( + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/javascript; charset=UTF-8') + t.assert.deepStrictEqual( readFileSync( resolve(__dirname, '..', 'static', 'swagger-ui-standalone-preset.js'), 'utf8' @@ -396,8 +396,8 @@ test('/documentation/static/:file should send back file from baseDir', async (t) method: 'GET', url: '/documentation/static/example-logo.svg' }) - t.equal(res.statusCode, 200) - t.equal( + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual( res.payload, readFileSync( resolve(__dirname, '..', 'examples', 'static', 'example-logo.svg'), @@ -426,9 +426,10 @@ test('/documentation/static/:file 404', async (t) => { url: '/documentation/static/stuff.css' }) const payload = JSON.parse(res.payload) - t.equal(res.statusCode, 404) - t.match(payload, { + t.assert.deepStrictEqual(res.statusCode, 404) + t.assert.deepStrictEqual(payload, { error: 'Not Found', + message: 'Route GET:/documentation/static/stuff.css not found', statusCode: 404 }) }) @@ -455,7 +456,7 @@ test('/documentation2/json route (overwrite)', async (t) => { const payload = JSON.parse(res.payload) await Swagger.validate(payload) - t.pass('valid swagger object') + t.assert.ok(true, 'valid swagger object') }) test('/documentation/:myfile should return 404 in dynamic mode', async (t) => { @@ -468,7 +469,7 @@ test('/documentation/:myfile should return 404 in dynamic mode', async (t) => { method: 'GET', url: '/documentation/swagger-ui.js' }) - t.equal(res.statusCode, 404) + t.assert.deepStrictEqual(res.statusCode, 404) }) test('/documentation/:myfile should run custom NotFoundHandler in dynamic mode', async (t) => { @@ -485,7 +486,7 @@ test('/documentation/:myfile should run custom NotFoundHandler in dynamic mode', method: 'GET', url: '/documentation/swagger-ui.js' }) - t.equal(res.statusCode, 410) + t.assert.deepStrictEqual(res.statusCode, 410) }) test('/documentation/* should not return module files when baseDir not set', async (t) => { @@ -498,14 +499,14 @@ test('/documentation/* should not return module files when baseDir not set', asy method: 'GET', url: '/documentation/README.md' }) - t.equal(res.statusCode, 404) + t.assert.deepStrictEqual(res.statusCode, 404) }) test('should return silent log level of route /documentation', async (t) => { const fastify = Fastify() fastify.addHook('onRoute', function (route) { - t.equal(route.logLevel, 'silent') + t.assert.deepStrictEqual(route.logLevel, 'silent') }) await fastify.register(fastifySwagger, swaggerOption) @@ -515,15 +516,15 @@ test('should return silent log level of route /documentation', async (t) => { method: 'GET', url: '/documentation/' }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-type'], 'text/html; charset=utf-8') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8') }) test('should return empty log level of route /documentation', async (t) => { const fastify = Fastify() fastify.addHook('onRoute', function (route) { - t.equal(route.logLevel, '') + t.assert.deepStrictEqual(route.logLevel, '') }) await fastify.register(fastifySwagger, swaggerOption) @@ -533,8 +534,8 @@ test('should return empty log level of route /documentation', async (t) => { method: 'GET', url: '/documentation/' }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-type'], 'text/html; charset=utf-8') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8') }) test('/documentation should display index html with correct asset urls', async (t) => { @@ -548,10 +549,10 @@ test('/documentation should display index html with correct asset urls', async ( url: '/documentation' }) - t.equal(res.payload.includes('href="/documentation/static/index.css"'), true) - t.equal(res.payload.includes('src="/documentation/static/theme/theme-js.js"'), true) - t.equal(res.payload.includes('href="/documentation/index.css"'), false) - t.equal(res.payload.includes('src="/documentation/theme/theme-js.js"'), false) + t.assert.deepStrictEqual(res.payload.includes('href="/documentation/static/index.css"'), true) + t.assert.deepStrictEqual(res.payload.includes('src="/documentation/static/theme/theme-js.js"'), true) + t.assert.deepStrictEqual(res.payload.includes('href="/documentation/index.css"'), false) + t.assert.deepStrictEqual(res.payload.includes('src="/documentation/theme/theme-js.js"'), false) }) test('/documentation/ should display index html with correct asset urls', async (t) => { @@ -565,10 +566,10 @@ test('/documentation/ should display index html with correct asset urls', async url: '/documentation/' }) - t.equal(res.payload.includes('href="./static/index.css"'), true) - t.equal(res.payload.includes('src="./static/theme/theme-js.js"'), true) - t.equal(res.payload.includes('href="./index.css"'), false) - t.equal(res.payload.includes('src="./theme/theme-js.js"'), false) + t.assert.strictEqual(res.payload.includes('href="./static/index.css"'), true) + t.assert.strictEqual(res.payload.includes('src="./static/theme/theme-js.js"'), true) + t.assert.strictEqual(res.payload.includes('href="./index.css"'), false) + t.assert.strictEqual(res.payload.includes('src="./theme/theme-js.js"'), false) }) test('/docs should display index html with correct asset urls when documentation prefix is set', async (t) => { @@ -582,10 +583,10 @@ test('/docs should display index html with correct asset urls when documentation url: '/docs' }) - t.equal(res.payload.includes('href="/docs/static/index.css"'), true) - t.equal(res.payload.includes('src="/docs/static/theme/theme-js.js"'), true) - t.equal(res.payload.includes('href="/docs/index.css"'), false) - t.equal(res.payload.includes('src="/docs/theme/theme-js.js"'), false) + t.assert.strictEqual(res.payload.includes('href="/docs/static/index.css"'), true) + t.assert.strictEqual(res.payload.includes('src="/docs/static/theme/theme-js.js"'), true) + t.assert.strictEqual(res.payload.includes('href="/docs/index.css"'), false) + t.assert.strictEqual(res.payload.includes('src="/docs/theme/theme-js.js"'), false) }) test('/docs/ should display index html with correct asset urls when documentation prefix is set', async (t) => { @@ -599,8 +600,59 @@ test('/docs/ should display index html with correct asset urls when documentatio url: '/docs/' }) - t.equal(res.payload.includes('href="./static/index.css"'), true) - t.equal(res.payload.includes('src="./static/theme/theme-js.js"'), true) - t.equal(res.payload.includes('href="./index.css"'), false) - t.equal(res.payload.includes('src="./theme/theme-js.js"'), false) + t.assert.strictEqual(res.payload.includes('href="./static/index.css"'), true) + t.assert.strictEqual(res.payload.includes('src="./static/theme/theme-js.js"'), true) + t.assert.strictEqual(res.payload.includes('href="./index.css"'), false) + t.assert.strictEqual(res.payload.includes('src="./theme/theme-js.js"'), false) +}) + +test('/documentation/ should display index html with correct asset urls', async (t) => { + t.plan(4) + const fastify = Fastify() + await fastify.register(fastifySwagger, swaggerOption) + await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] } }) + + const res = await fastify.inject({ + method: 'GET', + url: '/documentation/' + }) + + t.assert.strictEqual(res.payload.includes('href="./static/index.css"'), true) + t.assert.strictEqual(res.payload.includes('src="./static/theme/theme-js.js"'), true) + t.assert.strictEqual(res.payload.includes('href="./index.css"'), false) + t.assert.strictEqual(res.payload.includes('src="./theme/theme-js.js"'), false) +}) + +test('/docs should display index html with correct asset urls when documentation prefix is set', async (t) => { + t.plan(4) + const fastify = Fastify() + await fastify.register(fastifySwagger, swaggerOption) + await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] }, routePrefix: '/docs' }) + + const res = await fastify.inject({ + method: 'GET', + url: '/docs' + }) + + t.assert.strictEqual(res.payload.includes('href="/docs/static/index.css"'), true) + t.assert.strictEqual(res.payload.includes('src="/docs/static/theme/theme-js.js"'), true) + t.assert.strictEqual(res.payload.includes('href="/docs/index.css"'), false) + t.assert.strictEqual(res.payload.includes('src="/docs/theme/theme-js.js"'), false) +}) + +test('/docs/ should display index html with correct asset urls when documentation prefix is set', async (t) => { + t.plan(4) + const fastify = Fastify() + await fastify.register(fastifySwagger, swaggerOption) + await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] }, routePrefix: '/docs' }) + + const res = await fastify.inject({ + method: 'GET', + url: '/docs/' + }) + + t.assert.strictEqual(res.payload.includes('href="./static/index.css"'), true) + t.assert.strictEqual(res.payload.includes('src="./static/theme/theme-js.js"'), true) + t.assert.strictEqual(res.payload.includes('href="./index.css"'), false) + t.assert.strictEqual(res.payload.includes('src="./theme/theme-js.js"'), false) }) diff --git a/test/serialize.test.js b/test/serialize.test.js index 65c431e..6d14e2c 100644 --- a/test/serialize.test.js +++ b/test/serialize.test.js @@ -1,128 +1,119 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const serialize = require('../lib/serialize') -test('serialize', async (t) => { - t.plan(8) +test('serialize boolean', t => { + t.plan(2) - t.test('boolean', t => { - t.plan(2) - - t.equal(serialize(true), 'true') - t.equal(serialize(false), 'false') - }) - - t.test('number', t => { - t.plan(7) - - t.equal(serialize(0), '0') - t.equal(serialize(1), '1') - t.equal(serialize(1.0), '1') - t.equal(serialize(1.01), '1.01') - t.equal(serialize(Infinity), 'Infinity') - t.equal(serialize(-Infinity), '-Infinity') - t.equal(serialize(NaN), 'NaN') - }) + t.assert.deepStrictEqual(serialize(true), 'true') + t.assert.deepStrictEqual(serialize(false), 'false') +}) - t.test('string', t => { - t.plan(3) +test('serialize number', t => { + t.plan(7) - t.equal(serialize('0'), '"0"') - t.equal(serialize('abc'), '"abc"') - t.equal(serialize('"a'), '"\\\"a"') // eslint-disable-line no-useless-escape - }) + t.assert.deepStrictEqual(serialize(0), '0') + t.assert.deepStrictEqual(serialize(1), '1') + t.assert.deepStrictEqual(serialize(1.0), '1') + t.assert.deepStrictEqual(serialize(1.01), '1.01') + t.assert.deepStrictEqual(serialize(Infinity), 'Infinity') + t.assert.deepStrictEqual(serialize(-Infinity), '-Infinity') + t.assert.deepStrictEqual(serialize(NaN), 'NaN') +}) - t.test('bigint', t => { - t.plan(3) +test('serialize string', t => { + t.plan(3) - t.equal(serialize(0n), '0n') - t.equal(serialize(1000000000n), '1000000000n') - t.equal(serialize(-9999n), '-9999n') - }) + t.assert.deepStrictEqual(serialize('0'), '"0"') + t.assert.deepStrictEqual(serialize('abc'), '"abc"') + t.assert.deepStrictEqual(serialize('"a'), '"\\\"a"') // eslint-disable-line no-useless-escape +}) - t.test('function', t => { - t.plan(7) +test('serialize bigint', t => { + t.plan(3) - t.equal(serialize(function a () {}), 'function a () {}') - t.equal(serialize(async function a () {}), 'async function a () {}') - t.equal(serialize(() => {}), '() => {}') - t.equal(serialize(async () => {}), 'async () => {}') - t.equal(serialize(() => Date.now), '() => Date.now') + t.assert.deepStrictEqual(serialize(0n), '0n') + t.assert.deepStrictEqual(serialize(1000000000n), '1000000000n') + t.assert.deepStrictEqual(serialize(-9999n), '-9999n') +}) - t.equal(serialize(function () {}), 'function () {}') - t.equal(serialize(async function () {}), 'async function () {}') - }) +test('serialize function', t => { + t.plan(7) - t.test('undefined', t => { - t.plan(1) + t.assert.deepStrictEqual(serialize(function a () { }), 'function a () { }') + t.assert.deepStrictEqual(serialize(async function a () { }), 'async function a () { }') + t.assert.deepStrictEqual(serialize(() => { }), '() => { }') + t.assert.deepStrictEqual(serialize(async () => { }), 'async () => { }') + t.assert.deepStrictEqual(serialize(() => Date.now), '() => Date.now') + t.assert.deepStrictEqual(serialize(function () { }), 'function () { }') + t.assert.deepStrictEqual(serialize(async function () { }), 'async function () { }') +}) - t.equal(serialize(undefined), 'undefined') - }) +test('serialize undefined', t => { + t.plan(1) - t.test('symbol', t => { - t.plan(2) + t.assert.deepStrictEqual(serialize(undefined), 'undefined') +}) - t.equal(serialize(Symbol('a')), 'Symbol("a")') - t.equal(serialize(Symbol()), 'Symbol()') // eslint-disable-line symbol-description - }) +test('serialize symbol', t => { + t.plan(2) - t.test('object', t => { - t.plan(7) + t.assert.deepStrictEqual(serialize(Symbol('a')), 'Symbol("a")') + t.assert.deepStrictEqual(serialize(Symbol()), 'Symbol()') // eslint-disable-line symbol-description +}) - t.test('null', t => { - t.plan(1) +test('serialize null', t => { + t.plan(1) - t.equal(serialize(null), 'null') - }) + t.assert.deepStrictEqual(serialize(null), 'null') +}) - t.test('RegExp', t => { - t.plan(1) +test('serialize RegExp', t => { + t.plan(1) - t.equal(serialize(/0-9/gi), '/0-9/gi') - }) + t.assert.deepStrictEqual(serialize(/0-9/gi), '/0-9/gi') +}) - t.test('Date', t => { - t.plan(1) +test('serialize Date', t => { + t.plan(1) - t.equal(serialize(new Date(0)), 'new Date(0)') - }) + t.assert.deepStrictEqual(serialize(new Date(0)), 'new Date(0)') +}) - t.test('Array', t => { - t.plan(5) +test('serialize Array', t => { + t.plan(5) - t.equal(serialize([]), '[]') - t.equal(serialize(['a']), '["a"]') - t.equal(serialize([1, 1n, 'a', true]), '[1,1n,"a",true]') - t.equal(serialize([{}]), '[{}]') - t.equal(serialize([{ a: [{}] }]), '[{"a":[{}]}]') - }) + t.assert.deepStrictEqual(serialize([]), '[]') + t.assert.deepStrictEqual(serialize(['a']), '["a"]') + t.assert.deepStrictEqual(serialize([1, 1n, 'a', true]), '[1,1n,"a",true]') + t.assert.deepStrictEqual(serialize([{}]), '[{}]') + t.assert.deepStrictEqual(serialize([{ a: [{}] }]), '[{"a":[{}]}]') +}) - t.test('POJO', t => { - t.plan(3) +test('serialize POJO', t => { + t.plan(3) - t.equal(serialize({}), '{}') - t.equal(serialize({ key: 'value' }), '{"key":"value"}') - t.equal(serialize({ null: null, undefined }), '{"null":null,"undefined":undefined}') - }) + t.assert.deepStrictEqual(serialize({}), '{}') + t.assert.deepStrictEqual(serialize({ key: 'value' }), '{"key":"value"}') + t.assert.deepStrictEqual(serialize({ null: null, undefined }), '{"null":null,"undefined":undefined}') +}) - t.test('Set', t => { - t.plan(3) +test('serialize Set', t => { + t.plan(3) - t.equal(serialize(new Set()), 'new Set([])') - t.equal(serialize(new Set(['a'])), 'new Set(["a"])') - t.equal(serialize(new Set(['a', {}])), 'new Set(["a",{}])') - }) + t.assert.deepStrictEqual(serialize(new Set()), 'new Set([])') + t.assert.deepStrictEqual(serialize(new Set(['a'])), 'new Set(["a"])') + t.assert.deepStrictEqual(serialize(new Set(['a', {}])), 'new Set(["a",{}])') +}) - t.test('Map', t => { - t.plan(3) +test('serialize Map', t => { + t.plan(3) - t.equal(serialize(new Map()), 'new Map([])') - t.equal(serialize(new Map([['a', 1]])), 'new Map([["a",1]])') - const map = new Map() - map.set('b', 1) + t.assert.deepStrictEqual(serialize(new Map()), 'new Map([])') + t.assert.deepStrictEqual(serialize(new Map([['a', 1]])), 'new Map([["a",1]])') + const map = new Map() + map.set('b', 1) - t.equal(serialize(map), 'new Map([["b",1]])') - }) - }) + t.assert.deepStrictEqual(serialize(map), 'new Map([["b",1]])') }) diff --git a/test/static.test.js b/test/static.test.js index 074d4b7..5ca467f 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -2,7 +2,7 @@ const fs = require('node:fs') const resolve = require('node:path').resolve -const { test } = require('tap') +const { test } = require('node:test') const yaml = require('yaml') const Fastify = require('fastify') const fastifySwagger = require('@fastify/swagger') @@ -34,10 +34,10 @@ test('swagger route returns yaml', async (t) => { url: '/documentation/yaml' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/x-yaml') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml') yaml.parse(res.payload) - t.pass('valid swagger yaml') + t.assert.ok(true, 'valid swagger yaml') }) test('swagger route returns json', async (t) => { @@ -60,10 +60,10 @@ test('swagger route returns json', async (t) => { url: '/documentation/json' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/json; charset=utf-8') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8') yaml.parse(res.payload) - t.pass('valid swagger json') + t.assert.ok(true, 'valid swagger json') }) test('postProcessor works, swagger route returns updated yaml', async (t) => { @@ -90,11 +90,42 @@ test('postProcessor works, swagger route returns updated yaml', async (t) => { url: '/documentation/yaml' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/x-yaml') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml') yaml.parse(res.payload) - t.matchSnapshot(res.payload) - t.pass('valid swagger yaml') + t.assert.strictEqual(res.payload, `openapi: 3.0.0 +info: + description: Test swagger specification + version: 1.0.0 + title: Test swagger specification + contact: + email: super.developer@gmail.com +servers: + - url: http://localhost:4000/ + description: Localhost (uses test data) +paths: + /status: + get: + description: Status route, so we can check if server is alive + tags: + - Status + responses: + "200": + description: Server is alive + content: + application/json: + schema: + type: object + properties: + health: + type: boolean + date: + type: string + example: + health: true + date: 2018-02-19T15:36:46.758Z +`) + t.assert.ok(true, 'valid swagger yaml') }) test('swagger route returns explicitly passed doc', async (t) => { @@ -125,8 +156,12 @@ test('swagger route returns explicitly passed doc', async (t) => { }) const payload = JSON.parse(res.payload) - t.matchSnapshot(JSON.stringify(payload, null, 2)) - t.pass('valid explicitly passed spec document') + t.assert.deepStrictEqual(payload, { + message: 'Route GET:/documentation/json not found', + error: 'Not Found', + statusCode: 404 + }) + t.assert.ok(true, 'valid explicitly passed spec document') }) test('/documentation/:file should serve static file from the location of main specification file', async (t) => { @@ -153,7 +188,7 @@ test('/documentation/:file should serve static file from the location of main sp url: '/documentation/non-existing-file' }) - t.equal(res.statusCode, 404) + t.assert.deepStrictEqual(res.statusCode, 404) } { @@ -162,8 +197,8 @@ test('/documentation/:file should serve static file from the location of main sp url: '/documentation/example-static-specification.yaml' }) - t.equal(res.statusCode, 200) - t.equal(exampleStaticSpecificationYaml, res.payload) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(exampleStaticSpecificationYaml, res.payload) } { @@ -172,7 +207,7 @@ test('/documentation/:file should serve static file from the location of main sp url: '/documentation/dynamic-swagger.js' }) - t.equal(res.statusCode, 200) + t.assert.deepStrictEqual(res.statusCode, 200) } }) @@ -198,7 +233,7 @@ test('/documentation/non-existing-file calls custom NotFoundHandler', async (t) url: '/documentation/some-file-that-does-not-exist.js' }) - t.equal(res.statusCode, 410) + t.assert.deepStrictEqual(res.statusCode, 410) }) test('/documentation/:file should be served from custom location', async (t) => { @@ -225,8 +260,8 @@ test('/documentation/:file should be served from custom location', async (t) => url: '/documentation/oauth2-redirect.html' }) - t.equal(res.statusCode, 200) - t.equal(oauthRedirectHtml, res.payload) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(oauthRedirectHtml, res.payload) }) test('/documentation/:file should be served from custom location with trailing slash(es)', async (t) => { @@ -252,8 +287,8 @@ test('/documentation/:file should be served from custom location with trailing s url: '/documentation/oauth2-redirect.html' }) - t.equal(res.statusCode, 200) - t.equal(oauthRedirectHtml, res.payload) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(oauthRedirectHtml, res.payload) }) test('/documentation/yaml returns cache.swaggerString on second request in static mode', async (t) => { @@ -276,10 +311,10 @@ test('/documentation/yaml returns cache.swaggerString on second request in stati url: '/documentation/yaml' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/x-yaml') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml') yaml.parse(res.payload) - t.pass('valid swagger yaml') + t.assert.ok(true, 'valid swagger yaml') } { @@ -288,10 +323,10 @@ test('/documentation/yaml returns cache.swaggerString on second request in stati url: '/documentation/yaml' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/x-yaml') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml') yaml.parse(res.payload) - t.pass('valid swagger yaml') + t.assert.ok(true, 'valid swagger yaml') } }) @@ -315,9 +350,9 @@ test('/documentation/json returns cache.swaggerObject on second request in stati url: '/documentation/json' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/json; charset=utf-8') - t.pass('valid swagger json') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8') + t.assert.ok(true, 'valid swagger json') } { @@ -326,9 +361,9 @@ test('/documentation/json returns cache.swaggerObject on second request in stati url: '/documentation/json' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/json; charset=utf-8') - t.pass('valid swagger json') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8') + t.assert.ok(true, 'valid swagger json') } }) @@ -351,10 +386,10 @@ test('/documentation/yaml returns cache.swaggerString on second request in dynam url: '/documentation/yaml' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/x-yaml') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml') yaml.parse(res.payload) - t.pass('valid swagger yaml') + t.assert.ok(true, 'valid swagger yaml') } { @@ -363,10 +398,10 @@ test('/documentation/yaml returns cache.swaggerString on second request in dynam url: '/documentation/yaml' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/x-yaml') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml') yaml.parse(res.payload) - t.pass('valid swagger yaml') + t.assert.ok(true, 'valid swagger yaml') } }) @@ -389,9 +424,9 @@ test('/documentation/json returns cache.swaggerObject on second request in dynam url: '/documentation/json' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/json; charset=utf-8') - t.pass('valid swagger json') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8') + t.assert.ok(true, 'valid swagger json') } { @@ -400,8 +435,8 @@ test('/documentation/json returns cache.swaggerObject on second request in dynam url: '/documentation/json' }) - t.equal(typeof res.payload, 'string') - t.equal(res.headers['content-type'], 'application/json; charset=utf-8') - t.pass('valid swagger json') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8') + t.assert.ok(true, 'valid swagger json') } }) diff --git a/test/swagger-initializer.test.js b/test/swagger-initializer.test.js index 2e68e39..62354bd 100644 --- a/test/swagger-initializer.test.js +++ b/test/swagger-initializer.test.js @@ -1,6 +1,6 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const Fastify = require('fastify') const fastifySwagger = require('@fastify/swagger') const fastifySwaggerUi = require('../index') @@ -17,8 +17,8 @@ test('/documentation/static/swagger-initializer.js should have default uiConfig' url: '/documentation/static/swagger-initializer.js' }) - t.equal(res.statusCode, 200) - t.ok(res.payload.includes('const config = {}')) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.ok(res.payload.includes('const config = {}')) }) test('/documentation/static/swagger-initializer.js should have configurable uiConfig', async (t) => { @@ -37,8 +37,8 @@ test('/documentation/static/swagger-initializer.js should have configurable uiCo url: '/documentation/static/swagger-initializer.js' }) - t.equal(res.statusCode, 200) - t.ok(res.payload.includes("const config = {\"onComplete\":() => { alert('test') }}")) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.ok(res.payload.includes("const config = {\"onComplete\":() => { alert('test') }}")) }) test('/documentation/static/swagger-initializer.js should have default initOAuth', async (t) => { @@ -53,8 +53,8 @@ test('/documentation/static/swagger-initializer.js should have default initOAuth url: '/documentation/static/swagger-initializer.js' }) - t.equal(res.statusCode, 200) - t.ok(res.payload.includes('ui.initOAuth({})')) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.ok(res.payload.includes('ui.initOAuth({})')) }) test('/documentation/static/swagger-initializer.js should have configurable initOAuth', async (t) => { @@ -73,8 +73,8 @@ test('/documentation/static/swagger-initializer.js should have configurable init url: '/documentation/static/swagger-initializer.js' }) - t.equal(res.statusCode, 200) - t.ok(res.payload.includes('ui.initOAuth({"clientId":"someId"})')) + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.ok(res.payload.includes('ui.initOAuth({"clientId":"someId"})')) }) test('customize logo', async (t) => { @@ -90,5 +90,5 @@ test('customize logo', async (t) => { await fastify.register(fastifySwaggerUi, { logo: { type: 'image/png', content: 'foobar' } }) const res = await fastify.inject('/documentation/static/swagger-initializer.js') - t.equal(res.body.indexOf(Buffer.from('foobar').toString('base64')) > -1, true) + t.assert.deepStrictEqual(res.body.indexOf(Buffer.from('foobar').toString('base64')) > -1, true) }) diff --git a/test/theme.test.js b/test/theme.test.js index 755e2c7..a13647d 100644 --- a/test/theme.test.js +++ b/test/theme.test.js @@ -1,6 +1,6 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const Fastify = require('fastify') const fastifySwagger = require('@fastify/swagger') const fastifySwaggerUi = require('../index') @@ -23,11 +23,11 @@ test('swagger route does not return additional theme', async (t) => { url: '/documentation' }) - t.equal(typeof res.payload, 'string') - t.notMatch(res.payload, /theme\/special\.js/) - t.notMatch(res.payload, /theme\/favicon\.png/) - t.notMatch(res.payload, /theme\/theme\.css/) - t.equal(res.headers['content-type'], 'text/html; charset=utf-8') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.deepStrictEqual(/theme\/special\.js/.test(res.payload), false) + t.assert.deepStrictEqual(/theme\/favicon\.png/.test(res.payload), false) + t.assert.deepStrictEqual(/theme\/theme\.css/.test(res.payload), false) + t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8') }) test('swagger route returns additional theme', async (t) => { @@ -66,18 +66,18 @@ test('swagger route returns additional theme', async (t) => { url: '/documentation' }) - t.equal(typeof res.payload, 'string') - t.match(res.payload, /theme\/special\.js/) - t.match(res.payload, /theme\/favicon\.png/) - t.match(res.payload, /theme\/theme\.css/) - t.equal(res.headers['content-type'], 'text/html; charset=utf-8') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.match(res.payload, /theme\/special\.js/) + t.assert.match(res.payload, /theme\/favicon\.png/) + t.assert.match(res.payload, /theme\/theme\.css/) + t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8') { const res = await fastify.inject({ method: 'GET', url: '/documentation/static/theme/special.js' }) - t.equal(res.payload, 'alert("loaded test-theme")') + t.assert.deepStrictEqual(res.payload, 'alert("loaded test-theme")') } { @@ -85,8 +85,8 @@ test('swagger route returns additional theme', async (t) => { method: 'GET', url: '/documentation/static/theme/favicon.png' }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-type'], 'image/png') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-type'], 'image/png') } { @@ -94,7 +94,7 @@ test('swagger route returns additional theme', async (t) => { method: 'GET', url: '/documentation/static/theme/theme.css' }) - t.equal(res.payload, '* {border: 1px red solid;}') + t.assert.deepStrictEqual(res.payload, '* {border: 1px red solid;}') } }) @@ -122,16 +122,16 @@ test('swagger route returns additional theme - only js', async (t) => { url: '/documentation' }) - t.equal(typeof res.payload, 'string') - t.match(res.payload, /theme\/special\.js/) - t.equal(res.headers['content-type'], 'text/html; charset=utf-8') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.match(res.payload, /theme\/special\.js/) + t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8') { const res = await fastify.inject({ method: 'GET', url: '/documentation/static/theme/special.js' }) - t.equal(res.payload, 'alert("loaded test-theme")') + t.assert.deepStrictEqual(res.payload, 'alert("loaded test-theme")') } }) @@ -159,16 +159,16 @@ test('swagger route returns additional theme - only css', async (t) => { url: '/documentation' }) - t.equal(typeof res.payload, 'string') - t.match(res.payload, /theme\/theme\.css/) - t.equal(res.headers['content-type'], 'text/html; charset=utf-8') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.match(res.payload, /theme\/theme\.css/) + t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8') { const res = await fastify.inject({ method: 'GET', url: '/documentation/static/theme/theme.css' }) - t.equal(res.payload, '* {border: 1px red solid;}') + t.assert.deepStrictEqual(res.payload, '* {border: 1px red solid;}') } }) @@ -202,17 +202,17 @@ test('swagger route returns additional theme - only favicon', async (t) => { url: '/documentation' }) - t.equal(typeof res.payload, 'string') - t.match(res.payload, /theme\/favicon\.png/) - t.equal(res.headers['content-type'], 'text/html; charset=utf-8') + t.assert.deepStrictEqual(typeof res.payload, 'string') + t.assert.match(res.payload, /theme\/favicon\.png/) + t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8') { const res = await fastify.inject({ method: 'GET', url: '/documentation/static/theme/favicon.png' }) - t.equal(res.statusCode, 200) - t.equal(res.headers['content-type'], 'image/png') + t.assert.deepStrictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(res.headers['content-type'], 'image/png') } }) @@ -238,7 +238,7 @@ test('swagger route returns additional theme - only title', async (t) => { url: '/documentation' }) - t.equal(typeof res.payload, 'string') - t.match(res.payload, /