Skip to content

Commit

Permalink
Add helpful test failures when incorrect require was used
Browse files Browse the repository at this point in the history
When not requiring the jest specific unexpected-react, or using with
the wrong renderer, fail the test with a message show how to fix it,
rather than failing with "unknown assertion"
  • Loading branch information
bruderstein committed Feb 26, 2017
1 parent 07b8ab0 commit 417dd2b
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 7 deletions.
22 changes: 21 additions & 1 deletion src/test-renderer-jest.js
Expand Up @@ -33,7 +33,27 @@ module.exports = {
jestSnapshotAssertions.installInto(expect);
snapshotFunctionType.installInto(expect);
snapshotFunctionAssertions.installInto(expect);

expect.addType({
name: 'RawReactTestRendererJson',
base: 'object',
identify: function (value) {
return value && typeof value === 'object' && value.props && value.children && value.type;
}
});

expect.addAssertion('<RawReactTestRendererJson> to match snapshot', function (expect, subject) {
expect.errorMode = 'bubble';
expect.fail({
message: function (output) {
return output.text('To assert snapshots, use the testRenderer directly, not the result of `.toJSON()`')
.nl().i()
.text('e.g.')
.nl().i()
.text(' const testRenderer = ReactTestRenderer.create(<MyComponent />);').nl().i()
.text(' expect(testRenderer, \'to match snapshot\');');
}
});
});
},

clearAll() {
Expand Down
50 changes: 45 additions & 5 deletions src/test-renderer.js
Expand Up @@ -5,11 +5,51 @@ module.exports = {
name: 'unexpected-react-test-renderer',

installInto(expect) {

expect.installPlugin(require('magicpen-prism'));

types.installInto(expect);
testRendererAssertions.installInto(expect);

expect.installPlugin(require('magicpen-prism'));

types.installInto(expect);
testRendererAssertions.installInto(expect);
expect.addType({
name: 'RawReactTestRendererJson',
base: 'object',
identify: function (value) {
return value && typeof value === 'object' && value.props && value.children && value.type;
}
});
expect.addAssertion([
'<ReactTestRenderer|ReactTestRendererOutput> to match snapshot',
'<ReactTestRenderer|ReactTestRendererOutput> to satisfy snapshot',
], function (expect) {

expect.errorMode = 'bubble';
expect.fail({
message: function (output) {
return output.text('To use the `')
.error(expect.testDescription)
.text('` assertion with the test renderer, require unexpected-react as `require(\'unexpected-react/test-renderer-jest\');`');
}
});
});

expect.addAssertion([
'<RawReactTestRendererJson> to match snapshot',
'<RawReactTestRendererJson> to satisfy snapshot',
], function (expect) {

expect.errorMode = 'bubble';
expect.fail({
message: function (output) {
return output.text('To use the `')
.error(expect.testDescription)
.text('` assertion with the test renderer, require unexpected-react as `require(\'unexpected-react/test-renderer-jest\');`')
.nl().i()
.nl().i()
.text('Also, don\'t pass the JSON, pass the test renderer directly');
}
});
});

},

clearAll() {
Expand Down
@@ -0,0 +1,58 @@

import EmulateDom from '../../helpers/emulateDom';

import Unexpected from 'unexpected';
import UnexpectedReact from '../../../unexpected-react';

import React from 'react';
import TestUtils from 'react-addons-test-utils';

import ClickCounter from '../../components/ClickCounter';

const expect = Unexpected.clone().use(UnexpectedReact);

describe('standard-instead-of-jest', function () {

it('shows a helpful error message when asserting using `to match snapshot`', function () {
expect(() => expect(<ClickCounter />, 'to match snapshot'), 'to throw',
[
'To use the `to match snapshot` assertion with the shallow and full DOM renderers, require unexpected-react as `require(\'unexpected-react/jest\');`',
'',
''
].join('\n')
);
});

it('shows a helpful error message when asserting using `to satisfy snapshot`', function () {
expect(() => expect(<ClickCounter />, 'to satisfy snapshot'), 'to throw',
[
'To use the `to satisfy snapshot` assertion with the shallow and full DOM renderers, require unexpected-react as `require(\'unexpected-react/jest\');`',
'',
''
].join('\n')
);
});

it('shows a helpful error message after shallow rendering when asserting using `to match snapshot`', function () {
expect(() => expect(<ClickCounter />, 'when rendered', 'to match snapshot'), 'to throw',
[
'expected <ClickCounter /> when rendered',
'To use the `to match snapshot` assertion with the shallow and full DOM renderers, require unexpected-react as `require(\'unexpected-react/jest\');`',
'',
''
].join('\n')
);
});

it('shows a helpful error message when asserting using `to satisfy snapshot`', function () {
expect(() => expect(<ClickCounter />, 'when rendered', 'to satisfy snapshot'), 'to throw',
[
'expected <ClickCounter /> when rendered',
'To use the `to satisfy snapshot` assertion with the shallow and full DOM renderers, require unexpected-react as `require(\'unexpected-react/jest\');`',
'',
''
].join('\n')
);
});

});
@@ -0,0 +1,40 @@
import Unexpected from 'unexpected';

import React, { PropTypes } from 'react';
import ReactTestRenderer from 'react-test-renderer';
import UnexpectedReactTest from '../../../test-renderer';

import ClickCounter from '../../components/ClickCounter';

const expect = Unexpected.clone()
.installPlugin(UnexpectedReactTest);

expect.output.preferredWidth = 80;

describe('test-renderer-instead-of-test-renderer-jest', function () {

it('shows a helpful error message when asserting using `to match snapshot`', function () {
const testRenderer = ReactTestRenderer.create(<ClickCounter />);
expect(() => expect(testRenderer, 'to match snapshot'), 'to throw',
'To use the `to match snapshot` assertion with the test renderer, require unexpected-react as `require(\'unexpected-react/test-renderer-jest\');`'
);
});

it('shows a helpful error message when asserting using `to satisfy snapshot`', function () {
const testRenderer = ReactTestRenderer.create(<ClickCounter />);
expect(() => expect(testRenderer, 'to satisfy snapshot'), 'to throw',
'To use the `to satisfy snapshot` assertion with the test renderer, require unexpected-react as `require(\'unexpected-react/test-renderer-jest\');`'
);
});

it('shows a helpful error message when asserting using `to match snapshot` and the JSON output', function () {
const testRenderer = ReactTestRenderer.create(<ClickCounter />);
expect(() => expect(testRenderer.toJSON(), 'to match snapshot'), 'to throw',
[
'To use the `to match snapshot` assertion with the test renderer, require unexpected-react as `require(\'unexpected-react/test-renderer-jest\');`',
'',
'Also, don\'t pass the JSON, pass the test renderer directly'
].join('\n')
);
});
});
12 changes: 12 additions & 0 deletions src/tests/testRenderer/snapshot.spec.js
Expand Up @@ -593,6 +593,18 @@ describe('snapshots', function () {
fs.unlinkSync.restore();
}
});

it('shows an error message if the JSON is asserted on directly', function () {

const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(() => expect(renderer.toJSON(), 'to match snapshot'), 'to throw',
[
'To assert snapshots, use the testRenderer directly, not the result of `.toJSON()`',
'e.g.',
' const testRenderer = ReactTestRenderer.create(<MyComponent />);',
' expect(testRenderer, \'to match snapshot\');'
].join('\n'));
});
});


33 changes: 33 additions & 0 deletions src/unexpected-react.js
Expand Up @@ -15,6 +15,39 @@ module.exports = {
types.installInto(expect);
shallowAssertions.installInto(expect);
deepAssertions.installInto(expect);

expect.addAssertion('<ReactTestRenderer|ReactTestRendererOutput> to (match|satisfy) snapshot', function (expect) {

expect.errorMode = 'bubble';
expect.fail({
message: function (output) {
return output.text('To use the ')
.error('`to ')
.error(this.flags.match ? 'match' : 'satisfy')
.error(' snapshot`')
.text(' assertion with the test renderer, require unexpected-react as `require(\'unexpected-react/test-rendered-jest\');`');
}
});
});

expect.addAssertion(
[
'<ReactElement|ReactShallowRenderer|RenderedReactElement|ReactShallowRendererPendingEvent|RenderedReactElementPendingEvent> to match snapshot',
'<ReactElement|ReactShallowRenderer|RenderedReactElement|ReactShallowRendererPendingEvent|RenderedReactElementPendingEvent> to satisfy snapshot'
],
function (expect) {
expect.errorMode = 'bubble';
expect.fail({
message: (output) => {
return output.text('To use the `')
.error(this.testDescription)
.text('` assertion with the shallow and full DOM renderers, require unexpected-react as `require(\'unexpected-react/jest\');`');
},
diff: (output) => {
return output;
}
});
});
},

clearAll() {
Expand Down
2 changes: 1 addition & 1 deletion wallaby-testrenderer.js
Expand Up @@ -52,4 +52,4 @@ module.exports = function (wallaby) {
})
}
};
};
};

0 comments on commit 417dd2b

Please sign in to comment.