Skip to content

Commit

Permalink
test that optArg throws on bad arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 19, 2023
1 parent 1b95def commit 89b38cf
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ export interface RimrafOptions {
maxBackoff?: number
}

/* c8 ignore start */
const typeOrUndef = (val: any, t: string) =>
typeof val === 'undefined' || typeof val === t
/* c8 ignore stop */

export const isRimrafOptions = (o: any): o is RimrafOptions =>
!!o &&
Expand All @@ -25,15 +23,13 @@ export const isRimrafOptions = (o: any): o is RimrafOptions =>
typeOrUndef(o.backoff, 'number') &&
typeOrUndef(o.maxBackoff, 'number')

/* c8 ignore start */
export const assertRimrafOptions: (o: any) => void = (
o: any
): asserts o is RimrafOptions => {
if (!isRimrafOptions(o)) {
throw new Error('invalid rimraf options')
}
}
/* c8 ignore stop */

import { rimrafManual, rimrafManualSync } from './rimraf-manual.js'
import { rimrafMoveRemove, rimrafMoveRemoveSync } from './rimraf-move-remove.js'
Expand Down
5 changes: 5 additions & 0 deletions test/opt-arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ const optArg = require('../dist/cjs/src/opt-arg.js').default
const opt = { a: 1 }
t.equal(optArg(opt), opt, 'returns object if provided')
t.same(optArg(), {}, 'returns new object otherwise')

t.throws(() => optArg(true))
t.throws(() => optArg(null))
t.throws(() => optArg('hello'))
t.throws(() => optArg({ maxRetries: 'banana' }))

0 comments on commit 89b38cf

Please sign in to comment.