8000 test: migrate to node test runner by ilteoood · Pull Request #64 · fastify/fast-json-stringify-compiler · GitHub
[go: up one dir, main page]

Skip to content

test: migrate to node test runner #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"lint": "eslint",
"lint:fix": "eslint --fix",
"unit": "tap test/**/*.test.js",
"unit": "c8 --100 node --test",
"test": "npm run unit && npm run test:typescript",
"test:typescript": "tsd"
},
Expand Down Expand Up @@ -54,11 +54,11 @@
],
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"c8": "^10.1.3",
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"neostandard": "^0.12.0",
"sanitize-filename": "^1.6.3",
"tap": "^18.7.2",
"tsd": "^0.31.0"
},
"pre-commit": [
Expand Down
6 changes: 3 additions & 3 deletions test/duplicate-schema.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const t = require('tap')
const { test } = require('node:test')
const FjsCompiler = require('../index')

t.test('Use input schema duplicate in the externalSchemas', async t => {
test('Use input schema duplicate in the externalSchemas', async t => {
t.plan(1)
const externalSchemas = {
schema1: {
Expand All @@ -22,5 +22,5 @@ t.test('Use input schema duplicate in the externalSchemas', async t => {
compiler({ schema: externalSchemas.schema1 })
compiler({ schema: externalSchemas.schema2 })

t.pass()
t.assert.ok(true)
})
12 changes: 6 additions & 6 deletions test/plugin.test.js
8000
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const t = require('tap')
const { test } = require('node:test')
const fastify = require('fastify')
const FjsCompiler = require('../index')

Expand All @@ -27,16 +27,16 @@ const externalSchemas2 = Object.freeze({

const fastifyFjsOptionsDefault = Object.freeze({})

t.test('basic usage', t => {
test('basic usage', t => {
t.plan(1)
const factory = FjsCompiler()
const compiler = factory(externalSchemas1, fastifyFjsOptionsDefault)
const serializeFunc = compiler({ schema: sampleSchema })
const result = serializeFunc({ name: 'hello' })
t.equal(result, '{"name":"hello"}')
t.assert.equal(result, '{"name":"hello"}')
})

t.test('fastify integration', async t => {
test('fastify integration', async t => {
const factory = FjsCompiler()

const app = fastify({
Expand Down Expand Up @@ -73,6 +73,6 @@ t.test('fastify integration', async t => {
}
})

t.equal(res.statusCode, 200)
t.same(res.json(), { name: 'serialize me' })
t.assert.equal(res.statusCode, 200)
t.assert.deepStrictEqual(res.json(), { name: 'serialize me' })
})
46 changes: 23 additions & 23 deletions test/standalone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('node:fs')
const path = require('node:path')
const t = require('tap')
const { test } = require('node:test')
const fastify = require('fastify')
const sanitize = require('sanitize-filename')

Expand All @@ -16,10 +16,10 @@ function generateFileName (routeOpts) {
return fileName
}

t.test('standalone', t => {
test('standalone', async t => {
t.plan(5)

t.teardown(async () => {
t.after(async () => {
for (const fileName of generatedFileNames) {
try {
await fs.promises.unlink(path.join(__dirname, fileName))
Expand All @@ -29,10 +29,10 @@ t.test('standalone', t => {

t.test('errors', t => {
t.plan(2)
t.throws(() => {
t.assert.throws(() => {
FjsStandaloneCompiler()
}, 'missing restoreFunction')
t.throws(() => {
t.assert.throws(() => {
FjsStandaloneCompiler({ readMode: false })
}, 'missing storeFunction')
})
Expand Down Expand Up @@ -74,28 +74,28 @@ t.test('standalone', t => {
const factory = FjsStandaloneCompiler({
readMode: false,
storeFunction (routeOpts, schemaSerializerCode) {
t.same(routeOpts, endpointSchema)
t.type(schemaSerializerCode, 'string')
t.assert.deepStrictEqual(routeOpts, endpointSchema)
t.assert.ok(typeof schemaSerializerCode === 'string')
fs.writeFileSync(path.join(__dirname, '/fjs-generated.js'), schemaSerializerCode)
generatedFileNames.push('/fjs-generated.js')
t.pass('stored the serializer function')
t.assert.ok('stored the serializer function')
}
})

const compiler = factory(schemaMap)
compiler(endpointSchema)
t.pass('compiled the endpoint schema')
t.assert.ok('compiled the endpoint schema')

t.test('usage standalone code', t => {
t.plan(3)
const standaloneSerializer = require('./fjs-generated')
t.ok(standaloneSerializer)
t.assert.ok(standaloneSerializer)

const valid = standaloneSerializer({ hello: 'world' })
t.same(valid, JSON.stringify({ hello: 'world' }))
t.assert.deepStrictEqual(valid, JSON.stringify({ hello: 'world' }))

const invalid = standaloneSerializer({ hello: [] })
t.same(invalid, '{"hello":""}')
t.assert.deepStrictEqual(invalid, '{"hello":""}')
})
})

Expand All @@ -106,9 +106,9 @@ t.test('standalone', t => {
readMode: false,
storeFunction (routeOpts, schemaSerializationCode) {
const fileName = generateFileName(routeOpts)
t.ok(routeOpts)
t.assert.ok(routeOpts)
fs.writeFileSync(path.join(__dirname, fileName), schemaSerializationCode)
t.pass(`stored the serializer function ${fileName}`)
t.assert.ok(`stored the serializer function ${fileName}`)
},
restoreFunction () {
t.fail('write mode ON')
Expand All @@ -119,16 +119,16 @@ t.test('standalone', t => {
await app.ready()
})

t.test('fastify integration - writeMode forces standalone', async t => {
await t.test('fastify integration - writeMode forces standalone', async t => {
t.plan(4)

const factory = FjsStandaloneCompiler({
readMode: false,
storeFunction (routeOpts, schemaSerializationCode) {
const fileName = generateFileName(routeOpts)
t.ok(routeOpts)
t.assert.ok(routeOpts)
fs.writeFileSync(path.join(__dirname, fileName), schemaSerializationCode)
t.pass(`stored the serializer function ${fileName}`)
t.assert.ok(`stored the serializer function ${fileName}`)
},
restoreFunction () {
t.fail('write mode ON')
Expand All @@ -143,7 +143,7 @@ t.test('standalone', t => {
await app.ready()
})

t.test('fastify integration - readMode', async t => {
await t.test('fastify integration - readMode', async t => {
t.plan(6)

const factory = FjsStandaloneCompiler({
Expand All @@ -153,7 +153,7 @@ t.test('standalone', t => {
},
restoreFunction (routeOpts) {
const fileName = generateFileName(routeOpts)
t.pass(`restore the serializer function ${fileName}}`)
t.assert.ok(`restore the serializer function ${fileName}}`)
return require(path.join(__dirname, fileName))
}
})
Expand All @@ -165,15 +165,15 @@ t.test('standalone', t => {
url: '/foo',
method: 'POST'
})
t.equal(res.statusCode, 200)
t.equal(res.payload, JSON.stringify({ hello: 'world' }))
t.assert.equal(res.statusCode, 200)
t.assert.equal(res.payload, JSON.stringify({ hello: 'world' }))

res = await app.inject({
url: '/bar?lang=it',
method: 'GET'
})
t.equal(res.statusCode, 200)
t.equal(res.payload, JSON.stringify({ lang: 'en' }))
t.assert.equal(res.statusCode, 200)
t.assert.equal(res.payload, JSON.stringify({ lang: 'en' }))
})

function buildApp (factory, serializerOpts) {
Expand Down
0