utils.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. let AssertError;
  2. if (Error.captureStackTrace) {
  3. AssertError = function AssertError(message, caller) {
  4. Error.prototype.constructor.call(this, message);
  5. this.message = message;
  6. if (Error.captureStackTrace) {
  7. Error.captureStackTrace(this, caller || AssertError);
  8. }
  9. };
  10. AssertError.prototype = new Error();
  11. } else {
  12. AssertError = Error;
  13. }
  14. /**
  15. * @deprecated Use chai's expect-style API instead (`expect(actualValue).to.equal(expectedValue)`)
  16. * @see https://www.chaijs.com/api/bdd/
  17. */
  18. export function equals(a, b, msg) {
  19. if (a !== b) {
  20. throw new AssertError(
  21. "'" + a + "' should === '" + b + "'" + (msg ? ': ' + msg : ''),
  22. equals
  23. );
  24. }
  25. };
  26. /**
  27. * @deprecated Use chai's expect-style API instead (`expect(actualValue).to.equal(expectedValue)`)
  28. * @see https://www.chaijs.com/api/bdd/#method_throw
  29. */
  30. export function shouldThrow(callback, type, msg) {
  31. let failed;
  32. try {
  33. callback();
  34. failed = true;
  35. } catch (caught) {
  36. if (type && !(caught instanceof type)) {
  37. throw new AssertError('Type failure: ' + caught);
  38. }
  39. if (
  40. msg &&
  41. !(msg.test ? msg.test(caught.message) : msg === caught.message)
  42. ) {
  43. throw new AssertError(
  44. 'Throw mismatch: Expected ' +
  45. caught.message +
  46. ' to match ' +
  47. msg +
  48. '\n\n' +
  49. caught.stack,
  50. shouldThrow
  51. );
  52. }
  53. }
  54. if (failed) {
  55. throw new AssertError('It failed to throw', shouldThrow);
  56. }
  57. };