@commercetools/enzyme-extensions

中文文档
5.0.0发布于 2019/06/13MITESM + CJS近 30 天下载 7567

Enzyme extensions tailored at improving dealing with shallowly rendered wrappers

原始使用文档
文档目录7 节
  1. What assumptions is this built with?
  2. Installation
  3. 1. Add package
  4. 2. Add a test setup file (test runner dependent)
  5. 3. Extend Enzyme with this package's helpers
  6. Usage
  7. Documentation

commercetools logo @commercetools/enzyme-extensions

Logo

CircleCI Status Codecov Coverage Status Downloads per Week Made with Coffee

Why should you use this? Read: Test a Render Prop!

NOTE This package used to provide a renderProp test helper, which is now part of enzyme itself as of v3.8.0 🎈.

We therefore dropped renderProp in v4.0.0 of this package. We recommend to use renderProp from enzyme itself instead.

Be aware that the API has changed while moving the function to enzyme.

// before
wrapper.renderProp('foo', 10, 20);
// after
wrapper.renderProp('foo')(10, 20);

party-parrot We are happy that our little helper has made it into enzyme.

What assumptions is this built with?

  • We like to shallow render and avoid mounting
    • 🤺 Shallow rendering is fast and ensures that you only interact with the unit under test
    • 🏙 Shallow rendering ensures that you do not snapshot past your test's concern
    • 🏎 Shallow rendering has shown to be more performant for us than mounting
  • We like declarative components and Render Props
    • 🧠 We can compose components easily while following along their interactions
    • 🔪 We like stubbing to test individual pieces of logic

Installation

1. Add package

yarn add @commercetools/enzyme-extensions -D

2. Add a test setup file (test runner dependent)

For Jest you would set up a setupTestFrameworkScriptFile. Create that file and add it to the jest configuration.

3. Extend Enzyme with this package's helpers

In that testFrameworkScriptFile file, import the extensions and add them to Enzyme

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-xx';
import configure from '@commercetools/enzyme-extensions';
import ShallowWrapper from 'enzyme/ShallowWrapper';

// You likely had this part already
Enzyme.configure({ adapter: new Adapter() });

// This is the actual integration.
// Behind the scenes this extends the prototype of the passed in `ShallowWrapper`
configure(ShallowWrapper);

Usage

Once set up, you can use the extension in your test files like this:

import React from 'react';
import { shallow } from 'enzyme';

describe('when rendering `<App>`', () => {
  const App = () => (
    <div id="app">
      <Mouse
        render={(x, y) => (
          <div>
            Cursor is at {x} {y}
          </div>
        )}
      />
    </div>
  );

  // Here we call the render function defined on Mouse and we provide
  // some custom arguments to it. This means we are effectively mocking
  // the Mouse component's implementation.
  // This is great to keep test concerns separate.
  const wrapper = shallow(<App />)
    .find(Mouse)
    // This is where we are actually using the drill function
    // Since we defined it on the prototype in the Installation step,
    // it does not need to be imported into the test itself.
    // We can call any property dynamically and even derive the property to
    // call depending on the props which are passed as the arguments of the
    // function passed to `drill`.
    .drill(props => props.render(10, 20));

  it('should render the mouse position', () => {
    expect(wrapper.equals(<div>Cursor is at 10 20</div>)).toBe(true);
  });
});

Enzyme's renderProp is built as an easy to use test helper for the most common cases. In case you need more control, you can use drill instead. drill offers more flexibility as:

  • the prop-to-call can be derived from the other props
  • the returned element can be set dynamically

See the drill documentation for more.

Documentation

版本与发布记录

@commercetools/enzyme-extensions 共发布 11 个版本,首次发布于 2018/05/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.0latest2019/06/137 年前
4.0.02018/12/187 年前
3.0.12018/05/258 年前
3.0.02018/05/258 年前
2.0.0next2018/05/228 年前
1.3.12018/05/188 年前

关于 @commercetools/enzyme-extensions 的常见问题

@commercetools/enzyme-extensions 是什么?

Enzyme extensions tailored at improving dealing with shallowly rendered wrappers

如何安装 @commercetools/enzyme-extensions?

在项目目录运行 npm install @commercetools/enzyme-extensions,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:enzyme(3.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@commercetools/enzyme-extensions 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/commercetools__enzyme-extensions 是否存在。

@commercetools/enzyme-extensions 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@commercetools/enzyme-extensions 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@commercetools/enzyme-extensions 可以商用吗?

@commercetools/enzyme-extensions 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@commercetools/enzyme-extensions 的下载量有多少?

根据 npm 官方下载统计,@commercetools/enzyme-extensions 在 2026/10/11 – 2026/11/09 这 30 天内被下载 7,567 次,日均约 252 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@commercetools/enzyme-extensions 还在维护吗?

最新版本 5.0.0 发布于 2019/06/13(7 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/05/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。