index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const { expect } = require('chai')
  2. const OError = require('../..')
  3. exports.expectError = function OErrorExpectError(e, expected) {
  4. expect(
  5. e.name,
  6. "error should set the name property to the error's name"
  7. ).to.equal(expected.name)
  8. expect(
  9. e instanceof expected.klass,
  10. 'error should be an instance of the error type'
  11. ).to.be.true
  12. expect(
  13. e instanceof Error,
  14. 'error should be an instance of the built-in Error type'
  15. ).to.be.true
  16. expect(
  17. require('util').types.isNativeError(e),
  18. 'error should be recognised by util.types.isNativeError'
  19. ).to.be.true
  20. expect(e.stack, 'error should have a stack trace').to.be.truthy
  21. expect(
  22. e.toString(),
  23. 'toString should return the default error message formatting'
  24. ).to.equal(expected.message)
  25. expect(
  26. e.stack.split('\n')[0],
  27. 'stack should start with the default error message formatting'
  28. ).to.match(new RegExp(`^${expected.name}:`))
  29. expect(
  30. e.stack.split('\n')[1],
  31. 'first stack frame should be the function where the error was thrown'
  32. ).to.match(expected.firstFrameRx)
  33. }
  34. exports.expectFullStackWithoutStackFramesToEqual = function (error, expected) {
  35. const fullStack = OError.getFullStack(error)
  36. const fullStackWithoutFrames = fullStack
  37. .split('\n')
  38. .filter((line) => !/^\s+at\s/.test(line))
  39. expect(
  40. fullStackWithoutFrames,
  41. 'full stack without frames should equal'
  42. ).to.deep.equal(expected)
  43. }