-
Notifications
You must be signed in to change notification settings - Fork 354
Add ability to throw error instead of warning in tests #28
Comments
This would be really useful for anyone using create-react-app, as PropType errors would appear in the new error overlay |
Related to previous conversation here. |
I just published a #54 on this repo also has an interesting approach, using a callback instead. |
I'm trying to use
|
@artin-phares I've created typed-props library which has the same interface and produce an array of validation report issues. |
There is a workaround for throwing an error on warnings. We can use PropTypes.checkPropTypes(spec, values, 'prop', name, () => {
process.nextTick(() => {
throw new Error('Check has failed');
});
}); This should work even in browsers with |
@a-ignatov-parc, won't |
|
@a-ignatov-parc Thrown error will loose the stack in that way and it will not be captured in the place where it was thrown. Also printing of PropTypes' message will not be stopped. |
I am not sure what is the current state of this issue and the project. I am using a small wrapper to throw error like so:
|
My solution with Jest: jest.mock('prop-types/checkPropTypes', () => {
return (...args) => {
const checkPropTypes = jest.requireActual('prop-types/checkPropTypes');
const originalConsoleError = console.error;
console.error = function(...args) {
process.nextTick(() => {
throw new Error(...args);
});
};
const result = checkPropTypes(...args);
console.error = originalConsoleError;
return result;
};
}); |
@mmiszy Why are you using |
I don't get what's the difference between @sarbbottam's and @mmiszy's workarounds. |
@vince1995 the second one throws the error in nextTick, which means you don't get a useful stack trace that tells you where |
@vince1995 @ljharb quite the opposite, actually. Throwing the error without After adding Error message in Jest without
Error message in Jest with
|
ah, you’re right, I’d forgotten that react 16 changed that behavior around error catching. |
Do you have the latest React? |
Yes. This is my setup-tests.js file
And my tests:
EDIT: I've created a new project. Still fails with nextTick. Without it works. Maybe my node version is different to yours? Mine is v12.13.1 |
For anyone else trying to captures prop-type errors in unit tests with Jest - this beforeEach(() => {
jest.spyOn(console, 'error')
jest.spyOn(console, 'warn')
})
afterEach(() => {
/* eslint-disable no-console,jest/no-standalone-expect */
expect(console.error).not.toBeCalled()
expect(console.warn).not.toBeCalled()
}) This breaks when any test calls It also requires your app to not call |
@jzaefferer The latter does not work for me. Is there any advance or solution is there alternatives for this pain? |
We recently upgraded Jest to latest and had to drop this. I don't know of any alternatives :/ |
Neither example worked for me. So create a new npm package to solve this problem and it is compatible with the latest version of Jest (27). |
fwiw, there's nothing specific to jest in there - that technique should be applied for every testing framework. |
@ljharb I don't have the same experience in other testing frameworks, I don't know how they work. If anyone wants to help you are welcome and we can rename the repo and the npm package. |
I'm currently using Jest and my solution was to mock the console, then every warning or error will throw an error when running the tests. Hope this can be useful for someone else.
|
In tests it would be nice to throw an error if the property isn't of its expected type.
It would be nice if this could be scoped to our own components and not 3rd party ones using 3rd party components, but thats not necesary.
People are already turning
console.warning
calls to throw errors, so this would be a nice upgrade for them.The text was updated successfully, but these errors were encountered: