Skip to content

This precompiler can load external data (JSON, CSV, or XLS/XLSX) into exmaples table.

License

Notifications You must be signed in to change notification settings

gherking/gpc-test-data

Repository files navigation

gpc-test-data

Downloads Version@npm Version@git CI Docs

This precompiler can load external data (JSON, CSV, or XLS/XLSX) into the examples table.

Usage

'use strict';
const compiler = require('gherking');
const { default: TestData } = require('gpc-test-data');

let ast = await compiler.load('./features/src/login.feature');
ast = await compiler.process(
  ast,
  new TestData({
    defaultValue: "-"
  })
);
await compiler.save('./features/dist/login.feature', ast, {
  lineBreak: '\r\n'
});
'use strict';
import {load, process, save} from "gherking";
import TestData from "gpc-test-data";

let ast = await load("./features/src/login.feature");
ast = await process(
  ast,
  new TestData({
    defaultValue: "-"
  })
);
await save('./features/dist/login.feature', ast, {
  lineBreak: '\r\n'
});

Tags

In the feature file, for each Examples, one of the following tags can be set:

  • @load_json(path) to load the data from a JSON file
  • @load_csv(path) to load the data from a CSV file
  • @load_xls(path) to load the data from an XLS/XLSX file, from the first sheet
  • @load_xls(path,sheet) to load the data from an XLS/XLSX file, from the given sheet
  • @load_http(url) to load the data from an HTTP source (a JSON response of an object array)

For each tag, the path must be either a relative (from cwd) or an absolute path to the file, including the file name and extension.

Formats

There are some restrictions on the various data formats:

  • For JSON, the content of the JSON file should be an array of objects, where the key of the object properties will be mapped to the examples columns.
  • For CSV and XLS/XLSX, the first non-empty row must contain the header/column names, which will be mapped to the examples columns.
  • For CSV, the csv-parse tool is used to parse the CSV files, see its documentation for the particular specialties of its parsing and requirements for a CSV file (for parsing, an adaptive configuration is set - columns, skipping empty lines, trimming, auto-BOM, auto-delimiter).
  • For XLS/XLSX, the xlsx tool is used to parse the XLS/XLSX files, see its documentation for the particular specialties of its parsing and requirements for an XLS/XLSX file (for parsing, an adaptive configuration if set - removing blank rows, skipping hidden rows, parsing number; additionally the CSV parsing options from the previous point).

Configuration

The precompiler supports the following configuration options to be set:

Option Type Description Default
keepTag boolean Whether the load-tags should be kept or removed. false
defaultValue string|number The default value to be added to the table, if a column/value is not found. ""
appendData boolean Whether the loaded data should be appended to the existing rows of the examples table or overwritten. true
ignoreKeyCase boolean Whether the casing of the example columns and data columns should be ignored. true
addSourceComment boolean Whether a comment should be added to the example, indicating the source and the data type. false
allowEmptyData boolean Whether empty data files are allowed to be loaded, or error should be thrown. false

Other

This package uses debug for logging, use gpc:test-data :

DEBUG=gpc:test-data* gherking ...

For detailed documentation see the TypeDocs documentation.