Skip to content

kimjoar/compare-errors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

compare errors

Install

npm install compare-errors

Usage

You can compare errors based on:

  • constructor:

    const err = new Error('foo')
    const res = compareErrors(Error)(err)
    // {
    //   type: 'constructor',
    //   matches: true,
    //   actual: 'Error: foo',
    //   expected: 'Error'
    // }
  • constructor and string message:

    const err = new Error('foo')
    const res = compareErrors(Error, 'foo')(err)
    // {
    //   type: 'message',
    //   matches: true,
    //   actual: 'foo',
    //   expected: 'foo'
    // }
  • constructor and regex message:

    const err = new Error('foo')
    const res = compareErrors(Error, /bar/)(err)
    // {
    //   type: 'message',
    //   matches: false,
    //   actual: 'foo',
    //   expected: /bar/
    // }
  • just a string or regex:

    const err = new Error('foo')
    const res = compareErrors(/foo/)(err)
    // {
    //   type: 'message',
    //   matches: true,
    //   actual: 'foo',
    //   expected: /foo/
    // }
  • instance:

    const err = new Error('foo')
    const err2 = new RangeError('foo')
    const res = compareErrors(err)(err2)
    // {
    //   type: 'instance',
    //   matches: false,
    //   actual: 'RangeError: foo',
    //   expected: 'Error: foo'
    // }

About

Compare errors based on instance, constructor and message

Resources

License

Stars

Watchers

Forks

Packages

No packages published