react-test-renderer-shallow-json
v1.1.4
Published
React package for shallow snapshot testing.
Downloads
245
Maintainers
Readme
react-test-renderer-shallow-json
Return an object representing the rendered tree.
This tree contains the platform-specific nodes like <div>
or <View>
and includes the user-written components as well.
User-written components are rendered recursively up to the specified depth
.
This is handy for shallow snapshot testing.
Disclaimer: this is not shallow rendering.
Instead of react-test-renderer
's .toJSON()
the result of .toTree()
is used as an alternative to react-test-renderer/shallow
.
Table of Contents
Installation
npm install --save-dev react-test-renderer-shallow-json
Usage
Replace your .toJSON()
based snapshot by a call to shallowJSON()
with the result of .toTree()
as it's argument.
import TestRenderer from 'react-test-renderer';
import shallowJSON from 'react-test-renderer-shallow-json';
function MyComponent() {
return (
<div>
<SubComponent foo="bar" />
<p className="my">Hello</p>
</div>
)
}
function SubComponent() {
return (
<p className="sub">Sub</p>
);
}
const testRenderer = TestRenderer.create(<MyComponent />);
console.log(shallowJSON(testRenderer.toTree()));
API
Please find the API docs at ./docs/README.md
.
Why?
- [Shallow Renderer] Plan forward #17321
react-shallow-renderer
is re-exported and moved to enzymejs/react-shallow-renderer
License
The MIT License (MIT)
Copyright (c) 2023 Andreas Deuschlinger
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.