test(parse-duration): create unit tests (#7968)

This commit is contained in:
Stephane M.
2024-09-22 20:50:42 +02:00
committed by GitHub
parent 7e14152473
commit 381c83d0b3
3 changed files with 30 additions and 5 deletions

View File

@@ -6,9 +6,11 @@ exports.parseDuration = value => {
if (typeof value === 'number') {
return value
}
const duration = ms(value)
if (duration === undefined) {
throw new TypeError(`not a valid duration: ${value}`)
if (typeof value === 'string' && value !== '') {
const duration = ms(value)
if (duration !== undefined) {
return duration
}
}
return duration
throw new TypeError(`not a valid duration: ${value}`)
}

View File

@@ -0,0 +1,22 @@
import { describe, it } from 'node:test'
import { parseDuration } from '@vates/parse-duration'
import assert from 'node:assert/strict'
describe('parseDuration()', () => {
it('should parse string', () => {
const input = '2 days'
const expected = 172800000
assert.strictEqual(parseDuration(input), expected)
})
it('should return its input if already a number', () => {
const input = 172800000
assert.strictEqual(parseDuration(input), input)
})
for (const input of [undefined, '', 'invalid duration']) {
it('should throw an error for ' + input, () => {
assert.throws(() => parseDuration(input), { message: `not a valid duration: ${input}` })
})
}
})

View File

@@ -27,6 +27,7 @@
"ms": "^2.1.2"
},
"scripts": {
"postversion": "npm publish --access public"
"postversion": "npm publish --access public",
"test": "node --test"
}
}