-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove warnings when rendering react-native components #831
Comments
I think you can not use |
With It would be great –even if a workaround– to remove such logs. |
Even with
Why would I need edit: even after installing react-dom it gives the same warning |
#1007 will be the solution to this. |
I also had the same issue with I cloned https://github.com/Microsoft/TypeScript-React-Native-Starter and upgraded all dev packages. I then did the setup for React 16: import * as enzyme from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';
enzyme.configure({
adapter: new Adapter(),
}); And ran import * as React from 'react';
import { Text } from 'react-native';
import { shallow } from 'enzyme';
import Hello from '../Hello';
it('renders correctly with defaults', () => {
const hello = shallow(<Hello name="World" />);
expect(hello.find(Text).render().text()).toEqual("Hello World!");
}); Which is testing this simple React Native component using only very basic React Native components: return (
<View style={styles.root}>
<Text style={styles.greeting}>
Hello {name + getExclamationMarks(enthusiasmLevel)}
</Text>
<View style={styles.buttons}>
<View style={styles.button}>
<Button title="-" onPress={onDecrement || (() => {})} color='red' />
</View>
<View style={styles.button}>
<Button title="+" onPress={onIncrement || (() => {})} color='blue' />
</View>
</View>
</View>
); And I got the following warnings:
|
React 16 is for React web - to properly use enzyme with react-native, we'd need an |
@ljharb I'm interested in what would be required to make an Also, is there any effort to write the adapter already? I'd be curious of any information/insight you could give into seeing how a react-native adapter would work. |
The best right now is to work off the base adapters. I'm not aware of any current effort to write a react-native adapter. |
Honestly F** it I did this for now to stop all the red in my console. I will watch #1436 describe('mounting', () => {
const origConsole = console.error;
beforeEach(() => {
console.error = () => {};
});
afterEach(() => {
console.error = origConsole;
});
it ......
mount....
}); |
@rmnegatives that will silence propType warnings, which should be failing your tests. |
@ljharb yes it will, it is up to the developer to do due diligence check the warnings first, then fix the propType or any none 'DOM' type error. After things look good you can put in this temporary fix. |
This should be fixed... |
Did anyone figure out how to do @rmnegatives hack with the new Jest 22 globalSetup hook so it doesn't have to be repeated in every file? For example, I tried to put this in the global setup file and it runs, but doesn't suppress the issues.
|
You'd need a react native adapter to use enzyme with React Native. Closing this in favor of #1436. |
In
See full |
I know this issue is closed, but my searching landed me here, so I'll post this here for others who might also end up here. I took this a bit further in my Hopefully, this is helpful until a more concrete and official solution is available. Also available as a Gist for sharing around. 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() }); |
A simple workaround to includes certain errors that we would like to log.
|
Same issue. Any solution ? |
Hi, I have been struggling to properly test
react-native
components, so I resigned to mount them and I am currently testing them only on shallow or render methods. I opened a stackoverflow question in case someone can help in this matter.However on every test I get a long list of warnings (printed with
console.error
, but not crashing the test), about the properties that are expected / received:I would like to remove such warnings or have a resource I can follow to properly test react-native components on Jest
The text was updated successfully, but these errors were encountered: