Skip to content

Commit

Permalink
chore: migrate from tap to native test runner (#172)
Browse files Browse the repository at this point in the history
* chore: migrate from tap to native test runner

* Merge branch 'master' into migrate-tap-to-native-test-runner

* remove coverage npm script

* fix test

* fix windows

* fix windows

* fix windows

---------

Signed-off-by: Aras Abbasi <aras.abbasi@googlemail.com>
  • Loading branch information
Uzlopak authored Aug 20, 2024
1 parent a625094 commit 3ac8496
Show file tree
Hide file tree
Showing 14 changed files with 426 additions and 401 deletions.
File renamed without changes.
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
49 changes: 0 additions & 49 deletions tap-snapshots/test/static.test.js.test.cjs

This file was deleted.

62 changes: 31 additions & 31 deletions test/csp.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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) => {
Expand All @@ -59,18 +59,18 @@ 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')
}

{
const res = await fastify.inject({
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')
}
})

Expand All @@ -95,18 +95,18 @@ 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')
}

{
const res = await fastify.inject({
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')
}
})

Expand Down Expand Up @@ -134,18 +134,18 @@ 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')
}

{
const res = await fastify.inject({
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')
}
})

Expand All @@ -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';"
}
})
Expand All @@ -174,18 +174,18 @@ 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')
}

{
const res = await fastify.inject({
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')
}
})

Expand All @@ -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';"
}
})
Expand All @@ -214,17 +214,17 @@ 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')
}

{
const res = await fastify.inject({
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")
}
})
8 changes: 4 additions & 4 deletions test/decorator.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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))
})
38 changes: 19 additions & 19 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const yaml = require('yaml')

Expand Down Expand Up @@ -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)
}
Expand All @@ -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 => {
Expand Down Expand Up @@ -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'])
})
4 changes: 2 additions & 2 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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)
})
Loading

0 comments on commit 3ac8496

Please sign in to comment.