Skip to content

Commit 16be252

Browse files
authored
Add CJS, ESM, UMD builds (#2)
* Add .editorconfig * Inline invariant * Replace `console.error` with `error` * Rename package * Tweak build configuration * Update dependencies
1 parent 52326c0 commit 16be252

24 files changed

+672
-276
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
indent_size = 2
8+
indent_style = space
9+
insert_final_newline = true
10+
max_line_length = 80
11+
trim_trailing_whitespace = true
12+
13+
[*.md]
14+
max_line_length = 0
15+
trim_trailing_whitespace = false
16+
17+
[COMMIT_EDITMSG]
18+
max_line_length = 0

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
build/

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"bracketSpacing": false,
3+
"singleQuote": true,
4+
"jsxBracketSameLine": true,
5+
"trailingComma": "all",
6+
"printWidth": 80
7+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Facebook, Inc. and its affiliates.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,63 @@
1-
# `react-test-renderer`
1+
# `react-shallow-renderer`
22

3-
This package provides an experimental React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment.
3+
When writing unit tests for React, shallow rendering can be helpful. Shallow rendering lets you render a component "one level deep" and assert facts about what its render method returns, without worrying about the behavior of child components, which are not instantiated or rendered. This does not require a DOM.
44

5-
Essentially, this package makes it easy to grab a snapshot of the "DOM tree" rendered by a React DOM or React Native component without using a browser or jsdom.
5+
## Installation
66

7-
Documentation:
7+
```sh
8+
# npm
9+
npm install react-shallow-renderer --save-dev
810

9-
[https://reactjs.org/docs/test-renderer.html](https://reactjs.org/docs/test-renderer.html)
11+
# Yarn
12+
yarn add react-shallow-renderer --dev
13+
```
14+
15+
## Usage
1016

11-
Usage:
17+
For example, if you have the following component:
1218

1319
```jsx
14-
const ReactTestRenderer = require('react-test-renderer');
20+
function MyComponent() {
21+
return (
22+
<div>
23+
<span className="heading">Title</span>
24+
<Subcomponent foo="bar" />
25+
</div>
26+
);
27+
}
28+
```
1529

16-
const renderer = ReactTestRenderer.create(
17-
<Link page="https://www.facebook.com/">Facebook</Link>
18-
);
30+
Then you can assert:
1931

20-
console.log(renderer.toJSON());
21-
// { type: 'a',
22-
// props: { href: 'https://www.facebook.com/' },
23-
// children: [ 'Facebook' ] }
32+
```jsx
33+
import ShallowRenderer from 'react-shallow-renderer';
34+
// in your test:
35+
const renderer = new ShallowRenderer();
36+
renderer.render(<MyComponent />);
37+
const result = renderer.getRenderOutput();
38+
expect(result.type).toBe('div');
39+
expect(result.props.children).toEqual([
40+
<span className="heading">Title</span>,
41+
<Subcomponent foo="bar" />,
42+
]);
2443
```
2544

26-
You can also use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: https://facebook.github.io/jest/blog/2016/07/27/jest-14.html.
45+
Shallow testing currently has some limitations, namely not supporting refs.
46+
47+
> Note:
48+
>
49+
> We also recommend checking out Enzyme's [Shallow Rendering API](https://airbnb.io/enzyme/docs/api/shallow.html). It provides a nicer higher-level API over the same functionality.
50+
51+
## Reference
52+
53+
### `shallowRenderer.render()`
54+
55+
You can think of the shallowRenderer as a "place" to render the component you're testing, and from which you can extract the component's output.
56+
57+
`shallowRenderer.render()` is similar to [`ReactDOM.render()`](https://reactjs.org/docs/react-dom.html#render) but it doesn't require DOM and only renders a single level deep. This means you can test components isolated from how their children are implemented.
58+
59+
### `shallowRenderer.getRenderOutput()`
60+
61+
After `shallowRenderer.render()` has been called, you can use `shallowRenderer.getRenderOutput()` to get the shallowly rendered output.
62+
63+
You can then begin to assert facts about the output.

babel.config.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
module.exports = {
2-
presets: ['@babel/preset-env', '@babel/preset-react'],
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
modules: process.env.NODE_ENV === 'test' ? 'auto' : false,
7+
// Exclude transforms that make all code slower.
8+
exclude: ['transform-typeof-symbol'],
9+
targets: process.env.NODE_ENV === 'test' ? {node: 'current'} : {ie: 11},
10+
},
11+
],
12+
'@babel/preset-react',
13+
],
314
plugins: [
4-
'@babel/plugin-proposal-class-properties',
15+
['@babel/plugin-proposal-class-properties', {loose: true}],
16+
['@babel/plugin-transform-classes', {loose: true}],
17+
['@babel/plugin-transform-template-literals', {loose: true}],
18+
// The following plugin is configured to use `Object.assign` directly.
19+
// Note that we ponyfill `Object.assign` below.
20+
// { ...todo, complete: true }
21+
[
22+
'@babel/plugin-proposal-object-rest-spread',
23+
{loose: true, useBuiltIns: true},
24+
],
25+
// Use 'object-assign' ponyfill.
26+
require.resolve('./scripts/babel/transform-object-assign-require'),
27+
// Keep stacks detailed in tests.
528
process.env.NODE_ENV === 'test' &&
629
'@babel/plugin-transform-react-jsx-source',
730
].filter(Boolean),

shallow.js renamed to index.js

File renamed without changes.

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
2+
modulePathIgnorePatterns: ['<rootDir>/build/'],
23
setupFilesAfterEnv: ['./scripts/jest/setupTests.js'],
34
};

npm/esm/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './react-shallow-renderer';

npm/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('./cjs/react-shallow-renderer.js');

0 commit comments

Comments
 (0)