Skip to content

Instantly share code, notes, and snippets.

@VannaDii
Created October 16, 2019 19:19
Show Gist options
  • Save VannaDii/eec75de839e7bd05d58aa982eefa175e to your computer and use it in GitHub Desktop.
Save VannaDii/eec75de839e7bd05d58aa982eefa175e to your computer and use it in GitHub Desktop.
TypeScript + React-Native + Redux + Jest + Enzyme + Enzyme Adapter
import 'react-native';
import 'jest-enzyme';
import Adapter from 'enzyme-adapter-react-16';
import Enzyme from 'enzyme';
import { global } from 'core-js';
import { JSDOM } from 'jsdom';
const jsdom = new JSDOM();
const { window } = jsdom;
function copyProps(src: any, target: any) {
Object.defineProperties(target, {
...Object.getOwnPropertyDescriptors(src),
...Object.getOwnPropertyDescriptors(target),
});
}
global.window = window;
global.document = window.document;
global.navigator = {
userAgent: 'node.js',
};
copyProps(window, global);
function _getCallerFile() {
const originalFunc = Error.prepareStackTrace;
let callerFile: string = 'unknown';
try {
const err = new Error();
let currentFile: string;
let stack: NodeJS.CallSite[];
let stackRecord: NodeJS.CallSite | undefined;
Error.prepareStackTrace = function(err, stack) {
return stack;
};
stack = <NodeJS.CallSite[]>(<unknown>err.stack);
stackRecord = <NodeJS.CallSite>stack.shift();
currentFile = stackRecord.getFileName() || '';
while (stack.length) {
stackRecord = <NodeJS.CallSite>stack.shift();
callerFile = stackRecord.getFileName() || '';
if (currentFile !== callerFile) break;
}
} catch (e) {
} finally {
Error.prepareStackTrace = originalFunc;
}
return callerFile;
}
function filteredConsole(
original: (message?: any, ...optionalParams: any[]) => void,
message?: any,
...optionalParams: any[]
) {
const callerFile = _getCallerFile();
const blockPattern = /.*(react-dom.development.js|module is not correctly linked)/i;
if (!blockPattern.test(callerFile) && !blockPattern.test(message)) {
original(message, ...optionalParams);
}
}
const originalConsoleWarn = console.warn;
const originalConsoleError = console.error;
console.error = (message?: any, ...optionalParams: any[]) =>
filteredConsole(originalConsoleError, message, ...optionalParams);
console.warn = (message?: any, ...optionalParams: any[]) =>
filteredConsole(originalConsoleWarn, message, ...optionalParams);
Enzyme.configure({ adapter: new Adapter() });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment