@cucumber-stories/cucumber-datatable

中文文档
3.0.1发布于 2023/04/11MITTypeScriptCommonJS零依赖近 30 天下载 2.4万

Cucumber DataTable is a library to simplify and enhance data tables in gherkins feature files.

原始使用文档
文档目录8 节
  1. Installation
  2. What is it?
  3. A sample is better than 100 words:
  4. How to use it?
  5. Converters
  6. Development
  7. Running tests
  8. Publish a new version

Cucumber DataTable

Node.js CI

Installation

npm i @cucumber-stories/cucumber-datatable

What is it?

Cucumber DataTable is a library to simplify and enhance data tables in gherkins feature files. By providing schema for your data table you can easily pass from silly strings to structured and typed data.

A sample is better than 100 words:

Imagine you want to handle a gherkin table like this:

Feature: Heroes sample

  Scenario: A simple heroes table
    Given the following heroes
      | Name         | Age |
      | Bruce Wayne  | 83  |
      | Tony Stark   | 52  |
      | Bruce Banner | 53  |

Thanks to @cucumber-stories/cucumber-datatable write this...

import { DataTable } from "@cucumber/cucumber";
import { cucumberDataTable } from "@cucumber-stories/cucumber-datatable";

const getHeroes = cucumberDataTable({
  name: { columnName: "Name", converter: Converters.String },
  age: { columnName: "Age", converter: Converters.Number },
});

Given(/^The following heroes$/, function (dataTable: DataTable) {
  const heroes = getHeroes(dataTable);
  console.log(heroes); // [{ name: 'Bruce Wayne', age: 83 }, ...]
});

...Instead of that

import { DataTable } from "@cucumber/cucumber";

Given(/^The following heroes$/, function (table: DataTable) {
  const lines = data.raw();
  // Here you have an hugly array of string arrays (string[][]) ☹️
});

How to use it?

First, you have to define a dictionary composed of 2 things. It has the same keys as output objects and attach to it the following metadata:

  • columnName: The column name in the gherkin file (.feature)
  • converter: The converter to use to transform the data from the gherkin data table

You have to use the cucumberDataTable() function to create a reusable function used in your steps to transform your data.

import { cucumberDataTable } from "@cucumber-stories/cucumber-datatable";

const dictionary = {
  name: {
    columnName: "Name", // Column name in Gherkin file
    converter: Converters.String, // Converter to use (more samples below)
  },
  age: {
    columnName: "Age",
    converter: Converters.Number,
  },
};

const getStructuredData = cucumberDataTable(dictionary);

// ....
Given(/^The following table/, function (dataTable: DataTable) {
  const data = getStructuredData(dataTable);
});

Converters

Converters are used to convert the data. Out of the box the library provides some useful converters.

Some of the converters can be configurable, you can have details on the Converter config section.

NameIn GherkinOutputDefault config
Converters.String"Foo""Foo"
Converters.Number"42"42
Converters.YesNoToBoolean"yes" or "no"true or false{ yes: "yes"; no: "no" }
Converters.StringArray"Foo, Bar"["Foo", "Bar"]{ separator: "," }
Converters.ObjectArray"Foo:42, Bar:32"see below{ propertySeparator: ":", itemSeparator: "," }
Converters.Nullable"<null>"see below{ nullValue: "<null>" }
Converters.CustomYour special formatting logicsee below

Converter config

If a converter needs configuration, you can call .withConfig method with the specified configuration, such as:

const dictionary = {
  tags: {
    columnName: "Tags",
    converter: Converters.StringArray.withConfig({
      separator: "-",
    }),
  },
};

Nullable

If some of your values need to be nullable, and want to be used as null in your code, you can use the Nullable converter.

const dictionary = {
  name: {
    columnName: "Name",
    converter: Converters.Nullable(Converters.String),
  },
};

// name will be string | null

You can also configure the null value, which default to <null>

const dictionary = {
  name: {
    columnName: "Name",
    converter: Converters.Nullable(Converters.String, {
      nullValue: "vide",
    }),
  },
};

// name will be string | null, when the value is "vide"

Object array converter

The Converters.ObjectArray() is a bit tricky, so it deserve its own part in our documentation.

Imagine the following gherkin file:

Feature: Heroes sample

  Scenario:
    Given the following attributes
      | Attributes     | Other |
      | 1:Color,2:Size | other |
      | 10:Name        | other |

If you want an output like this:

[
  {
    attributes: [
      { code: 1, name: "Color" },
      { code: 2, name: "Size" },
    ],
    other: "other",
  },
  {
    attributes: [{ code: 10, name: "Name" }],
    other: "other",
  },
];

You probably need the Converters.ObjectArray().

To use this converter you have to define:

  • the separator of different items
  • the separator of different properties of a same object

And then the schema of nested objects:

import {
  cucumberDataTable,
  Converters,
} from "@cucumber-stories/cucumber-datatable";

const dictionary = {
  attributes: {
    columnName: "Attributes",
    converter: Converters.ObjectArray({
      code: {
        position: 0,
        converter: Converters.Number,
      },
      name: {
        position: 1,
        converter: Converters.String,
      },
    }).withConfig({
      propertySeparator: ":",
      itemSeparator: ",",
    }),
  },
  // Other columns...
  other: {
    columnName: "Other",
    converter: Converters.String,
  },
};

const getStructuredData = cucumberDataTable(dictionary);

You don't have to specify .withConfig if you want to keep the default config which is { propertySeparator: ":", itemSeparator: "," }

Custom converter

To sum up, a converter is an instance of the Converter class. To create a custom converter to answer your need, you have to use Converter.Custom(<Your converter function>).

The converter function takes:

  1. a string in argument (the data from the gherkin file),
  2. an optional config as a second parameter,
  3. and returns what you want.

For example if you want to output a custom structure from a column you can do this:

import {
  cucumberDataTable,
  Converters,
} from "@cucumber-stories/cucumber-datatable";

const dictionary = {
  code: {
    columnName: "Code",
    converter: Converters.Custom((code: string) => ({ nested: code })),
  },
  configurableCode: {
    columnName: "Configurable code",
    converter: Converters.Custom(
      (code: string, config?: { append: string }) => code + config?.append
    ).withConfig({
      append: "-code",
    }),
  },
  // Other columns...
  other: {
    columnName: "Other",
    converter: Converters.String,
  },
};

const getStructuredData = cucumberDataTable(dictionary);

It will transform this table:

Feature: Heroes sample

  Scenario: A simple heroes table
    Given the following table
      | Code | Configurable code | Other |
      | 001  | 001               | other |
      | 002  | 001               | other |

Into:

[
  { code: { nested: "001" }, configurableCode: "001-code", other: "other" },
  { code: { nested: "002" }, configurableCode: "002-code", other: "other" },
];

Development

Running tests

Run npm test and npm run test-types

Publish a new version

  1. npm version <major|minor|patch>
  2. git push --tags
  3. Create a release on this tag.

版本与发布记录

@cucumber-stories/cucumber-datatable 共发布 7 个版本,首次发布于 2022/11/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.1latest2023/04/113 年前
3.0.02022/11/303 年前
2.0.02022/11/233 年前
1.2.02022/11/183 年前
1.1.02022/11/173 年前
1.0.12022/11/173 年前

关于 @cucumber-stories/cucumber-datatable 的常见问题

@cucumber-stories/cucumber-datatable 是什么?

Cucumber DataTable is a library to simplify and enhance data tables in gherkins feature files.

如何安装 @cucumber-stories/cucumber-datatable?

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

@cucumber-stories/cucumber-datatable 支持 TypeScript 吗?

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

@cucumber-stories/cucumber-datatable 支持 ESM 还是 CommonJS?

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

@cucumber-stories/cucumber-datatable 可以商用吗?

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

@cucumber-stories/cucumber-datatable 的下载量有多少?

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

@cucumber-stories/cucumber-datatable 还在维护吗?

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