@cucumber/react-components

中文文档
24.5.0发布于 2026/09/28MITTypeScriptES Module构建来源已验证近 30 天下载 5131

React components for Cucumber

原始使用文档
文档目录6 节
  1. Usage
  2. Attachments
  3. Styling
  4. Theming
  5. Custom styles
  6. Custom rendering

@cucumber/react-components

A set of React components for rendering Gherkin documents and Cucumber results.

Preview and examples:
https://react-components-9pg.pages.dev/

Examples Tables

Usage

  • This package is distributed in ES module format only.
  • React 18 or above is required.

The source code for the screenshot above is:

import { EnvelopesProvider, FilteredDocuments } from '@cucumber/react-components'

<EnvelopesProvider envelopes={envelopes}>
  <FilteredDocuments />
</EnvelopesProvider>

envelopes should be an array of Cucumber Messages that came from a test run. You can obtain these from most Cucumber implementations by using a "Message" formatter.

Attachments

Attachments from test runs are shown with their corresponding steps. The baseline behavior for attachments is a download button. However, we have some special handling for common MIME types to make them more useful without leaving the report:

  • image/* - images are rendered with an <img/> tag
  • video/* - videos are rendered with a <video/ tag
  • text/x.cucumber.log+plain - logs (from calls to the log function in Cucumber) are rendered as monospace text, and support ANSI colors
  • text/uri-list - one or more URLs are rendered as links that open in a new tab
  • application/json - JSON is rendered as monospace text and prettified
  • text/* - other text types are rendered as monospace text

Styling

The standard styling comes without any extra providers or configuration. There are several ways you can apply different styling to the components.

Theming

You can provide your own theme with a small amount of CSS. Target the element above your highest-level usage of the components:

<div className="dark-theme">
  <EnvelopesProvider envelopes={envelopes}>
    <FilteredDocuments />
  </EnvelopesProvider>
</div>

In your CSS, you can set the background and text colors, and override the supported custom property values as desired:

.dark-theme {
  background-color: #1d1d26;
  color: #c9c9d1;
  --cucumber-anchor-color: #4caaee;
  --cucumber-keyword-color: #d89077;
  --cucumber-parameter-color: #4caaee;
  --cucumber-tag-color: #85a658;
  --cucumber-docstring-color: #66a565;
  --cucumber-error-background-color: #cf6679;
  --cucumber-error-text-color: #222;
  --cucumber-code-background-color: #282a36;
  --cucumber-code-text-color: #f8f8f2;
  --cucumber-panel-background-color: #282a36;
  --cucumber-panel-accent-color: #313442;
  --cucumber-panel-text-color: #f8f8f2;
}

Custom styles

For more control over the styling, you can override the CSS used by individual components.

Let's say you want to do something totally different with the typography of doc strings. In your own CSS, you might write something like:

.acme-docstring {
  font-weight: bold;
  font-style: italic;
  background-color: black;
  color: hotpink;
  text-shadow: 1px 1px 2px white;
  padding: 10px;
}

Then, you can provide an overrides prop to the CustomRendering component, in the form of an object that declares which class names you're going to override and what with:

<CustomRendering overrides={{
  DocString: {
    docString: 'acme-docstring'
  }
}}>
  <EnvelopesProvider envelopes={envelopes}>
    <FilteredDocuments />
  </EnvelopesProvider>
</CustomRendering>

Some components have multiple styling hooks - e.g. the <Tags> component has the tags class name for the list, and the tag class name for each item. In these cases, you can provide custom class names for just the ones you want to change, and any you omit will pick up the built-in styling like normal.

Custom rendering

To change the rendering of some components entirely, you can selectively provide your own component implementations to be used instead of the built-in ones.

Staying with the doc string example, you can use the same overrides prop, but this time instead of an object with class names, you provide a React functional component, giving you full control over the rendering:

<CustomRendering overrides={{
  DocString: (props) => (
    <>
      <p>I am going to render this doc string in a textarea:</p>
      <textarea>{props.docString.content}</textarea>
    </>
  )
}}>
  <EnvelopesProvider envelopes={envelopes}>
    <FilteredDocuments />
  </EnvelopesProvider>
</CustomRendering>

In each case where you provide your own component, it will receive the same props as the default component, plus two more:

  • styles - class names for the default styling, so you can still apply these to your custom component if it makes sense
  • DefaultRenderer - the default React component, useful if you only want to provide your own rendering for certain cases, and otherwise fall back to the default rendering (don't forget to pass it the props)

版本与发布记录

@cucumber/react-components 共发布 29 个版本,首次发布于 2022/04/12;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
24.5.0latest2026/09/281 个月前
24.4.02026/09/032 个月前
24.3.02026/04/136 个月前
24.2.02026/01/319 个月前
24.1.22025/11/2911 个月前
24.1.12025/11/2511 个月前

关于 @cucumber/react-components 的常见问题

@cucumber/react-components 是什么?

React components for Cucumber

如何安装 @cucumber/react-components?

在项目目录运行 npm install @cucumber/react-components,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)、react-dom(^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cucumber/react-components 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cucumber/react-components 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cucumber/react-components 可以商用吗?

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

@cucumber/react-components 的下载量有多少?

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

@cucumber/react-components 还在维护吗?

最新版本 24.5.0 发布于 2026/09/28(1 个月前);至今共发布 29 个版本,其中过去 12 个月发布了 9 个;首次发布于 2022/04/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。