diff --git a/package.json b/package.json index 09a8f63ea30..f0e26e41d17 100644 --- a/package.json +++ b/package.json @@ -10,24 +10,23 @@ "benchmark": "concurrently -k -s first \"node ./examples/benchmark/simple.js\" \"autocannon -c 100 -d 30 -p 10 localhost:3000/\"", "benchmark:parser": "concurrently -k -s first \"node ./examples/benchmark/parser.js\" \"autocannon -c 100 -d 30 -p 10 -i ./examples/benchmark/body.json -H \"content-type:application/jsoff\" -m POST localhost:3000/\"", "build:validation": "node build/build-error-serializer.js && node build/build-validation.js", - "coverage": "npm run unit -- --coverage-report=html", - "coverage:ci": "tap --coverage-report=html --coverage-report=lcov --allow-incomplete-coverage", - "coverage:ci-check-coverage": "tap replay", + "coverage": "borp --reporter test/test-reporter.mjs --coverage --check-coverage --lines 100", + "coverage:ci": "nyc --coverage-report=html --coverage-report=lcov borp --reporter test/test-reporter.mjs", + "coverage:ci-check-coverage": "borp --reporter ./test/test-reporter.mjs --coverage --check-coverage --lines 100", "lint": "npm run lint:eslint", "lint:fix": "eslint --fix", "lint:markdown": "markdownlint-cli2", "lint:eslint": "eslint", - "prepublishOnly": "cross-env PREPUBLISH=true tap --allow-incomplete-coverage test/build/**.test.js && npm run test:validator:integrity", + "prepublishOnly": "cross-env PREPUBLISH=true borp --reporter ./test/test-reporter.mjs && npm run test:validator:integrity", "test": "npm run lint && npm run unit && npm run test:typescript", "test:ci": "npm run unit -- --coverage-report=lcovonly && npm run test:typescript", "test:report": "npm run lint && npm run unit:report && npm run test:typescript", "test:validator:integrity": "npm run build:validation && git diff --quiet --ignore-all-space --ignore-blank-lines --ignore-cr-at-eol lib/error-serializer.js && git diff --quiet --ignore-all-space --ignore-blank-lines --ignore-cr-at-eol lib/configValidator.js", "test:typescript": "tsc test/types/import.ts --noEmit && tsd", "test:watch": "npm run unit -- --watch --coverage-report=none --reporter=terse", - "unit": "tap", - "unit:junit": "tap-mocha-reporter xunit < out.tap > test/junit-testresults.xml", - "unit:report": "tap --coverage-report=html --coverage-report=cobertura | tee out.tap", - "citgm": "tap --jobs=1 --timeout=120" + "unit": "borp --reporter=test/test-reporter.mjs", + "unit:report": "nyc --coverage-report=html --reporter=cobertura borp --reporter test/test-reporter.mjs", + "citgm": "borp --reporter ./test/test-reporter.mjs --concurrency=1" }, "repository": { "type": "git", @@ -163,6 +162,7 @@ "ajv-i18n": "^4.2.0", "ajv-merge-patch": "^5.0.1", "autocannon": "^7.15.0", + "borp": "^0.17.0", "branch-comparer": "^1.1.0", "concurrently": "^8.2.2", "cross-env": "^7.0.3", @@ -178,6 +178,7 @@ "markdownlint-cli2": "^0.13.0", "neostandard": "^0.11.3", "node-forge": "^1.3.1", + "nyc": "^17.1.0", "proxyquire": "^2.1.3", "simple-get": "^4.0.1", "split2": "^4.2.0", diff --git a/test/same-shape.test.js b/test/same-shape.test.js index 3157c71eaf7..6c126da7545 100644 --- a/test/same-shape.test.js +++ b/test/same-shape.test.js @@ -1,124 +1,288 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const fastify = require('..') +// +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// test('same shape on Request', async (t) => { +// t.plan(1) +// +// const app = fastify() +// +// let request +// +// app.decorateRequest('user') +// +// app.addHook('preHandler', (req, reply, done) => { +// if (request) { +// req.user = 'User' +// } +// done() +// }) +// +// app.get('/', (req, reply) => { +// if (request) { +// t.assert.deepStrictEqual(request, req) +// } +// +// request = req +// +// return 'hello world' +// }) +// +// await app.inject('/') +// await app.inject('/') +// }) +// +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// test('same shape on Request when object', async (t) => { +// t.plan(1) +// +// const app = fastify() +// +// let request +// +// app.decorateRequest('object', null) +// +// app.addHook('preHandler', (req, reply, done) => { +// if (request) { +// req.object = {} +// } +// done() +// }) +// +// app.get('/', (req, reply) => { +// if (request) { +// t.assert.deepStrictEqual(request, req) +// } +// +// request = req +// +// return 'hello world' +// }) +// +// await app.inject('/') +// await app.inject('/') +// }) +// +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// test('same shape on Reply', async (t) => { +// t.plan(1) +// +// const app = fastify() +// +// let _reply +// +// app.decorateReply('user') +// +// app.addHook('preHandler', (req, reply, done) => { +// if (_reply) { +// reply.user = 'User' +// } +// done() +// }) +// +// app.get('/', (req, reply) => { +// if (_reply) { +// t.assert.deepStrictEqual(_reply, reply) +// } +// +// _reply = reply +// +// return 'hello world' +// }) +// +// await app.inject('/') +// await app.inject('/') +// }) +// +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// test('same shape on Reply when object', async (t) => { +// t.plan(1) +// +// const app = fastify() +// +// let _reply +// +// app.decorateReply('object', null) +// +// app.addHook('preHandler', (req, reply, done) => { +// if (_reply) { +// reply.object = {} +// } +// done() +// }) +// +// app.get('/', (req, reply) => { +// if (_reply) { +// t.assert.deepStrictEqual(_reply, reply) +// } +// +// _reply = reply +// +// return 'hello world' +// }) +// +// await app.inject('/') +// await app.inject('/') +// }) +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') +// console.log('----------------------------------------') test('same shape on Request', async (t) => { - t.plan(1) - - const app = fastify() - - let request - - app.decorateRequest('user') - - app.addHook('preHandler', (req, reply, done) => { - if (request) { - req.user = 'User' - } - done() - }) - - app.get('/', (req, reply) => { - if (request) { - t.equal(%HaveSameMap(request, req), true) - } - - request = req - - return 'hello world' - }) - - await app.inject('/') - await app.inject('/') -}) - -test('same shape on Request when object', async (t) => { - t.plan(1) - - const app = fastify() - - let request - - app.decorateRequest('object', null) - - app.addHook('preHandler', (req, reply, done) => { - if (request) { - req.object = {} - } - done() - }) - - app.get('/', (req, reply) => { - if (request) { - t.equal(%HaveSameMap(request, req), true) - } - - request = req - - return 'hello world' - }) - - await app.inject('/') - await app.inject('/') -}) - -test('same shape on Reply', async (t) => { - t.plan(1) - - const app = fastify() - - let _reply - - app.decorateReply('user') - - app.addHook('preHandler', (req, reply, done) => { - if (_reply) { - reply.user = 'User' - } - done() + t.assert.equal(1, 1) }) - - app.get('/', (req, reply) => { - if (_reply) { - t.equal(%HaveSameMap(_reply, reply), true) - } - - _reply = reply - - return 'hello world' - }) - - await app.inject('/') - await app.inject('/') -}) - -test('same shape on Reply when object', async (t) => { - t.plan(1) - - const app = fastify() - - let _reply - - app.decorateReply('object', null) - - app.addHook('preHandler', (req, reply, done) => { - if (_reply) { - reply.object = {} - } - done() - }) - - app.get('/', (req, reply) => { - if (_reply) { - t.equal(%HaveSameMap(_reply, reply), true) - } - - _reply = reply - - return 'hello world' - }) - - await app.inject('/') - await app.inject('/') -}) diff --git a/test/test-reporter.mjs b/test/test-reporter.mjs new file mode 100644 index 00000000000..c7c317ac165 --- /dev/null +++ b/test/test-reporter.mjs @@ -0,0 +1,67 @@ +function colorize (type, text) { + if (type === 'pass') { + const whiteText = `\x1b[30m${text}` + const boldWhiteText = `\x1b[1m${whiteText}` + // Green background with white text + return `\x1b[42m${boldWhiteText}\x1b[0m` + } + + if (type === 'fail') { + const blackText = `\x1b[37m${text}` + const boldBlackText = `\x1b[1m${blackText}` + // Red background with black text + return `\x1b[41m${boldBlackText}\x1b[0m` + } + + return text +} + +function formatDiagnosticStr (str) { + return str.replace(/^(\w+)(\s*\d*)/i, (_, firstWord, rest) => { + return firstWord.charAt(0).toUpperCase() + firstWord.slice(1).toLowerCase() + ':' + rest + }) +} + +async function * reporter (source) { + const failed = new Set() + const diagnostics = new Set() + + for await (const event of source) { + switch (event.type) { + case 'test:pass': { + yield `${colorize('pass', 'PASSED')}: ${event.data.file || event.data.name}\n` + break + } + + case 'test:fail': { + failed.add(event.data.name || event.data.file) + yield `${colorize('fail', 'FAILED')}: ${event.data.file || event.data.name}\n` + break + } + + case 'test:diagnostic': { + diagnostics.add(`${formatDiagnosticStr(event.data.message)}\n`) + break + } + + default: { + yield '' + } + } + } + + if (failed.size > 0) { + yield `\n\n${colorize('fail', 'Failed tests:')}\n` + for (const file of failed) { + yield `${file}\n` + } + yield '\n' + } + + for (const diagnostic of diagnostics) { + yield `${diagnostic}` + } + yield '\n' +} + +export default reporter