123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- exports.setMatchers = exports.getMatchers = exports.setState = exports.getState = exports.INTERNAL_MATCHER_FLAG = undefined;
- var _asymmetric_matchers = require('./asymmetric_matchers');
- function _toConsumableArray(arr) {
- if (Array.isArray(arr)) {
- for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++)
- arr2[i] = arr[i];
- return arr2;
- } else {
- return Array.from(arr);
- }
- }
- /**
- * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
- *
- * This source code is licensed under the MIT license found in the
- * LICENSE file in the root directory of this source tree.
- *
- *
- */
- // Global matchers object holds the list of available matchers and
- // the state, that can hold matcher specific values that change over time.
- const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object');
- // Notes a built-in/internal Jest matcher.
- // Jest may override the stack trace of Errors thrown by internal matchers.
- const INTERNAL_MATCHER_FLAG = (exports.INTERNAL_MATCHER_FLAG = Symbol.for(
- '$$jest-internal-matcher'
- ));
- if (!global[JEST_MATCHERS_OBJECT]) {
- Object.defineProperty(global, JEST_MATCHERS_OBJECT, {
- value: {
- matchers: Object.create(null),
- state: {
- assertionCalls: 0,
- expectedAssertionsNumber: null,
- isExpectingAssertions: false,
- suppressedErrors: [] // errors that are not thrown immediately.
- }
- }
- });
- }
- const getState = (exports.getState = () => global[JEST_MATCHERS_OBJECT].state);
- const setState = (exports.setState = state => {
- Object.assign(global[JEST_MATCHERS_OBJECT].state, state);
- });
- const getMatchers = (exports.getMatchers = () =>
- global[JEST_MATCHERS_OBJECT].matchers);
- const setMatchers = (exports.setMatchers = (matchers, isInternal, expect) => {
- Object.keys(matchers).forEach(key => {
- const matcher = matchers[key];
- Object.defineProperty(matcher, INTERNAL_MATCHER_FLAG, {
- value: isInternal
- });
- if (!isInternal) {
- // expect is defined
- class CustomMatcher extends _asymmetric_matchers.AsymmetricMatcher {
- constructor() {
- let inverse =
- arguments.length > 0 && arguments[0] !== undefined
- ? arguments[0]
- : false;
- super();
- this.inverse = inverse;
- for (
- var _len = arguments.length,
- sample = Array(_len > 1 ? _len - 1 : 0),
- _key = 1;
- _key < _len;
- _key++
- ) {
- sample[_key - 1] = arguments[_key];
- }
- this.sample = sample;
- }
- asymmetricMatch(other) {
- var _ref = matcher.apply(
- undefined,
- [other].concat(_toConsumableArray(this.sample))
- );
- const pass = _ref.pass;
- return this.inverse ? !pass : pass;
- }
- toString() {
- return `${this.inverse ? 'not.' : ''}${key}`;
- }
- getExpectedType() {
- return 'any';
- }
- toAsymmetricMatcher() {
- return `${this.toString()}<${this.sample.join(', ')}>`;
- }
- }
- expect[key] = function() {
- for (
- var _len2 = arguments.length, sample = Array(_len2), _key2 = 0;
- _key2 < _len2;
- _key2++
- ) {
- sample[_key2] = arguments[_key2];
- }
- return new (Function.prototype.bind.apply(
- CustomMatcher,
- [null].concat([false], sample)
- ))();
- };
- if (!expect.not) {
- expect.not = {};
- }
- expect.not[key] = function() {
- for (
- var _len3 = arguments.length, sample = Array(_len3), _key3 = 0;
- _key3 < _len3;
- _key3++
- ) {
- sample[_key3] = arguments[_key3];
- }
- return new (Function.prototype.bind.apply(
- CustomMatcher,
- [null].concat([true], sample)
- ))();
- };
- }
- });
- Object.assign(global[JEST_MATCHERS_OBJECT].matchers, matchers);
- });
|