index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. }
  21. exports.expectFullStackWithoutStackFramesToEqual = function (error, expected) {
  22. // But the stack contains all of the errors and tags.
  23. const fullStack = OError.getFullStack(error)
  24. const fullStackWithoutFrames = fullStack
  25. .split('\n')
  26. .filter((line) => !/^\s+at\s/.test(line))
  27. expect(fullStackWithoutFrames).to.deep.equal(expected)
  28. }