Skip to content

Commit

Permalink
chore: migrate test suite from tap to node:test (#190)
Browse files Browse the repository at this point in the history
* migrated tap to node:test with c8 coverage

* removed .taprc

* fixed linting issues

* updated c8 to v10

* added t.plan and fixed failing tests
  • Loading branch information
narendra-reddy-nf authored Sep 11, 2024
1 parent 0609866 commit 8fec680
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
2 changes: 0 additions & 2 deletions .taprc

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"coverage": "cross-env VALUE_FROM_ENV=pippo tap --coverage-report=html",
"coverage": "cross-env VALUE_FROM_ENV=pippo c8 --reporter=html node --test",
"lint": "standard | snazzy",
"lint:fix": "standard --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "cross-env VALUE_FROM_ENV=pippo tap"
"test:unit": "c8 --100 cross-env VALUE_FROM_ENV=pippo node --test"
},
"repository": {
"type": "git",
Expand All @@ -37,7 +37,7 @@
"fastify": "^5.0.0-alpha.4",
"snazzy": "^9.0.0",
"standard": "^17.1.0",
"tap": "^20.0.1",
"c8": "^10.1.2",
"tsd": "^0.31.0"
},
"pre-commit": [
Expand Down
46 changes: 24 additions & 22 deletions test/fastify-env.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
'use strict'

const t = require('tap')
const t = require('node:test')
const path = require('node:path')
const Fastify = require('fastify')
const fastifyEnv = require('../index')

function makeTest (t, options, isOk, confExpected, errorMessage) {
async function makeTest (t, options, isOk, confExpected, errorMessage) {
t.plan(isOk ? 2 : 1)

const fastify = Fastify()
fastify.register(fastifyEnv, options)
.ready(err => {
if (isOk) {
t.notOk(err)
t.strictSame(fastify.config, confExpected)
return
}
try {
await fastify.register(fastifyEnv, options)
await fastify.ready()

t.strictSame(err.message, errorMessage)
})
if (isOk) {
t.assert.ifError(null)
t.assert.deepStrictEqual(fastify.config, confExpected)
}
} catch (err) {
if (!isOk) {
t.assert.deepStrictEqual(err.message, errorMessage)
}
}
}

const tests = [
Expand Down Expand Up @@ -248,19 +251,19 @@ const tests = [
]

tests.forEach(function (testConf) {
t.test(testConf.name, t => {
t.test(testConf.name, async t => {
const options = {
schema: testConf.schema,
data: testConf.data,
dotenv: testConf.dotenv,
dotenvConfig: testConf.dotenvConfig
}

makeTest(t, options, testConf.isOk, testConf.confExpected, testConf.errorMessage)
await makeTest(t, options, testConf.isOk, testConf.confExpected, testConf.errorMessage)
})
})

t.test('should use custom config key name', t => {
t.test('should use custom config key name', async (t) => {
t.plan(1)
const schema = {
type: 'object',
Expand All @@ -274,13 +277,12 @@ t.test('should use custom config key name', t => {
}

const fastify = Fastify()
fastify.register(fastifyEnv, {
await fastify.register(fastifyEnv, {
schema,
confKey: 'customConfigKeyName'
})
.ready(() => {
t.strictSame(fastify.customConfigKeyName, { PORT: 6666 })
})
await fastify.ready()
t.assert.deepStrictEqual(fastify.customConfigKeyName, { PORT: 6666 })
})

t.test('should use function `getEnvs` to retrieve the envs object', async t => {
Expand Down Expand Up @@ -310,8 +312,8 @@ t.test('should use function `getEnvs` to retrieve the envs object', async t => {
url: '/'
})

t.strictSame(requestEnvs, { PORT: 6666 })
t.strictSame(fastify.getEnvs(), { PORT: 6666 })
t.assert.deepStrictEqual(requestEnvs, { PORT: 6666 })
t.assert.deepStrictEqual(fastify.getEnvs(), { PORT: 6666 })
})
t.test('should skip the getEnvs decorators if there is already one with the same name', async t => {
t.plan(2)
Expand Down Expand Up @@ -342,6 +344,6 @@ t.test('should skip the getEnvs decorators if there is already one with the same
url: '/'
})

t.strictSame(requestEnvs, 'another_value')
t.strictSame(fastify.getEnvs(), 'another_value')
t.assert.deepStrictEqual(requestEnvs, 'another_value')
t.assert.deepStrictEqual(fastify.getEnvs(), 'another_value')
})

0 comments on commit 8fec680

Please sign in to comment.