├── .circleci └── config.yml ├── .editorconfig ├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── LICENSE ├── README.md ├── babel.config.js ├── index.js ├── jest.config.js ├── npm ├── esm │ └── index.js └── index.js ├── package.json ├── rollup.config.js ├── scripts ├── babel │ └── transform-object-assign-require.js ├── copyFiles.js └── jest │ ├── matchers │ └── toWarnDev.js │ ├── setupTests.js │ └── shouldIgnoreConsoleError.js ├── src ├── ReactShallowRenderer.js ├── __tests__ │ ├── ReactShallowRenderer-test.js │ ├── ReactShallowRendererHooks-test.js │ └── ReactShallowRendererMemo-test.js └── shared │ ├── ReactLazyComponent.js │ ├── ReactSharedInternals.js │ ├── ReactSymbols.js │ ├── checkPropTypes.js │ ├── consoleWithStackDev.js │ ├── describeComponentFrame.js │ ├── getComponentName.js │ ├── objectIs.js │ └── shallowEqual.js └── yarn.lock /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # JavaScript Node CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-javascript/ for more details 4 | # 5 | version: 2 6 | jobs: 7 | build: 8 | docker: 9 | # specify the version you desire here 10 | - image: circleci/node:10 11 | 12 | steps: 13 | - checkout 14 | 15 | # Download and cache dependencies 16 | - restore_cache: 17 | keys: 18 | - v1-dependencies-{{ checksum "yarn.lock" }} 19 | # fallback to using the latest cache if no exact match is found 20 | - v1-dependencies- 21 | 22 | - run: yarn --frozen-lockfile 23 | 24 | - save_cache: 25 | key: v1-dependencies-{{ checksum "yarn.lock" }} 26 | paths: 27 | - ~/.cache/yarn 28 | 29 | - run: yarn lint 30 | 31 | - run: yarn test 32 | 33 | - run: yarn build 34 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 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 19 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const restrictedGlobals = require('confusing-browser-globals'); 4 | 5 | const OFF = 'off'; 6 | const ERROR = 'error'; 7 | 8 | // Files that are transformed and can use ES6/JSX. 9 | const esNextPaths = [ 10 | // Internal forwarding modules 11 | './index.js', 12 | // Source files 13 | 'src/**/*.js', 14 | // Jest 15 | 'scripts/jest/setupTests.js', 16 | ]; 17 | 18 | // Files that we distribute on npm that should be ES5-only. 19 | const es5Paths = ['npm/**/*.js']; 20 | 21 | module.exports = { 22 | env: { 23 | browser: true, 24 | es6: true, 25 | node: true, 26 | }, 27 | extends: [ 28 | 'eslint:recommended', 29 | 'plugin:react/recommended', 30 | 'plugin:prettier/recommended', 31 | 'prettier/react', 32 | ], 33 | globals: { 34 | Atomics: 'readonly', 35 | SharedArrayBuffer: 'readonly', 36 | }, 37 | parser: 'babel-eslint', 38 | parserOptions: { 39 | ecmaVersion: 2018, 40 | sourceType: 'script', 41 | }, 42 | plugins: ['react'], 43 | settings: { 44 | react: { 45 | version: 'detect', 46 | }, 47 | }, 48 | rules: { 49 | 'no-console': ERROR, 50 | 'no-empty': OFF, 51 | 'no-restricted-globals': [ERROR, ...restrictedGlobals], 52 | 'no-unsafe-finally': OFF, 53 | 'no-unused-vars': [ERROR, {args: 'none'}], 54 | 'no-useless-escape': OFF, 55 | 56 | // We apply these settings to files that should run on Node. 57 | // They can't use JSX or ES6 modules, and must be in strict mode. 58 | // They can, however, use other ES6 features. 59 | // (Note these rules are overridden later for source files.) 60 | 'no-var': ERROR, 61 | strict: ERROR, 62 | }, 63 | overrides: [ 64 | { 65 | // We apply these settings to files that we ship through npm. 66 | // They must be ES5. 67 | files: es5Paths, 68 | parser: 'espree', 69 | parserOptions: { 70 | ecmaVersion: 5, 71 | sourceType: 'script', 72 | }, 73 | rules: { 74 | 'no-var': OFF, 75 | strict: ERROR, 76 | }, 77 | overrides: [ 78 | { 79 | // These files are ES5 but with ESM support. 80 | files: ['npm/esm/**/*.js'], 81 | parserOptions: { 82 | // Although this is supposed to be 5, ESLint doesn't allow sourceType 'module' when ecmaVersion < 2015. 83 | // See https://github.com/eslint/eslint/issues/9687#issuecomment-508448526 84 | ecmaVersion: 2015, 85 | sourceType: 'module', 86 | }, 87 | }, 88 | ], 89 | }, 90 | { 91 | // We apply these settings to the source files that get compiled. 92 | // They can use all features including JSX (but shouldn't use `var`). 93 | files: esNextPaths, 94 | parserOptions: { 95 | ecmaVersion: 2018, 96 | sourceType: 'module', 97 | }, 98 | rules: { 99 | 'no-var': ERROR, 100 | strict: OFF, 101 | }, 102 | }, 103 | { 104 | // Rollup understands ESM 105 | files: ['rollup.config.js'], 106 | parserOptions: { 107 | ecmaVersion: 2018, 108 | sourceType: 'module', 109 | }, 110 | }, 111 | { 112 | files: ['**/__tests__/**/*.js', 'scripts/jest/setupTests.js'], 113 | env: { 114 | 'jest/globals': true, 115 | }, 116 | plugins: ['jest'], 117 | rules: { 118 | // https://github.com/jest-community/eslint-plugin-jest 119 | 'jest/no-focused-tests': ERROR, 120 | 'jest/valid-expect': ERROR, 121 | 'jest/valid-expect-in-promise': ERROR, 122 | 123 | // React & JSX 124 | // This isn't useful in our test code 125 | 'react/display-name': OFF, 126 | 'react/jsx-key': OFF, 127 | 'react/no-deprecated': OFF, 128 | 'react/no-string-refs': OFF, 129 | 'react/prop-types': OFF, 130 | }, 131 | }, 132 | { 133 | files: ['scripts/**/*.js', 'npm/**/*.js'], 134 | rules: { 135 | 'no-console': OFF, 136 | }, 137 | }, 138 | ], 139 | }; 140 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | build/ 3 | .eslintcache 4 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "bracketSpacing": false, 3 | "singleQuote": true, 4 | "jsxBracketSameLine": true, 5 | "trailingComma": "all", 6 | "printWidth": 80 7 | } 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `react-shallow-renderer` 2 | 3 | [](https://github.com/enzymejs/react-shallow-renderer/blob/master/LICENSE) 4 | [](https://www.npmjs.com/package/react-shallow-renderer) 5 | [](https://circleci.com/gh/enzymejs/react-shallow-renderer/tree/master) 6 | 7 | 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. 8 | 9 | ## Installation 10 | 11 | ```sh 12 | # npm 13 | npm install react-shallow-renderer --save-dev 14 | 15 | # Yarn 16 | yarn add react-shallow-renderer --dev 17 | ``` 18 | 19 | ## Usage 20 | 21 | For example, if you have the following component: 22 | 23 | ```jsx 24 | function MyComponent() { 25 | return ( 26 |
{this.state.counter}
; 1034 | } 1035 | } 1036 | 1037 | const shallowRenderer = createRenderer(); 1038 | const result = shallowRenderer.render({this.state.counter}
; 1062 | } 1063 | } 1064 | 1065 | const shallowRenderer = createRenderer(); 1066 | const result = shallowRenderer.render({this.state.counter}
; 1096 | } 1097 | } 1098 | 1099 | const shallowRenderer = createRenderer(); 1100 | const result = shallowRenderer.render(24 | Your name is: {name} 25 |
26 |38 | Your name is: Dominic 39 |
40 |50 | Your name is: Dominic 51 |
52 |67 | Your name is: {name} 68 |
69 |81 | Your name is: Dan 82 |
83 |105 | Your name is: {name + ' (' + letter + ')'} 106 |
107 |118 | Your name is: Sophie (S) 119 |
120 |127 | Your name is: Sophie (S) 128 |
129 |136 | Your name is: Dan (D) 137 |
138 |160 | The counter is at: {state.count.toString()} 161 |
162 |172 | The counter is at: 0 173 |
174 |182 | The counter is at: 0 183 |
184 |210 | The counter is at: {state.count.toString()} 211 |
212 |222 | The counter is at: 1 223 |
224 |The random number is: {randomNumberRef.current.number}
260 |The random number is: {randomNumber.number}
280 |{value}
300 |default
310 |321 | Your name is: {name} 322 |
323 |333 | Your name is: {name} 334 |
335 |346 | Your name is: Dominic 347 |
348 |355 | Your name is: Dan 356 |
357 |The random number is: {randomNumberRef.current.number}
368 |393 | Your name is: {name} ({count}) 394 |
395 |404 | Your name is: Dominic ({0}) 405 |
, 406 | ); 407 | 408 | result.props.onClick(); 409 | let updated = shallowRenderer.render(element); 410 | expect(updated.props.children).toEqual( 411 |412 | Your name is: Dan ({0}) 413 |
, 414 | ); 415 | 416 | _dispatch('foo'); 417 | updated = shallowRenderer.render(element); 418 | expect(updated.props.children).toEqual( 419 |420 | Your name is: Dan ({1}) 421 |
, 422 | ); 423 | 424 | _dispatch('inc'); 425 | updated = shallowRenderer.render(element); 426 | expect(updated.props.children).toEqual( 427 |428 | Your name is: Dan ({2}) 429 |
, 430 | ); 431 | }); 432 | 433 | it('should ignore a foreign update outside the render', () => { 434 | let _updateCountForFirstRender; 435 | 436 | function SomeComponent() { 437 | const [count, updateCount] = React.useState(0); 438 | if (!_updateCountForFirstRender) { 439 | _updateCountForFirstRender = updateCount; 440 | } 441 | return count; 442 | } 443 | 444 | const shallowRenderer = createRenderer(); 445 | const element ={this.state.counter}
; 1068 | } 1069 | }, 1070 | ); 1071 | 1072 | const shallowRenderer = createRenderer(); 1073 | const result = shallowRenderer.render({this.state.counter}
; 1098 | } 1099 | }, 1100 | ); 1101 | 1102 | const shallowRenderer = createRenderer(); 1103 | const result = shallowRenderer.render({this.state.counter}
; 1134 | } 1135 | }, 1136 | ); 1137 | 1138 | const shallowRenderer = createRenderer(); 1139 | const result = shallowRenderer.render(