Git Product home page Git Product logo

react-shallow-renderer's Introduction

react-shallow-renderer

GitHub license npm version CircleCI

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.

Installation

# npm
npm install react-shallow-renderer --save-dev

# Yarn
yarn add react-shallow-renderer --dev

Usage

For example, if you have the following component:

function MyComponent() {
  return (
    <div>
      <span className="heading">Title</span>
      <Subcomponent foo="bar" />
    </div>
  );
}

Then you can assert:

import ShallowRenderer from 'react-shallow-renderer';
// in your test:
const renderer = new ShallowRenderer();
renderer.render(<MyComponent />);
const result = renderer.getRenderOutput();
expect(result.type).toBe('div');
expect(result.props.children).toEqual([
  <span className="heading">Title</span>,
  <Subcomponent foo="bar" />,
]);

Shallow testing currently has some limitations, namely not supporting refs.

Note:

We also recommend checking out Enzyme's Shallow Rendering API. It provides a nicer higher-level API over the same functionality.

Reference

shallowRenderer.render()

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.

shallowRenderer.render() is similar to ReactDOM.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.

shallowRenderer.getRenderOutput()

After shallowRenderer.render() has been called, you can use shallowRenderer.getRenderOutput() to get the shallowly rendered output.

You can then begin to assert facts about the output.

react-shallow-renderer's People

Contributors

acdlite avatar adamsau avatar aweary avatar bvaughn avatar chenesan avatar d4rky-pl avatar dependabot-preview[bot] avatar eps1lon avatar fatfisz avatar flarnie avatar gaearon avatar him2him2 avatar iamdustan avatar jwbay avatar koba04 avatar kweiberth avatar lunaruan avatar maciej-ka avatar necolas avatar nminhnguyen avatar raphamorim avatar rodrigopr avatar sebmarkbage avatar simenb avatar skratchdot avatar sophiebits avatar threepointone avatar trueadm avatar walaura avatar zpao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-shallow-renderer's Issues

Feature: Add find* to gain alignment with react-test-renderer

It seems that react-test-renderer exports this package under react-test-renderer/shallow to support shallow rendering. However this package doesn't contain the low level "find" functions that make it easy to dive into a shallow component similar to react-test-renderer's default export.

The ask is to implement the following functions for consistency:

I know enzyme "provides a nicer higher-level API over the same functionality" my ask is to provide even basic low level to have alignment between shallow and deep core rendering engines.

I think we could probably just copy most of the logic for the deep renderer here and it should get us 80% of the way.

Then in the future Enzyme could update to use these lower level functions to help fuel it's higher level functions.

React 18 support

In my very very limited use, it doesn't appear to be a problem to just be using React 18 so far, but it would be good to explicitly add 18 to peerDeps if compatible. This would just silence the install time warning.

react-shallow-renderer 16.14.0 breaks Jest

 FAIL  src/utils/columnUtils.test.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    D:\repos\react-data-grid\node_modules\react-shallow-renderer\index.js:10
    export {default} from './src/ReactShallowRenderer';
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      at new Script (node:vm:100:7)
      at Object.<anonymous> (node_modules/react-test-renderer/shallow.js:3:18)

Jest doesn't support esm yet, and modules under node_modules aren't transpiled by default, so this is going to break builds.
For example: https://github.com/adazzle/react-data-grid/pull/2097/checks?check_run_id=1286474140

Arrow Function names no longer populated after upgrade to babel 7

I'm not positive, but I think this could be an error in this repo. I'm upgrading from babel 6 to 7, and found that my tests using react-test-renderer/shallow are failing for functional component children which are using arrow functions. I'm no longer able to access result.type.name on the render result, for instance. It comes back as '' instead of the name of the arrow function. I still see the display names correctly in the react dev tools, just not these tests.

Let me know if any additional info would be useful to debug this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.