Skip to content

Commit

Permalink
chore: node test migration and ts fix
Browse files Browse the repository at this point in the history
  • Loading branch information
simoneb committed Nov 12, 2024
1 parent c467494 commit 29fd7d9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 66 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,3 @@ yarn.lock
# editor files
.vscode
.idea

#tap files
.tap/
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"fastify": "^5.0.0",
"snazzy": "^9.0.0",
"standard": "^17.1.0",
"tap": "^18.7.2",
"tsd": "^0.31.0"
},
"resolutions": {
Expand All @@ -29,8 +28,8 @@
"lint:fix": "standard --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap",
"test:watch": "tap --watch --no-coverage-report"
"test:unit": "node --test --experimental-test-coverage",
"test:watch": "node --test --watch"
},
"repository": {
"type": "git",
Expand Down
42 changes: 17 additions & 25 deletions test/fastify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

const { serverFactory, TIME, unmockTime, mockTime } = require('./helpers')
const pretty = require('pino-pretty')
const tap = require('tap')

const { test } = tap
const { before, beforeEach, after, test } = require('node:test')

const messages = []
let server = serverFactory(messages, { colorize: false })

tap.before(() => {
before(() => {
mockTime()
})

tap.teardown(() => {
after(() => {
unmockTime()
})

tap.beforeEach(() => {
beforeEach(() => {
// empty messages array
messages.splice(0, messages.length)

Expand All @@ -27,21 +25,20 @@ tap.beforeEach(() => {
test('should log server started messages', async (t) => {
t.beforeEach(async () => {
await server.listen({ port: 63995 })
t.teardown(async () => await server.close())
t.afterEach(async () => await server.close())
})

t.test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => {
await t.test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => {
const messagesExpected = [
`${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mServer listening at http://127.0.0.1:63995\x1B[39m\n`,
`${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mServer listening at http://[::1]:63995\x1B[39m\n`
]

// sort because the order of the messages is not guaranteed
t.same(messages.sort(), messagesExpected.sort())
t.end()
t.assert.strictEqual(messages.sort(), messagesExpected.sort())
})

t.test(
await t.test(
'colors not supported in TTY',
{ skip: pretty.isColorSupported },
(t) => {
Expand All @@ -51,8 +48,7 @@ test('should log server started messages', async (t) => {
]

// sort because the order of the messages is not guaranteed
t.same(messages.sort(), messagesExpected.sort())
t.end()
t.assert.strictEqual(messages.sort(), messagesExpected.sort())
}
)
})
Expand All @@ -72,29 +68,27 @@ methods.forEach((method) => {
})
})

t.test(
await t.test(
'colors supported in TTY',
{ skip: !pretty.isColorSupported },
(t) => {
const messagesExpected = [
`${TIME} - \x1B[32minfo\x1B[39m - ${method} /path - \x1B[36mincoming request\x1B[39m\n`,
`${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mrequest completed\x1B[39m\n`
]
t.same(messages, messagesExpected)
t.end()
t.assert.deepStrictEqual(messages, messagesExpected)
}
)

t.test(
await t.test(
'colors not supported in TTY',
{ skip: pretty.isColorSupported },
(t) => {
const messagesExpected = [
`${TIME} - info - ${method} /path - incoming request\n`,
`${TIME} - info - request completed\n`
]
t.same(messages, messagesExpected)
t.end()
t.assert.deepStrictEqual(messages, messagesExpected)
}
)
})
Expand All @@ -118,7 +112,7 @@ test('should handle user defined log', async (t) => {
await server.inject('/a-path-with-user-defined-log')
})

t.test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => {
await t.test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => {
const messagesExpected = [
`${TIME} - \x1B[32minfo\x1B[39m - GET /a-path-with-user-defined-log - \x1B[36mincoming request\x1B[39m\n`,
`${TIME} - \x1B[41mfatal\x1B[49m - \x1B[36ma user defined fatal log\x1B[39m\n`,
Expand All @@ -129,11 +123,10 @@ test('should handle user defined log', async (t) => {
`${TIME} - \x1B[90mtrace\x1B[39m - \x1B[36ma user defined trace log\x1B[39m\n`,
`${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mrequest completed\x1B[39m\n`
]
t.same(messages, messagesExpected)
t.end()
t.assert.deepStrictEqual(messages, messagesExpected)
})

t.test(
await t.test(
'colors not supported in TTY',
{ skip: pretty.isColorSupported },
(t) => {
Expand All @@ -147,8 +140,7 @@ test('should handle user defined log', async (t) => {
`${TIME} - trace - a user defined trace log\n`,
`${TIME} - info - request completed\n`
]
t.same(messages, messagesExpected)
t.end()
t.assert.deepStrictEqual(messages, messagesExpected)
}
)
})
4 changes: 2 additions & 2 deletions test/formatDate.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const formatDate = require('../lib/formatDate')
const test = require('tap').test
const { test } = require('node:test')

const timeFormatRE = /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d$/

Expand All @@ -12,6 +12,6 @@ test('should generate valid formatted time', (t) => {

for (let i = 0; i < iterations; ++i) {
const randomInt = Math.floor(Math.random() * maxTimestamp)
t.ok(timeFormatRE.test(formatDate(randomInt)))
t.assert.ok(timeFormatRE.test(formatDate(randomInt)))
}
})
50 changes: 19 additions & 31 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@

const { EPOCH, TIME, MESSAGE_KEY, mockTime, unmockTime } = require('./helpers')
const target = require('..')
const tap = require('tap')
const { before, after, test } = require('node:test')
const pretty = require('pino-pretty')
const { messageFormatFactory } = target

const { test } = tap

const messageFormat = messageFormatFactory(
undefined,
undefined,
pretty.isColorSupported
)

tap.before(() => {
before(() => {
mockTime()
})

tap.teardown(() => {
after(() => {
unmockTime()
})

test('able to instantiate target without arguments', (t) => {
target()
t.pass()
t.end()
})

test('format log correctly with different logDescriptor', async (t) => {
Expand All @@ -50,15 +46,14 @@ test('format log correctly with different logDescriptor', async (t) => {
]
]

await logDescriptorLogPairs.forEach(
await Promise.all(logDescriptorLogPairs.map(
async ([logDescriptor, expectedLogColored, expectedLogUncolored]) => {
await t.test(
'colors supported in TTY',
{ skip: !pretty.isColorSupported },
(t) => {
const log = messageFormat(logDescriptor, MESSAGE_KEY)
t.equal(log, expectedLogColored)
t.end()
t.assert.strictEqual(log, expectedLogColored)
}
)

Expand All @@ -67,12 +62,11 @@ test('format log correctly with different logDescriptor', async (t) => {
{ skip: pretty.isColorSupported },
(t) => {
const log = messageFormat(logDescriptor, MESSAGE_KEY)
t.equal(log, expectedLogUncolored)
t.end()
t.assert.strictEqual(log, expectedLogUncolored)
}
)
}
)
))
})

test('colorize log correctly with different logDescriptor', async (t) => {
Expand All @@ -97,15 +91,14 @@ test('colorize log correctly with different logDescriptor', async (t) => {
]
]

await logDescriptorColorizedLogPairs.forEach(
await Promise.all(logDescriptorColorizedLogPairs.map(
async ([logDescriptor, expectedLogColored, expectedLogUncolored]) => {
await t.test(
'colors supported in TTY',
{ skip: !pretty.isColorSupported },
(t) => {
const log = messageFormat(logDescriptor, MESSAGE_KEY)
t.equal(log, expectedLogColored)
t.end()
t.assert.strictEqual(log, expectedLogColored)
}
)

Expand All @@ -114,12 +107,11 @@ test('colorize log correctly with different logDescriptor', async (t) => {
{ skip: pretty.isColorSupported },
(t) => {
const log = messageFormat(logDescriptor, MESSAGE_KEY)
t.equal(log, expectedLogUncolored)
t.end()
t.assert.strictEqual(log, expectedLogUncolored)
}
)
}
)
))
})

test('format log correctly with custom levels', async (t) => {
Expand Down Expand Up @@ -154,15 +146,14 @@ test('format log correctly with custom levels', async (t) => {
]
]

await logCustomLevelsLogPairs.forEach(
await Promise.all(logCustomLevelsLogPairs.map(
async ([logDescriptor, expectedLogColored, expectedLogUncolored]) => {
await t.test(
'colors supported in TTY',
{ skip: !pretty.isColorSupported },
(t) => {
const log = messageFormat(logDescriptor, MESSAGE_KEY)
t.equal(log, expectedLogColored)
t.end()
t.assert.strictEqual(log, expectedLogColored)
}
)

Expand All @@ -171,12 +162,11 @@ test('format log correctly with custom levels', async (t) => {
{ skip: pretty.isColorSupported },
(t) => {
const log = messageFormat(logDescriptor, MESSAGE_KEY)
t.equal(log, expectedLogUncolored)
t.end()
t.assert.strictEqual(log, expectedLogUncolored)
}
)
}
)
))
})

test('format log correctly with custom colors per level', async (t) => {
Expand Down Expand Up @@ -214,15 +204,14 @@ test('format log correctly with custom colors per level', async (t) => {
]
]

await logCustomLevelsLogPairs.forEach(
await Promise.all(logCustomLevelsLogPairs.map(
async ([logDescriptor, expectedLogColored, expectedLogUncolored]) => {
await t.test(
'colors supported in TTY',
{ skip: !pretty.isColorSupported },
(t) => {
const log = messageFormat(logDescriptor, MESSAGE_KEY)
t.equal(log, expectedLogColored)
t.end()
t.assert.strictEqual(log, expectedLogColored)
}
)

Expand All @@ -231,10 +220,9 @@ test('format log correctly with custom colors per level', async (t) => {
{ skip: pretty.isColorSupported },
(t) => {
const log = messageFormat(logDescriptor, MESSAGE_KEY)
t.equal(log, expectedLogUncolored)
t.end()
t.assert.strictEqual(log, expectedLogUncolored)
}
)
}
)
))
})
4 changes: 2 additions & 2 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pretty from 'pino-pretty';
import { expectType } from "tsd";
import { expectType, expectAssignable } from "tsd";
import oneLineLogger, {
CustomColor,
LogDescriptor,
Expand All @@ -18,4 +18,4 @@ expectType<Request | undefined>(({} as LogDescriptor).req)
expectType<(colorize: boolean, levels: Record<string, number>, colors?: CustomColor) => (log: LogDescriptor, messageKey: string) => string>(messageFormatFactory)

expectType<typeof oneLineLogger>(oneLineLoggerNamed)
expectType<(opts?: pretty.PrettyOptions) => pretty.PrettyStream>(oneLineLogger)
expectAssignable<(opts?: pretty.PrettyOptions) => pretty.PrettyStream>(oneLineLogger)

0 comments on commit 29fd7d9

Please sign in to comment.