toEqual.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toEqual = toEqual;
  6. var _util = require("../util");
  7. var _matcherHint = require("./matcherHint");
  8. var _globals = require("../common/globals");
  9. /**
  10. * Copyright Microsoft Corporation. All rights reserved.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. // Omit colon and one or more spaces, so can call getLabelPrinter.
  25. const EXPECTED_LABEL = 'Expected';
  26. const RECEIVED_LABEL = 'Received';
  27. // The optional property of matcher context is true if undefined.
  28. const isExpand = expand => expand !== false;
  29. async function toEqual(matcherName, receiver, receiverType, query, expected, options = {}) {
  30. (0, _util.expectTypes)(receiver, [receiverType], matcherName);
  31. const matcherOptions = {
  32. comment: options.contains ? '' : 'deep equality',
  33. isNot: this.isNot,
  34. promise: this.promise
  35. };
  36. const timeout = (0, _globals.currentExpectTimeout)(options);
  37. const {
  38. matches: pass,
  39. received,
  40. log,
  41. timedOut
  42. } = await query(!!this.isNot, timeout);
  43. const message = pass ? () => (0, _matcherHint.matcherHint)(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined) + `Expected: not ${this.utils.printExpected(expected)}\n` + `Received: ${this.utils.printReceived(received)}` + (0, _util.callLogText)(log) : () => (0, _matcherHint.matcherHint)(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined) + this.utils.printDiffOrStringify(expected, received, EXPECTED_LABEL, RECEIVED_LABEL, isExpand(this.expand)) + (0, _util.callLogText)(log);
  44. // Passing the actual and expected objects so that a custom reporter
  45. // could access them, for example in order to display a custom visual diff,
  46. // or create a different error message
  47. return {
  48. actual: received,
  49. expected,
  50. message,
  51. name: matcherName,
  52. pass,
  53. log,
  54. timeout: timedOut ? timeout : undefined
  55. };
  56. }