Skip to content

Commit

Permalink
chore: create unit tests for parse-duration
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-m-dev committed Sep 9, 2024
1 parent 1ca4680 commit 5c59740
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions @vates/parse-duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ exports.parseDuration = value => {
if (typeof value === 'number') {
return value
}
if (typeof value !== 'string' || value === '') {
throw new TypeError(`not a valid duration: ${value}`)
}
const duration = ms(value)
if (duration === undefined) {
throw new TypeError(`not a valid duration: ${value}`)
Expand Down
22 changes: 22 additions & 0 deletions @vates/parse-duration/parse-duration.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { parseDuration } from '@vates/parse-duration/index.js'

describe('`ms` without magic: always parse a duration and throws if invalid.', () => {
it('should parse string "2 days"', () => {
const input = '2 days'
const expected = 172800000
assert.strictEqual(parseDuration(input), expected)
})

it('should return the same duration if it is already in milliseconds', () => {
const input = 172800000
const expected = 172800000
assert.strictEqual(parseDuration(input), expected)
})

it('should throw an error for an undefined duration', () => {
const input = undefined
assert.throws(() => parseDuration(input), { message: 'not a valid duration: undefined' })
})
})
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<!--packages-start-->

- @vates/parse-duration minor
- xo-server minor
- xo-server-perf-alert minor
- xo-web minor
Expand Down

0 comments on commit 5c59740

Please sign in to comment.