error-snapshot
Checks if given function throws an expected error (by saving snapshot)
Last updated 3 years ago by bahmutov .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install error-snapshot 
SYNC missed versions from official npm registry.

error-snapshot

Checks if given function throws an expected error (by saving snapshot)

NPM

Build status semantic-release js-standard-style

Install

Requires Node version 6 or above.

npm install --save-dev error-snapshot

Bring a snapshot function, for example snap-shot-it

npm install --save-dev snap-shot-it

Use

When checking if a function throws an error as expected when called

const snapshot = require('snap-shot-it')
const errorSnapshot = require('error-snapshot')(snapshot)
it('snapshots error', () => {
  const fn = () => {
    throw new Error('A test error')
  }
  errorSnapshot(fn)
})

First time the test runs, it will save error message snapshot. If the function does NOT throw an error, or if the error has a different message, it throws an error. The error message is saved into a snapshot file using snap-shot-it.

By default, the error message and the constructor name is saved, but you can specify additional properties to pick.

Example

// basic
it('snapshots error', () => {
  const fn = () => {
    throw new Error('A test error')
  }
  errorSnapshot(fn)
})
// saved snapshot
exports['snapshots error 1'] = {
  "name": "Error",
  "message": "A test error"
}
// extra properties
it('snapshots extra properties by name', () => {
  const fn = () => {
    const e = new Error('A test error')
    e.foo = 42
    e.bar = 'some bar prop'
    throw e
  }
  errorSnapshot(fn, 'foo', 'bar')
})
// saved snapshot
exports['snapshots extra properties by name 1'] = {
  "name": "Error",
  "message": "A test error",
  "foo": 42,
  "bar": "some bar prop"
}

For more examples, see test file and the snapshots

Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2017 Gleb Bahmutov <gleb.bahmutov@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Current Tags

  • 2.0.0                                ...           latest (3 years ago)

2 Versions

  • 2.0.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (3)
Dev Dependencies (15)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |