@cucumber/react

中文文档
19.2.0发布于 2022/03/25MITTypeScriptCommonJS近 30 天下载 1.3万

React components for Cucumber

此软件包已被标记为弃用

This package is now published under @cucumber/react-components

原始使用文档
文档目录20 节
  1. Screenshot
  2. Usage
  3. <GherkinDocument> features
  4. Styling
  5. Built-in themes
  6. Custom themes
  7. Custom styles
  8. Custom rendering
  9. Build / hack
  10. Ideas
  11. ScenarioList component
  12. Link to JIRA
  13. Search
  14. Search results
  15. Filtering / sorting
  16. Tag search
  17. On-demand data
  18. Server / App
  19. Rerun tests
  20. Alerts

Cucumber-React

Cucumber-React is a set of React components for rendering Gherkin documents and Cucumber results.

Screenshot

Examples Tables

Usage

The source code for screenshots above is:

<EnvelopesWrapper envelopes={envelopes}>
  <GherkinDocumentList />
</EnvelopesWrapper>

The <GherkinDocumentList> React component, is an accordion of <GherkinDocument>.

The <GherkinDocument> React component and any component nested within it (such as <Scenario>) can be rendered standalone.

<GherkinDocument> features

The <GherkinDocument> React component is instantiated with a single gherkinDocument prop. The value must be a GherkinDocument object. You can use the Gherkin parser to generate a GherkinDocument object.

By default the <GherkinDocument> component will not display any coloured results, as the GherkinDocument message object does not contain results, only the AST of the document. This is fine for simple use cases where results are not important.

To render a <GherkinDocument> with results and highlighted Cucumber Expression parameters parameters it must be nested inside a <Wrapper> component.

Styling

The standard styling comes from wrapping your top-level usage with the CucumberReact component (sans-props). There are several ways you can apply different styling to the components.

Built-in themes

These are the built-in themes:

  • light (default)
  • dark
  • auto (honours your operating system preference for either light or dark)

You can activate one of these by passing the theme prop to the CucumberReact component:

<CucumberReact theme="dark">
  <GherkinDocument />
</CucumberReact>

Custom themes

You can also provide your own theme with a small amount of CSS. Pass the CucumberReact component a class name instead of a theme name:

<CucumberReact className="acme-widgets">
  <GherkinDocument />
</CucumberReact>

In your CSS for the acme-widgets class, you can override the supported custom property values as desired. Here's the CSS that drives the built-in "dark" theme:

.darkTheme {
  --cucumber-background-color: #1d1d26;
  --cucumber-text-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 a customRendering prop to the CucumberReact component, in the form of an object that declares which class names you're going to override and what with:

<CucumberReact customRendering={{
  DocString: {
    docString: 'acme-docstring'
  }
}}>
  <GherkinDocument />
</CucumberReact>

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 customRendering prop, but this time instead of an object with class names, you provide a React functional component, giving you full control over the rendering:

<CucumberReact customRendering={{
  DocString: (props) => (
    <>
      <p>I am going to render this doc string in a textarea:</p>
      <textarea>{props.docString.content}</textarea>
    </>
  )
}}>
  <GherkinDocument />
</CucumberReact>

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)

Build / hack

Install dependencies

npm install

Run tests

npm test

Interactive development

npm run storybook

Ideas

ScenarioList component

A component that renders a list of scenarios (possibly from multiple files, filtered by e.g. tag).

This component could be used to render relevant scenarios in 3rd-party tools, such as JIRA, Confluence and various issue trackers that support plugins.

Configure with a regexp and url function, and tags will be rendered as JIRA issue links

Search by tag, but also by text. Could use http://elasticlunr.com/ or https://lunrjs.com/ - or it could simply perform filtering on an array of GherkinDocument messages.

Search results

Each scenario displayed underneath each other, grouped by feature file. The feature description is "collapsed", (unless it contains the search term) but can be opened.

Filtering / sorting

  • by tag
  • by duration (find slow ones)
  • by status
  • by recency (update timestamp) - exclude old ones
  • by flickeriness
  • Render a tag cloud for all tags
    • Size: count
    • Color: pass/fail/undefined

On-demand data

For large reports (especially with screenshots) it may be too heavy to store it all in the browser. The GUI should request data for the current document on demand. The GUI should also be able to filter what kind of events it wants. For example, to render the initial screen.

Server / App

It should be easy to use. Just run the app (Electron). It will create a named pipe where it will listen. What's written here gets written straight to the React app (no websocket, it's in the same process). This app can be fairly small.

Rerun tests

Add a message to represent a config+cwd+env for a run, so the GUI can rerun it. The config is essentially command line options. They can be modified in the gui. Rerun on file change can also be set up. This just makes the whole DX simple.

Alerts

The app could use the OS to send screen messages (autotest like)

版本与发布记录

@cucumber/react 共发布 38 个版本,首次发布于 2020/01/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
19.2.0latest2022/03/254 年前
19.1.12022/03/234 年前
19.1.02022/03/234 年前
19.0.02022/01/254 年前
18.1.22021/09/085 年前
18.1.12021/09/085 年前

关于 @cucumber/react 的常见问题

@cucumber/react 是什么?

React components for Cucumber注意:该包已被作者标记为弃用——This package is now published under @cucumber/react-components

如何安装 @cucumber/react?

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

@cucumber/react 支持 TypeScript 吗?

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

@cucumber/react 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cucumber/react 可以商用吗?

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

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

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

@cucumber/react 还在维护吗?

最新版本 19.2.0 发布于 2022/03/25(4 年前);至今共发布 38 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/01/10。作者已将其标记为弃用,新项目建议寻找替代方案。