2020-11-02 23:29:12 +01:00
|
|
|
import '@testing-library/jest-dom';
|
2017-09-28 14:25:49 +02:00
|
|
|
|
|
|
|
// setup file
|
2021-11-11 19:35:47 +11:00
|
|
|
import registerRequireContextHook from '@storybook/babel-plugin-require-context-hook/register';
|
2018-06-12 21:50:13 +03:00
|
|
|
|
|
|
|
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
|
|
|
};
|
2023-03-06 08:24:35 +01:00
|
|
|
|
|
|
|
Object.defineProperty(global, 'localStorage', { value: localStorageMock, writable: true });
|
2018-11-11 22:43:05 -08:00
|
|
|
|
2017-11-12 13:40:07 -05:00
|
|
|
/* Fail tests on PropType warnings
|
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'),
|
2023-04-26 11:19:29 +02:00
|
|
|
(error: any) => error.message.match(/Browserslist: .* is outdated. Please run:/),
|
2020-11-02 23:29:12 +01:00
|
|
|
(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'),
|
2022-10-11 00:34:29 +02:00
|
|
|
// Storyshots blows up if your project includes a (non stories.) mdx file.
|
2022-06-16 16:00:18 +10:00
|
|
|
(error: any) => error.message.match(/Unexpected error while loading .*(?<!stories)\.mdx/),
|
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;
|