Skip to content

Commit

Permalink
test(huston)!: use node test runners
Browse files Browse the repository at this point in the history
Signed-off-by: mateonunez <mateonunez95@gmail.com>
  • Loading branch information
mateonunez committed Nov 1, 2023
1 parent 7877ec7 commit 06760cc
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 62 deletions.
7 changes: 6 additions & 1 deletion packages/huston/huston.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ const normalizers = require('./lib/normalizers.js')
const database = require('./lib/helpers/database.js')
const logger = require('./lib/mocks/logger.js')

module.exports = { time, normalizers, database, logger }
module.exports = {
...time,
...normalizers,
...database,
logger
}
27 changes: 12 additions & 15 deletions packages/huston/lib/normalizers.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict'

function removeNulls (obj) {
if (typeof obj !== 'object') {
return obj
}
function removeNulls (object) {
if (object === null) return ''
if (typeof object !== 'object') return object

/* c8 ignore next 3 */
if (Array.isArray(obj)) {
return obj.map(removeNulls)
if (Array.isArray(object)) {
return object.map(removeNulls)
}

const newObj = {}
for (const [key, value] of Object.entries(obj)) {
for (const [key, value] of Object.entries(object)) {
if (value === null) {
newObj[key] = ''
} else {
Expand All @@ -22,18 +20,17 @@ function removeNulls (obj) {
return newObj
}

function removeReservedWords (obj) {
if (typeof obj !== 'object') {
return obj
function removeReservedWords (object) {
if (typeof object !== 'object') {
return object
}

/* c8 ignore next 3 */
if (Array.isArray(obj)) {
return obj.map(removeReservedWords)
if (Array.isArray(object)) {
return object.map(removeReservedWords)
}

const newObj = {}
for (const [key, value] of Object.entries(obj)) {
for (const [key, value] of Object.entries(object)) {
if (key !== 'id') {
newObj[key] = removeReservedWords(value)
}
Expand Down
6 changes: 2 additions & 4 deletions packages/huston/lib/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ function getNanoTime () {
if (typeof process !== 'undefined' && process.hrtime !== undefined) {
return process.hrtime.bigint()
}
/* c8 ignore next */

return BigInt(0)
}

function formatTime (time) {
/* c8 ignore start */
if (typeof time === 'number') {
time = BigInt(time)
}
Expand All @@ -19,11 +18,10 @@ function formatTime (time) {
} else if (time < BigInt(1e3)) {
return `${time}ns`
} else if (time < BigInt(1e6)) {
return `${time / BigInt(1e6)}μs`
return `${time / BigInt(1e3)}μs`
} else if (time < BigInt(1e9)) {
return `${time / BigInt(1e6)}ms`
}
/* c8 ignore stop */
return `${time / BigInt(1e9)}s`
}

Expand Down
5 changes: 2 additions & 3 deletions packages/huston/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"lint": "standard | snazzy",
"lint:fix": "standard --fix | snazzy",
"test": "npm run lint && c8 --100 tap --no-coverage test/**/*.test.js"
"test": "npm run lint:fix && c8 node --test --test-reporter spec"
},
"repository": {
"type": "git",
Expand All @@ -20,8 +20,7 @@
"devDependencies": {
"c8": "^8.0.1",
"snazzy": "^9.0.0",
"standard": "^17.1.0",
"tap": "^16.3.9"
"standard": "^17.1.0"
},
"author": {
"email": "mateonunez95@gmail.com",
Expand Down
39 changes: 0 additions & 39 deletions packages/huston/test/index.test.js

This file was deleted.

41 changes: 41 additions & 0 deletions packages/huston/test/normalizers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const test = require('node:test')
const assert = require('node:assert')
const { removeNulls, removeReservedWords } = require('./../lib/normalizers.js')

test.describe('normalizers', () => {
test('should remove nulls', () => {
const object = { a: null, b: 1 }
const normalized = removeNulls(object)
assert.deepStrictEqual(normalized, { a: '', b: 1 })
})

test('should remove reserved words', () => {
const object = { id: 1, a: 2 }
const normalized = removeReservedWords(object)
assert.deepStrictEqual(normalized, { a: 2 })
})

test('should remove nulls in arrays', () => {
const array = [null, 1, { a: null, b: 2 }, [null, 3]]
const normalized = removeNulls(array)
assert.deepStrictEqual(normalized, ['', 1, { a: '', b: 2 }, ['', 3]])
})

test('should remove reserved words in arrays', () => {
const arr = [{ id: 1, a: 2 }, [{ id: 3, b: 4 }]]
const normalized = removeReservedWords(arr)
assert.deepStrictEqual(normalized, [{ a: 2 }, [{ b: 4 }]])
})

test('should remove nulls in nested objects', () => {
const object = { a: { c: null, d: 2 }, b: [null, 3] }
const normalized = removeNulls(object)
assert.deepStrictEqual(normalized, { a: { c: '', d: 2 }, b: ['', 3] })
})

test('should remove reserved words in nested objects', () => {
const object = { a: { id: 1, c: 2 }, b: [{ id: 3, d: 4 }] }
const normalized = removeReservedWords(object)
assert.deepStrictEqual(normalized, { a: { c: 2 }, b: [{ d: 4 }] })
})
})
71 changes: 71 additions & 0 deletions packages/huston/test/time.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const test = require('node:test')
const assert = require('node:assert')
const { getNanoTime, formatTime, sleep } = require('./../lib/time.js')

test.describe('time', () => {
test.it('should get nano time', async () => {
const time = getNanoTime()
assert.equal(typeof time, 'bigint')
})

test.it('should format time in nanoseconds', async () => {
const time = BigInt(123)
const formattedTime = formatTime(time)
assert.equal(formattedTime, '123ns')
})

test.it('should format time in microseconds', async () => {
const time = BigInt(123456)
const formattedTime = formatTime(time)
assert.equal(formattedTime, '123μs')
})

test.it('should format time in milliseconds', async () => {
const time = BigInt(123456789)
const formattedTime = formatTime(time)
assert.equal(formattedTime, '123ms')
})

test.it('should format time in seconds', async () => {
const time = BigInt(1234567890)
const formattedTime = formatTime(time)
assert.equal(formattedTime, '1s')
})

test.it('should convert number to bigint and format time', async () => {
const time = 123456789
const formattedTime = formatTime(time)
assert.equal(formattedTime, '123ms')
})

test.it('should throw error for negative time', async () => {
const time = BigInt(-123)
assert.throws(() => formatTime(time), /microtime must be positive/)
})

test.it('should sleep for 100ms', async () => {
const start = getNanoTime()
await sleep(100)
const end = getNanoTime()
const elapsed = end - start
assert.equal(elapsed >= BigInt(1e8), true)
})

test.it('should return BigInt(0) if process or process.hrtime is undefined', () => {
const originalHrtime = process.hrtime

Object.defineProperty(process, 'hrtime', {
value: undefined,
writable: true
})

let time = getNanoTime()
assert.equal(time, BigInt(0))

process.hrtime = originalHrtime

time = getNanoTime()
assert.ok(typeof time === 'bigint')
assert.ok(time > BigInt(0))
})
})

0 comments on commit 06760cc

Please sign in to comment.