/**
 * Copyright (c) Facebook, Inc. and its affiliates. 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.
 *
 */ import { AsymmetricMatcher } from './asymmetricMatchers' ; import type { Expect , MatcherState , MatchersObject , SyncExpectationResult , } from './types' ; const JEST_MATCHERS_OBJECT = Symbol . for ( '$$jest-matchers-object' ) ; export const INTERNAL_MATCHER_FLAG = Symbol . for ( '$$jest-internal-matcher' ) ; if ( ! global . hasOwnProperty ( JEST_MATCHERS_OBJECT ) ) { const defaultState = { assertionCalls : 0 , expectedAssertionsNumber : null , isExpectingAssertions : false , suppressedErrors : [ ] , } ; Object . defineProperty ( global , JEST_MATCHERS_OBJECT , { value : { matchers : Object . create ( null ) , state : defaultState , } , } ) ; } export const getState = < State extends MatcherState = MatcherState > ( ) : State => ( global as any ) [ JEST_MATCHERS_OBJECT ] . state ; export const setState = < State extends MatcherState = MatcherState > ( state ) : void => { Object . assign ( ( global as any ) [ JEST_MATCHERS_OBJECT ] . state , state ) ; } ; export const getMatchers = < State extends MatcherState = MatcherState > ( ) : MatchersObject < State > => ( global as any ) [ JEST_MATCHERS_OBJECT ] . matchers ; export const setMatchers = < State extends MatcherState = MatcherState > ( matchers , isInternal , expect ) : void => { Object . keys ( matchers ) . forEach ( ( key ) => { const matcher = matchers [ key ] ; Object . defineProperty ( matcher , INTERNAL_MATCHER_FLAG , { value : isInternal , } ) ; if ( ! isInternal ) { class CustomMatcher extends AsymmetricMatcher < [ unknown , ... Array < unknown > ] , State > { constructor ( inverse = false , ... sample ) { super ( sample , inverse ) ; } asymmetricMatch ( other ) { const { pass } = matcher . call ( this . getMatcherContext ( ) , other , ... this . sample ) as SyncExpectationResult ; return this . inverse ? ! pass : pass ; } toString ( ) { return `${ this . inverse ? 'not.' : '' }${ key }` ; } getExpectedType ( ) { return 'any' ; } toAsymmetricMatcher ( ) { return `${ this . toString ( ) }<${ this . sample . map ( String ) . join ( ', ' ) }>` ; } } Object . defineProperty ( expect , key , { configurable : true , enumerable : true , value : ( ... sample ) => new CustomMatcher ( false , ... sample ) , writable : true , } ) ; Object . defineProperty ( expect . not , key , { configurable : true , enumerable : true , value : ( ... sample ) => new CustomMatcher ( true , ... sample ) , writable : true , } ) ; } } ) ; Object . assign ( ( global as any ) [ JEST_MATCHERS_OBJECT ] . matchers , matchers ) ; } ;