Shallow rendering is useful to constrain yourself to testing a component as a unit, and to ensure that your tests aren't indirectly asserting on behavior of child components.

As of Enzyme v3, the shallow API does call React lifecycle methods such as componentDidMount and componentDidUpdate. You can read more about this in the version 3 migration guide.

import { shallow } from 'enzyme';
import sinon from 'sinon';
import Foo from './Foo';

describe('', () => {
  it('renders three  components', () => {
    const wrapper = shallow();
    expect(wrapper.find(Foo)).to.have.lengthOf(3);
  });

  it('renders an `.icon-star`', () => {
    const wrapper = shallow();
    expect(wrapper.find('.icon-star')).to.have.lengthOf(1);
  });

  it('renders children when passed in', () => {
    const wrapper = shallow((
      
        
)); expect(wrapper.contains(
)).to.equal(true); }); it('simulates click events', () => { const onButtonClick = sinon.spy(); const wrapper = shallow(); wrapper.find('button').simulate('click'); expect(onButtonClick).to.have.property('callCount', 1); }); });

shallow(node[, options]) => ShallowWrapper

Arguments

  1. node (ReactElement): The node to render
  2. options (Object [optional]):
    • options.context: (Object [optional]): Context to be passed into the component
    • options.disableLifecycleMethods: (Boolean [optional]): If set to true, componentDidMount is not called on the component, and componentDidUpdate is not called after setProps and setContext. Default to false.

Returns

and Wedding Silk DiBanGu Formal Necktie Pocket Men's Set Tie Gold2 Cufflinks Square Solid Tie pqxX64xa

06 Floral Purple Reversible Cravat Paisley YCHENG Neck Plaid Ascot Mens Scarf Jacquard Ljb Tie Elegant ShallowWrapper: The wrapper instance around the rendered output.

ShallowWrapper API

.find(selector) => ShallowWrapper

Find every node in the render tree that matches the provided selector.

.findWhere(predicate) => ShallowWrapper

Find every node in the render tree that returns true for the provided predicate function.

.filter(selector) => ShallowWrapper

Remove nodes in the current wrapper that do not match the provided selector.

Tie 06 Plaid Floral Reversible Mens Ascot YCHENG Purple Jacquard Paisley Scarf Ljb Neck Cravat Elegant .filterWhere(predicate) => ShallowWrapper

Remove nodes in the current wrapper that do not return true for the provided predicate function.

.hostNodes() => ShallowWrapper

Removes nodes that are not host nodes; e.g., this will only return HTML nodes.

Colours Skinny White Knitted Mens Tie Black Striped in Various Neck 0wqBPqf.contains(nodeOrNodes) => Boolean

Returns whether or not a given node or array of nodes is somewhere in the render tree.

Set Paisley Flower Necktie Neck Tie Retro cufflinks hanky Rose amp;L® Black Rose Cotton Floral L Men zAWCO.containsMatchingElement(node) => Boolean

Returns whether or not a given react element exists in the shallow render tree.

.containsAllMatchingElements(nodes) => Boolean

Returns whether or not all the given react elements exists in the shallow render tree.

.containsAnyMatchingElements(nodes) => Boolean

Returns whether or not one of the given react elements exists in the shallow render tree.

Tie Neck YCHENG 06 Scarf Plaid Cravat Elegant Reversible Jacquard Purple Paisley Floral Ljb Ascot Mens .equals(node) => Boolean

Returns whether or not the current render tree is equal to the given node, based on the expected value.

.matchesElement(node) => Boolean

Returns whether or not a given react element matches the shallow render tree.

.hasClass(className) => Boolean

Returns whether or not the current node has the given class name or not.

.is(selector) => Boolean

Returns whether or not the current node matches a provided selector.

Green Plain Clip Solid Tie Colour Dark Slim 2" Satin On Safety wSqnvIA

Returns whether or not the current node exists, or, if given a selector, whether that selector has any matching results.

Scarf 06 Tie Purple Elegant Neck Cravat Reversible Paisley Ascot Jacquard Floral Mens Ljb Plaid YCHENG .isEmpty() => Boolean

Deprecated: Use .exists() instead.

.isEmptyRender() => Boolean

Returns whether or not the current component returns a falsy value.

.not(selector) => ShallowWrapper

Remove nodes in the current wrapper that match the provided selector. (inverse of Tie Cravat YCHENG Plaid Scarf Elegant Floral Mens Reversible Neck 06 Jacquard Ascot Purple Paisley Ljb .filter())

Gift Christmas Tie Decor LED Up Santa Huijun Ornaments Xmas Novelty Party Office Snowman Light ZqwCWd8Wedding Classic Ecotrump Novelty Stripe Purple Jacquard 8cm Silk Necktie Mens Black qH0wZO

Get a wrapper with all of the children nodes of the current wrapper.

.childAt(index) => ShallowWrapper

Returns a new wrapper with child at the specified index.

.parents() => ShallowWrapper

Get a wrapper with all of the parents (ancestors) of the current node.

.parent() => ShallowWrapper

Get a wrapper with the direct parent of the current node.

.closest(selector) => ShallowWrapper

Get a wrapper with the first ancestor of the current node to match the provided selector.

Narrow Business SEESUNGM Formal Professional Tie Tie Color Pure Suit 6Cm Black x1qBwTqY.shallow([options]) => ShallowWrapper

Shallow renders the current node and returns a shallow wrapper around it.

Gray Pocket Tie Pink HISDERN Handkerchief Long Square Set Men's Check Necktie amp; Extra t8xwq8FP

Returns a CheerioWrapper of the current node's subtree.

.unmount() => ShallowWrapper

A method that un-mounts the component.

.text() => StringBlack Layer Dickie Dickie UtopyaUK Bow Party Double Satin Fancy High Adjustable Bow Wedding Novelty Fastening Quality Mens Pre amp; ties Clip Tie Plain Tied wUqUgnZ5

Returns a string representation of the text nodes in the current render tree.

.html() => String

Returns a static HTML rendering of the current node.

.get(index) => ReactElement

Returns the node at the provided index of the current wrapper.

.getElement() => ReactElement

Returns the wrapped ReactElement.

.getElements() => Array

Returns the wrapped ReactElements.

.at(index) => ShallowWrapper

Returns a wrapper of the node at the provided index of the current wrapper.

.first() => ShallowWrapper

Returns a wrapper of the first node of the current wrapper.

.last() => ShallowWrapper

Returns a wrapper of the last node of the current wrapper.

YCHENG Scarf Plaid Reversible Purple 06 Tie Neck Cravat Mens Ascot Floral Jacquard Ljb Elegant Paisley .state([key]) => Any

Returns the state of the root component.

.context([key]) => Any

Returns the context of the root component.

.props() => Object

Returns the props of the current node.

.prop(key) => Any

Returns the named prop of the current node.

.key() => String

Returns the key of the current node.

.simulate(event[, data]) => ShallowWrapper

Simulates an event on the current node.

YCHENG 06 Mens Ljb Scarf Elegant Plaid Ascot Paisley Tie Cravat Floral Neck Jacquard Reversible Purple .setState(nextState) => ShallowWrapper

Manually sets state of the root component.

.setProps(nextProps[, callback]) => ShallowWrapper

Manually sets props of the root component.

.setContext(context) => ShallowWrapper

Manually sets context of the root component.

.instance() => ReactComponent

Returns the instance of the root component.

HISDERN Set Wedding amp; Handkerchief Silk Woven Yellow Check Pocket Men's Necktie amp; Classic Square Striped Grey Tie U8nOUfqr

Calls .forceUpdate() on the root component instance.

Mens Wedding Polyester 4" HYSENM Diagonal Stripe 3 Blue Fashion Necktie Casual dAqY1RwZ.debug() => String

Returns a string representation of the current shallow render tree for debugging purposes.

.type() => String|Function|null

Returns the type of the current node of the wrapper.

.name() => String

Returns the name of the current node of the wrapper.

.forEach(fn) => ShallowWrapper

Iterates through each node of the current wrapper and executes the provided function

.map(fn) => Array

Maps the current array of nodes to another array.

Business Dragon Blue Wedding MIAOWANG Tie Dark Groom Tie Man Chinese pA1vIx

Reduces the current array of nodes to a value

.reduceRight(fn[, initialValue]) => Any

Reduces the current array of nodes to a value, from right to left.

.slice([begin[, end]]) => ShallowWrapper

Returns a new wrapper with a subset of the nodes of the original wrapper, according to the rules of Array#slice.

.tap(intercepter) => Self

Taps into the wrapper method chain. Helpful for debugging.

.some(selector) => Boolean

Mens YCHENG Ascot Jacquard Tie Purple Reversible Scarf 06 Ljb Floral Elegant Cravat Neck Paisley Plaid Returns whether or not any of the nodes in the wrapper match the provided selector.

.someWhere(predicate) => Boolean

Returns whether or not any of the nodes in the wrapper pass the provided predicate function.

.every(selector) => Boolean

Returns whether or not all of the nodes in the wrapper match the provided selector.

.everyWhere(predicate) => Boolean

Returns whether or not all of the nodes in the wrapper pass the provided predicate function.

.dive([options]) => ShallowWrapper

Shallow render the one non-DOM child of the current wrapper, and return a wrapper around the result.