index.js 975 B

1234567891011121314151617181920212223242526272829
  1. const { expect } = require('chai')
  2. const OError = require('../..')
  3. exports.expectError = function OErrorExpectError(e, expected) {
  4. // should set the name to the error's name
  5. expect(e.name).to.equal(expected.name)
  6. // should be an instance of the error type
  7. expect(e instanceof expected.klass).to.be.true
  8. // should be an instance of the built-in Error type
  9. expect(e instanceof Error).to.be.true
  10. // should be recognised by util.isError
  11. expect(require('util').types.isNativeError(e)).to.be.true
  12. // should have a stack trace
  13. expect(e.stack).to.be.truthy
  14. // toString should return the default error message formatting
  15. expect(e.toString()).to.equal(expected.message)
  16. // stack should start with the default error message formatting
  17. expect(e.stack.split('\n')[0]).to.match(new RegExp(`^${expected.name}:`))
  18. // first stack frame should be the function where the error was thrown
  19. expect(e.stack.split('\n')[1]).to.match(expected.firstFrameRx)
  20. }