lundi 18 février 2019

How can I make Jest test for a thrown error silently

I’m writing a test to assert that a component throws an error if provided one prop but not another.

The test itself passes, but the console still complains about an uncaught error and prints the entire stack trace. Is there a way I can get Jest to stop printing this information, as it pollutes the test runner and makes it look like something has failed.

For reference, this is my test:

it("throws an error if showCancel is set to true, but no onCancel method is provided", () => {
    // Assert that an error is thrown
    expect(() => mount(<DropTarget showCancel={ true }/>)).toThrowError("If `showCancel` is true, you must provide an `onCancel` method");
}); 

The error itself is thrown here:

if(props.showCancel && !props.onCancel) {
    throw new Error("If `showCancel` is true, you must provide an `onCancel` method");
}

Aucun commentaire:

Enregistrer un commentaire