Skip to content

Commit

Permalink
strip tag indicator character from mocha + cucumber.
Browse files Browse the repository at this point in the history
add support for key/value tags
  • Loading branch information
bryanbcook committed Nov 10, 2023
1 parent 4c16a43 commit 23933bd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
13 changes: 9 additions & 4 deletions src/parsers/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ function getTestCase(rawCase) {
test_case.name = rawCase["name"];
test_case.duration = rawCase["duration"];
if (rawCase.tags && rawCase.tags.length > 0) {
const rawtags = rawCase.tags;
const tagsArray = rawCase.tags;
let tags = [];
for (let i = 0; i < rawtags.length; i++) {
let tagName = rawtags[i]["name"];
test_case.meta_data.set(tagName, rawtags[i]["line"])
let rawTags = [];
for (let i = 0; i < tagsArray.length; i++) {
let rawTagName = tagsArray[i]["name"];
let tag = rawTagName.substring(1).split("=");
let tagName = tag[0];
test_case.meta_data.set(tagName, tag[1] ?? "")
tags.push(tagName);
rawTags.push(rawTagName);
}
test_case.meta_data.set("tags", tags.join(","));
test_case.meta_data.set("tagsRaw", rawTags.join(","));
}
if (rawCase.state && rawCase.state === "failed") {
test_case.status = 'FAIL';
Expand Down
11 changes: 8 additions & 3 deletions src/parsers/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ function getTestCase(rawCase) {
const regexp = /([\@\#][^\s]*)/gm; // match @tag or #tag
let matches = [...test_case.name.matchAll(regexp)];
if (matches.length > 0) {
let tags = []
let tags = [];
let rawTags = [];
for (let match of matches) {
let tagName = match[0];
test_case.meta_data.set(tagName, "");
let rawTag = match[0];
let tag = rawTag.substring(1).split("=");
let tagName = tag[0];
test_case.meta_data.set(tagName, tag[1] ?? "");
tags.push(tagName);
rawTags.push(rawTag);
}
test_case.meta_data.set("tags", tags.join(","));
test_case.meta_data.set("tagsRaw", rawTags.join(","));
}
if (rawCase["state"] == "pending") {
test_case.status = 'SKIP';
Expand Down
4 changes: 4 additions & 0 deletions tests/data/cucumber/single-suite-single-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
{
"name": "@fast",
"line": 4
},
{
"name": "@testCase=1234",
"line": 4
}
],
"type": "scenario"
Expand Down
2 changes: 1 addition & 1 deletion tests/parser.cucumber.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Parser - Cucumber Json', () => {
skipped: 0,
stack_trace: "",
status: "PASS",
meta_data: newMap({tags:"@green,@fast", "@green":4, "@fast":4}),
meta_data: newMap({tags:"green,fast,testCase", green: "", fast: "", testCase: "1234", tagsRaw:"@green,@fast,@testCase=1234"}),
steps: [],
total: 0
}
Expand Down
12 changes: 7 additions & 5 deletions tests/parser.mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,19 @@ describe('Parser - Mocha Json', () => {
const result = parse({ type: 'mocha', files: [`${testDataPath}/multiple-suites-multiple-tests-tags.json`] });
let testcase = result.suites[0].cases[0];
assert.equal(testcase.meta_data.has("tags"), true);
assert.equal(testcase.meta_data.get("tags"), "@fast,#1255")
assert.equal(testcase.meta_data.has("@fast"), true);
assert.equal(testcase.meta_data.has("#1255"), true);
assert.equal(testcase.meta_data.get("tags"), "fast,1255")
assert.equal(testcase.meta_data.get("tagsRaw"), "@fast,#1255")
assert.equal(testcase.meta_data.has("fast"), true);
assert.equal(testcase.meta_data.has("1255"), true);
});

it('has single tag', () => {
const result = parse({ type: 'mocha', files: [`${testDataPath}/multiple-suites-multiple-tests-tags.json`] });
let testcase = result.suites[1].cases[0];
assert.equal(testcase.meta_data.has("tags"), true);
assert.equal(testcase.meta_data.get("tags"), "#1234")
assert.equal(testcase.meta_data.has("#1234"), true);
assert.equal(testcase.meta_data.get("tags"), "1234")
assert.equal(testcase.meta_data.get("tagsRaw"), "#1234")
assert.equal(testcase.meta_data.has("1234"), true);
});

it('does not include tags meta if no tags are present', () =>{
Expand Down

0 comments on commit 23933bd

Please sign in to comment.