百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-unit

> 编程语言
开源

适用于 ReactJS 的轻量级单元测试库

194 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

适用于 ReactJS 的轻量级单元测试库

react-unit

React Unit is a lightweight unit test library for ReactJS with very few (js-only) dependencies.

By using react-unit you can run your ReactJS unit tests directly from node or gulp without having to install any heavyweight external dependencies (such as jsdom, phantomjs, the python runtime, etc.).

Note: for testing simple components you might also be interested in react-cucumber.

Installation

bash
npm install --save-dev react-unit

and then, in your tests:

…

Note that, while this example is using Jasmine, react-unit should work with any other test language of your choice.

Usage

To use react-unit just require the createComponent function:

javascript
var createComponent = require('react-unit');

Then use it to create your component:

javascript
var component = createComponent();

or (if, for some reason you are not into JSX):

javascript
var component = createComponent(React.createElement(MyComponent, { value: "hello, world!" }));

Now that you have a representation of your component you can use it to find actual HTML elements calling findByQuery:

javascript
var allInputs     = component.findByQuery('input');
var allRows       = component.findByQuery('.row');
var allFirstNames = component.findByQuery('[name=firstName]');

By now you probably noted that findByQuery takes something suspiciously similar to jQuery selectors. This is not an innocent coincidence, react-unit is bundled with the amazing jQuery Sizzle to allow you to search your react DOM using query selectors.

In addition to findByQuery you can use findBy to test every element using a custom function:

javascript
var all = component.findBy(function() { return true; }); // () => true
var moreThanTwo = component.findBy(function(c) { return c.props.value > 2 });

To find elements by their ref attribute, you can use the findByRef method:

javascript
var allMyRefs = component.findByRef('myRef');

If you want to find a component using a component variable instead of a string expression, you can use findByComponent:

javascript
var component = createComponent.shallow(); // Note: the .shallow!
// or var component = createComponent.interleaved();

var children = component.findByComponent(ChildComponent);

Note that findByComponent only works with shallow and interleaved rendering modes. See Rendering Modes below for more details.

If you want to test event handling, you can bind a handler to your component:

javascript
var changeEvent;
function handler(e) { changeEvent = e; }
var component = createComponent();

Then find and interact with any element in the component:

javascript
component.findByQuery('some selector')[0].onChange('some event');

Finally assert the event:

javascript
expect(changeEvent).toBe('some event');

If at any point you want to inspect the rendered component you can use:

javascript
console.log(component.dump());

API Reference

Creating components

javascript
// createComponent :: ReactElement -> Component
createComponent = (reactElement) => Component

Renders reactElement using the deep rendering strategy (see Rendering Modes for more details). Returns the rendered Component.

This method produces a component tree that is somewhat similar to applying ReactDOM.render.

For example:

javascript
var createComponent = require('react-unit');
var component = createComponent();

More examples in test/create-component.jsx.


javascript
// createComponent.shallow :: ReactElement -> Component
createComponent.shallow = (reactElement) => Component

Renders reactElement using the shallow rendering strategy (see Rendering Modes for more details). Returns the rendered Component.

This method produces a shallow component tree. That is, it renders the root component and all the children HTML nodes, stopping at the first child component level.

For example:

javascript
var createComponent = require('react-unit');
var component = createComponent.shallow();

More examples in test/create-component-shallow.jsx.


javascript
// createComponent.interleaved :: ReactElement -> Component
createComponent.interleaved = (reactElement) => Component

Renders reactElement using the interleaved rendering strategy (see Rendering Modes for more details). Returns the rendered Component.

This method produces a component tree that interleaves react components and actual rendered components.

For example:

javascript
var createComponent = require('react-unit');
var component = createComponent.interleaved();

More examples in test/create-component-interleaved.jsx.


Finding components

javascript
// findByQuery :: String -> [Component]
component.findByQuery => (sizzleExpression) => [Components]

Returns all the descendant elements of component matching sizzleExpression.

For example:

javascript
var inputs = component.findByQuery('input');

More examples in test/find-by-query.jsx.


javascript
// findByComponent :: ReactElement -> [Component]
component.findByComponent => (reactElement) => [Components]

Returns all the descendant elements of component of type reactElement. Note that findByComponent only works with shallow and interleaved rendering modes. See Rendering Modes below for more details.

For example:

javascript
// assuming: var MyItem = React.createClass({ ... });
var items = component.findByComponent(MyItem);

More examples in test/find-by-component.jsx.


javascript
// findBy :: (Component -> bool) -> [Component]
component.findBy => (fn) => [Components]

Returns all the descendant elements of component for whom fn returns true.

For example:

javascript
var moreThanTwos = component.findBy(c => c.props.value > 2);

More examples in test/find-by.jsx.


javascript
// findByRef :: String -> [Component]
component.findBy => (ref) => [Components]

Returns all the descendant elements of component matching the ref attribute.

For example:

javascript
var allMyRefs = component.findByRef('myRef');

More examples in test/find-by-ref.jsx.


Inspecting components

javascript
// dump :: () -> String
component.dump => () => String

Returns a string representation of the pseudo-HTML of the component. This method is very useful for troubleshooting broken tests.

For example:

javascript
var html = component.dump();
// or
console.log(component.dump());

javascript
component.texts // :: [String]
component.text  // :: String

Return the text of all the descendant elements of component. texts is a flat array containing the texts of every descendant element in depth order. text behaves like DOMNode.textContent (i.e. component.texts.join('')).

Some examples in test/text.jsx.


javascript
// key :: Object
component.props

The props object of component.


javascript
// key :: String
component.key

The key of component.


javascript
// ref :: String
component.ref

The ref of component.


Rendering Modes

Deep rendering (default behavior)

By default react-unit will use a deep (recursive) rendering strategy. This produces an output that is very similar to that of ReactDOM.render.

For example, given:

javascript
var Person = React.createClass({
  render: function() {
    var children = React.Children.map(this.props.children, (c,i) => 
{c}
);
    return 

  }
});

Calling createComponent in a composite component:

javascript
var component = createComponent(
  
    
    
    
  );

Results in a representation of the following HTML:

xml

    

    

      

    

    

      

    

  

In other words, the output is the HTML that results of calling the render method of every component. Note that, as a side-effect of deep rendering, component tags (e.g. ``) were erased from the HTML representation.

In the example above you find Lisa with:

javascript
var lisa = component.findByQuery('div > ul > li > div > h1')[1];

On the flip side, you cannot use findByQuery to find your components because, after rendering, they were replaced by the HTML they generate in their render method:

javascript
var persons = component.findByQuery('Person');
expect(persons.length).toEqual(0);

Shallow rendering

Sometimes you might want to stop rendering after the first level of components. In true unit test spirit you would like to just test a component assuming the components it depends upon are working.

To achieve this you can use createComponent.shallow as follows:

javascript
var component = createComponent.shallow(
  
    
    
    
  );

And the result would be a representation of the following pseudo-HTML:

xml

To find Lisa you could use any of the following:

javascript
var lisaByAttr         = component.findByQuery('Person[name=Lisa]')[0];
var lisaByTagAndOrder  = component.findByQuery('Person')[1];
var lisaByCompAndOrder = component.findByComponent(Person)[1];

And access the properties as usual:

javascript
expect(lisaByAttr.prop('name')).toEqual('Lisa');

Interleaved rendering

This rendering mode is similar to the deep mode above with the exception that components are NOT erased form the HTML representation. This means that you can mix and match HTML tags and react components in your findByQuery selectors.

To use interleaved rendering call createComponent.interleaved as follows:

javascript
var component = createComponent.interleaved(
  
    
    
    
  );

The result would be a representation of the following pseudo-HTML:

xml

  

        
      

      

        
          

        
      

      

        
          

        
      

    

  

And you can find components with:

javascript
var lisaComp    = component.findByQuery('Person[name=Lisa]')[0];
var lisaCompAlt = component.findByComponent(Person)[2];

var lisaName    = component.findByQuery('Person[name=Lisa] h1')[0];
var lisaNameAlt = lisaComp.findByQuery('h1')[0];

Excluding components

Using exclude you can now leave a component out of test as if it didn't exist.

javascript
//single component
createComponent.exclude(ChildComponent)(ParentComponent);
createComponent.exclude(ChildComponent).shallow(ParentComponent);
createComponent.exclude(ChildComponent).interleaved(ParentComponent);

//multi components
createComponent.exclude([ChildComponent1, ChildComponent2])(ParentComponent);
createComponent.exclude([ChildComponent1, ChildComponent2]).shallow(ParentComponent);
createComponent.exclude([ChildComponent1, ChildComponent2]).interleaved(ParentComponent);

Mocking components

Using mock you can now replace a component with another

javascript
//single mock
createComponent.mock(Actual, Mock)(ParentComponent);

//multi mock
createComponent
  .mock(Actual1, Mock1)
  .mock(Actual2, Mock2)(ParentComponent);

Implicit context

If you want to test components that use React's implicit [context](https://facebook.github.io/react/d

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言