You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. const errcode = require('../index');
  3. const expect = require('expect.js');
  4. describe('errcode', () => {
  5. describe('string as first argument', () => {
  6. it('should throw an error', () => {
  7. expect(() => { errcode('my message'); }).to.throwError((err) => {
  8. expect(err).to.be.a(TypeError);
  9. });
  10. });
  11. });
  12. describe('error as first argument', () => {
  13. it('should accept an error and do nothing', () => {
  14. const myErr = new Error('my message');
  15. const err = errcode(myErr);
  16. expect(err).to.be(myErr);
  17. expect(err.hasOwnProperty(err.code)).to.be(false);
  18. });
  19. it('should accept an error and add a code', () => {
  20. const myErr = new Error('my message');
  21. const err = errcode(myErr, 'ESOME');
  22. expect(err).to.be(myErr);
  23. expect(err.code).to.be('ESOME');
  24. });
  25. it('should accept an error object and add code & properties', () => {
  26. const myErr = new Error('my message');
  27. const err = errcode(myErr, 'ESOME', { foo: 'bar', bar: 'foo' });
  28. expect(err).to.be.an(Error);
  29. expect(err.code).to.be('ESOME');
  30. expect(err.foo).to.be('bar');
  31. expect(err.bar).to.be('foo');
  32. });
  33. it('should create an error object without code but with properties', () => {
  34. const myErr = new Error('my message');
  35. const err = errcode(myErr, { foo: 'bar', bar: 'foo' });
  36. expect(err).to.be.an(Error);
  37. expect(err.code).to.be(undefined);
  38. expect(err.foo).to.be('bar');
  39. expect(err.bar).to.be('foo');
  40. });
  41. it('should set a non-writable field', () => {
  42. const myErr = new Error('my message');
  43. Object.defineProperty(myErr, 'code', {
  44. value: 'derp',
  45. writable: false,
  46. });
  47. const err = errcode(myErr, 'ERR_WAT');
  48. expect(err).to.be.an(Error);
  49. expect(err.stack).to.equal(myErr.stack);
  50. expect(err.code).to.be('ERR_WAT');
  51. });
  52. it('should add a code to frozen object', () => {
  53. const myErr = new Error('my message');
  54. const err = errcode(Object.freeze(myErr), 'ERR_WAT');
  55. expect(err).to.be.an(Error);
  56. expect(err.stack).to.equal(myErr.stack);
  57. expect(err.code).to.be('ERR_WAT');
  58. });
  59. it('should to set a field that throws at assignment time', () => {
  60. const myErr = new Error('my message');
  61. Object.defineProperty(myErr, 'code', {
  62. enumerable: true,
  63. set() {
  64. throw new Error('Nope!');
  65. },
  66. get() {
  67. return 'derp';
  68. },
  69. });
  70. const err = errcode(myErr, 'ERR_WAT');
  71. expect(err).to.be.an(Error);
  72. expect(err.stack).to.equal(myErr.stack);
  73. expect(err.code).to.be('ERR_WAT');
  74. });
  75. it('should retain error type', () => {
  76. const myErr = new TypeError('my message');
  77. Object.defineProperty(myErr, 'code', {
  78. value: 'derp',
  79. writable: false,
  80. });
  81. const err = errcode(myErr, 'ERR_WAT');
  82. expect(err).to.be.a(TypeError);
  83. expect(err.stack).to.equal(myErr.stack);
  84. expect(err.code).to.be('ERR_WAT');
  85. });
  86. it('should add a code to a class that extends Error', () => {
  87. class CustomError extends Error {
  88. set code(val) {
  89. throw new Error('Nope!');
  90. }
  91. }
  92. const myErr = new CustomError('my message');
  93. Object.defineProperty(myErr, 'code', {
  94. value: 'derp',
  95. writable: false,
  96. configurable: false,
  97. });
  98. const err = errcode(myErr, 'ERR_WAT');
  99. expect(err).to.be.a(CustomError);
  100. expect(err.stack).to.equal(myErr.stack);
  101. expect(err.code).to.be('ERR_WAT');
  102. // original prototype chain should be intact
  103. expect(() => {
  104. const otherErr = new CustomError('my message');
  105. otherErr.code = 'derp';
  106. }).to.throwError();
  107. });
  108. it('should support errors that are not Errors', () => {
  109. const err = errcode({
  110. message: 'Oh noes!',
  111. }, 'ERR_WAT');
  112. expect(err.message).to.be('Oh noes!');
  113. expect(err.code).to.be('ERR_WAT');
  114. });
  115. });
  116. describe('falsy first arguments', () => {
  117. it('should not allow passing null as the first argument', () => {
  118. expect(() => { errcode(null); }).to.throwError((err) => {
  119. expect(err).to.be.a(TypeError);
  120. });
  121. });
  122. it('should not allow passing undefined as the first argument', () => {
  123. expect(() => { errcode(undefined); }).to.throwError((err) => {
  124. expect(err).to.be.a(TypeError);
  125. });
  126. });
  127. });
  128. });