mirror of
https://github.com/vatesfr/xen-orchestra.git
synced 2026-09-10 22:14:48 -05:00
test(parse-duration): create unit tests (#7968)
This commit is contained in:
@@ -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}`)
|
||||
}
|
||||
|
||||
22
@vates/parse-duration/index.test.mjs
Normal file
22
@vates/parse-duration/index.test.mjs
Normal 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}` })
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -27,6 +27,7 @@
|
||||
"ms": "^2.1.2"
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public"
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user