Skip to content

Commit b872114

Browse files
kpsrokathymikee
authored andcommitted
feat: Removes peer dependency on react-test-renderer. (#26)
1 parent c792640 commit b872114

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

__tests__/__snapshots__/snapshotDiff.test.js.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,8 @@ exports[`diffs short strings 1`] = `
129129
+ abcy
130130
"
131131
`;
132+
133+
exports[`failed optional deps throws with sensible message on missing react-test-renderer 1`] = `
134+
"Failed to load optional module \\"react-test-renderer\\". If you need to compare React elements, please add \\"react-test-renderer\\" to your project's dependencies.
135+
Cannot find module 'non-existent-module-for-testing' from 'snapshotDiff.test.js'"
136+
`;

__tests__/snapshotDiff.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,24 @@ test('can use contextLines with React components', () => {
153153
})
154154
).toMatchSnapshot();
155155
});
156+
157+
describe('failed optional deps', () => {
158+
beforeEach(() => {
159+
jest.mock('react-test-renderer', () => {
160+
// $FlowFixMe -- this is intended.
161+
require('non-existent-module-for-testing'); // eslint-disable-line
162+
});
163+
});
164+
165+
afterEach(() => {
166+
jest.resetModules();
167+
});
168+
169+
test('throws with sensible message on missing react-test-renderer', () => {
170+
const testComponentA = <Component test="a" />;
171+
const testComponentB = <Component test="b" />;
172+
expect(() =>
173+
snapshotDiff(testComponentA, testComponentB)
174+
).toThrowErrorMatchingSnapshot();
175+
});
176+
});

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
},
1717
"repository": "https://github.com/thymikee/snapshot-diff",
1818
"peerDependencies": {
19-
"jest": ">=16",
20-
"react-test-renderer": ">=15"
19+
"jest": ">=16"
2120
},
2221
"dependencies": {
2322
"jest-diff": "^22.0.6",

src/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,24 @@ function diffStrings(valueA: any, valueB: any, options: Options) {
5858
});
5959
}
6060

61+
function requireReactTestRenderer() {
62+
try {
63+
return require('react-test-renderer'); // eslint-disable-line import/no-extraneous-dependencies
64+
} catch (error) {
65+
if (error.code === 'MODULE_NOT_FOUND') {
66+
throw new Error(
67+
`Failed to load optional module "react-test-renderer". ` +
68+
`If you need to compare React elements, please add "react-test-renderer" to your ` +
69+
`project's dependencies.\n` +
70+
`${error.message}`
71+
);
72+
}
73+
throw error;
74+
}
75+
}
76+
6177
function diffReactComponents(valueA: any, valueB: any, options: Options) {
62-
const renderer = require('react-test-renderer');
78+
const renderer = requireReactTestRenderer();
6379
const reactValueA = renderer.create(valueA).toJSON();
6480
const reactValueB = renderer.create(valueB).toJSON();
6581
const prettyFormatOptions = { plugins: [ReactElement], min: true };

0 commit comments

Comments
 (0)