storybook/scripts/jest.init.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

import 'jest-enzyme/lib/index';
// setup file
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import registerRequireContextHook from 'babel-plugin-require-context-hook/register';
registerRequireContextHook();
// mock console.info calls for cleaner test execution
2017-11-21 04:18:36 +03:00
global.console.info = jest.fn().mockImplementation(() => {});
// mock local storage calls
const localStorageMock = {
getItem: jest.fn().mockName('getItem'),
setItem: jest.fn().mockName('setItem'),
clear: jest.fn().mockName('clear'),
};
global.localStorage = localStorageMock;
configure({ adapter: new Adapter() });
/* 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.
*/
2019-01-03 13:13:59 +01:00
const ignoreList = [
error => error.message.includes('":first-child" is potentially unsafe'),
error => error.message.includes('Failed prop type') && error.stack.includes('storyshots'),
];
const throwMessage = (type, message) => {
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;
}
};
const throwWarning = message => throwMessage('warn: ', message);
const throwError = message => throwMessage('error: ', message);
global.console.error = throwError;
global.console.warn = throwWarning;