2017-09-28 14:25:49 +02:00
import 'jest-enzyme/lib/index' ;
2020-11-02 23:29:12 +01:00
import '@testing-library/jest-dom' ;
2017-09-28 14:25:49 +02:00
// setup file
import { configure } from 'enzyme' ;
2020-11-02 23:29:12 +01:00
// @ts-ignore
2017-09-28 14:25:49 +02:00
import Adapter from 'enzyme-adapter-react-16' ;
2020-11-02 23:29:12 +01:00
// @ts-ignore
2019-05-06 12:44:58 +02:00
import regeneratorRuntime from 'regenerator-runtime' ;
2020-11-02 23:29:12 +01:00
// @ts-ignore
2018-06-12 21:50:13 +03:00
import registerRequireContextHook from 'babel-plugin-require-context-hook/register' ;
registerRequireContextHook ( ) ;
2020-11-02 23:29:12 +01:00
jest . mock ( 'util-deprecate' , ( ) = > ( fn : any ) = > fn ) ;
2019-03-18 21:28:00 +01:00
2017-11-19 18:22:46 -05:00
// mock console.info calls for cleaner test execution
2017-11-21 04:18:36 +03:00
global . console . info = jest . fn ( ) . mockImplementation ( ( ) = > { } ) ;
2019-03-18 21:28:00 +01:00
global . console . debug = jest . fn ( ) . mockImplementation ( ( ) = > { } ) ;
2017-11-19 18:22:46 -05:00
2018-11-11 22:43:05 -08:00
// mock local storage calls
const localStorageMock = {
2018-11-11 23:11:57 -08:00
getItem : jest.fn ( ) . mockName ( 'getItem' ) ,
setItem : jest.fn ( ) . mockName ( 'setItem' ) ,
clear : jest.fn ( ) . mockName ( 'clear' ) ,
2018-11-11 22:43:05 -08:00
} ;
2020-11-02 23:29:12 +01:00
// @ts-ignore
2018-11-11 22:43:05 -08:00
global . localStorage = localStorageMock ;
2020-11-02 23:29:12 +01:00
// @ts-ignore
2019-05-06 12:44:58 +02:00
global . regeneratorRuntime = regeneratorRuntime ;
2018-11-11 22:43:05 -08:00
2017-09-28 14:25:49 +02:00
configure ( { adapter : new Adapter ( ) } ) ;
2017-11-12 13:40:07 -05:00
/ * F a i l t e s t s o n P r o p T y p e w a r n i n g s
2019-01-03 13:13:59 +01:00
This allows us to throw an error in tests environments when there are prop - type warnings .
This should keep the tests free of warnings going forward .
2017-11-12 13:40:07 -05:00
* /
2019-01-03 13:13:59 +01:00
const ignoreList = [
2020-11-02 23:29:12 +01:00
( error : any ) = > error . message . includes ( '":nth-child" is potentially unsafe' ) ,
( error : any ) = > error . message . includes ( '":first-child" is potentially unsafe' ) ,
( error : any ) = > error . message . includes ( 'Failed prop type' ) && error . stack . includes ( 'storyshots' ) ,
( error : any ) = >
2019-09-24 13:11:17 +02:00
error . message . includes ( 'react-async-component-lifecycle-hooks' ) &&
error . stack . includes ( 'addons/knobs/src/components/__tests__/Options.js' ) ,
2018-12-21 17:10:38 +01:00
] ;
2020-11-02 23:29:12 +01:00
const throwMessage = ( type : any , message : any ) = > {
2019-01-03 20:56:48 +01:00
const error = new Error ( ` ${ type } ${ message } ` ) ;
2019-01-03 13:13:59 +01:00
if ( ! ignoreList . reduce ( ( acc , item ) = > acc || item ( error ) , false ) ) {
throw error ;
2018-12-21 17:10:38 +01:00
}
2017-11-12 13:40:07 -05:00
} ;
2020-11-02 23:29:12 +01:00
const throwWarning = ( message : any ) = > throwMessage ( 'warn: ' , message ) ;
const throwError = ( message : any ) = > throwMessage ( 'error: ' , message ) ;
2017-11-12 13:40:07 -05:00
global . console . error = throwError ;
2019-01-03 20:56:48 +01:00
global . console . warn = throwWarning ;
2019-10-02 13:32:37 +02:00
// Mock for matchMedia since it's not yet implemented in JSDOM (https://jestjs.io/docs/en/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom)
2020-03-27 20:04:50 +01:00
global . window . matchMedia = jest . fn ( ) . mockImplementation ( ( query ) = > {
2019-10-02 13:32:37 +02:00
return {
matches : false ,
media : query ,
onchange : null ,
addListener : jest.fn ( ) , // deprecated
removeListener : jest.fn ( ) , // deprecated
addEventListener : jest.fn ( ) ,
removeEventListener : jest.fn ( ) ,
dispatchEvent : jest.fn ( ) ,
} ;
} ) ;