Skip to content

Commit

Permalink
Add some tests for error swallowing
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Apr 19, 2016
1 parent 8830842 commit c019762
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/createClassProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ function proxyClass(InitialComponent) {
try {
return component.apply(context, params);
} catch (err) {
// Native ES6 class instantiation
const instance = new component(...params);

Object.keys(instance).forEach(key => {
if (RESERVED_STATICS.indexOf(key) > -1) {
return;
Expand Down
15 changes: 15 additions & 0 deletions test/consistency.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ function createModernFixtures() {
}

class Anon extends React.Component {
constructor(props) {
super(props);
throw new Error('Oops.');
}

render() {
return <div>Anon</div>;
}
Expand Down Expand Up @@ -86,6 +91,10 @@ function createClassicFixtures() {
});

const Anon = React.createClass({
getInitialState() {
throw new Error('Oops.');
},

render() {
return <div>Anon</div>;
}
Expand Down Expand Up @@ -269,6 +278,12 @@ describe('consistency', () => {
});
expect(doNothingBeforeItWasDeleted.toString()).toEqual('<method was deleted>');
});

it('does not swallow constructor errors', () => {
let proxy = createProxy(Anon);
const Proxy = proxy.get();
expect(() => renderer.render(<Proxy />)).toThrow('Oops');
});
}

describe('classic', () => {
Expand Down
27 changes: 25 additions & 2 deletions test/pure-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ function createPureFixtures() {
return <div {...props}>Foo</div>;
}

function Qux() {
throw new Error('Oops.');
}

const Quy = () => {
throw new Error('Ouch.');
}

return {
Bar,
Baz,
Foo
Foo,
Qux,
Quy
};
}

Expand All @@ -41,12 +51,16 @@ describe('pure component', () => {
let Bar;
let Baz;
let Foo;
let Qux;
let Quy;

beforeEach(() => {
({
Bar,
Baz,
Foo
Foo,
Qux,
Quy
} = createPureFixtures());
});

Expand All @@ -64,5 +78,14 @@ describe('pure component', () => {
renderer.render(<Proxy />);
expect(renderer.getRenderOutput().props.children).toEqual('Foo');
});

it('does not swallow errors', () => {
const proxy = createProxy(Qux);
const Proxy = proxy.get();
expect(() => renderer.render(<Proxy />)).toThrow('Oops.');

proxy.update(Quy);
expect(() => renderer.render(<Proxy />)).toThrow('Ouch.');
});
});
});

0 comments on commit c019762

Please # to comment.