Skip to content

Commit d72224a

Browse files
committed
Correctly filter test cases when using omitFiltered
This fixes #1018 [1]. [1] #1018
1 parent a499f76 commit d72224a

File tree

4 files changed

+165
-44
lines changed

4 files changed

+165
-44
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file.
66

77
- Allow generation of JSON reports with hooks (After / Before) even if `baseUrl` is undefined, fixes [#1017](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1017).
88

9+
- Correctly filter test cases in HTML reports when using `omitFiltered`, fixes [#1018](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1018).
10+
911
## v17.1.0
1012

1113
- Add support for (testing) type-specific configuration, fixes [#700](https://github.com/badeball/cypress-cucumber-preprocessor/issues/700).

features/reporters/html.feature

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,87 @@ Feature: html report
4040
When I run cypress
4141
Then it passes
4242
And the report should display when last run
43+
44+
Rule: it should obey `omitFiltered`
45+
Background:
46+
Given additional preprocessor configuration
47+
"""
48+
{
49+
"omitFiltered": true
50+
}
51+
"""
52+
53+
Scenario: without tags
54+
Given a file named "cypress/e2e/a.feature" with:
55+
"""
56+
Feature: a feature
57+
Scenario: a scenario
58+
Given a step
59+
Scenario: another scenario
60+
Given a step
61+
"""
62+
And a file named "cypress/support/step_definitions/steps.js" with:
63+
"""
64+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
65+
Given("a step", function() {})
66+
"""
67+
When I run cypress
68+
Then it should appear as if both tests ran
69+
And the HTML should display 2 executed scenarios
70+
71+
Scenario: with user-provided tags
72+
Given a file named "cypress/e2e/a.feature" with:
73+
"""
74+
Feature: a feature
75+
@foobar
76+
Scenario: a scenario
77+
Given a step
78+
Scenario: another scenario
79+
Given a step
80+
"""
81+
And a file named "cypress/support/step_definitions/steps.js" with:
82+
"""
83+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
84+
Given("a step", function() {})
85+
"""
86+
When I run cypress with "--env tags=@foobar"
87+
Then it should appear as if only a single test ran
88+
And the HTML should display 1 executed scenario
89+
90+
Scenario: @focus
91+
Given a file named "cypress/e2e/a.feature" with:
92+
"""
93+
Feature: a feature
94+
@focus
95+
Scenario: a scenario
96+
Given a step
97+
Scenario: another scenario
98+
Given a step
99+
"""
100+
And a file named "cypress/support/step_definitions/steps.js" with:
101+
"""
102+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
103+
Given("a step", function() {})
104+
"""
105+
When I run cypress
106+
Then it should appear as if only a single test ran
107+
And the HTML should display 1 executed scenario
108+
109+
Scenario: @skip
110+
Given a file named "cypress/e2e/a.feature" with:
111+
"""
112+
Feature: a feature
113+
Scenario: a scenario
114+
Given a step
115+
@skip
116+
Scenario: another scenario
117+
Given a step
118+
"""
119+
And a file named "cypress/support/step_definitions/steps.js" with:
120+
"""
121+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
122+
Given("a step", function() {})
123+
"""
124+
When I run cypress
125+
Then it should appear as if only a single test ran
126+
And the HTML should display 1 executed scenario

features/step_definitions/html_steps.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,48 @@ Then("the report should display when last run", async function () {
2222
{ runScripts: "dangerously" }
2323
);
2424

25-
const dt = Array.from(dom.window.document.querySelectorAll("dt")).find(
26-
(el) => el.textContent === "last run"
25+
const dt = assertAndReturn(
26+
Array.from(dom.window.document.querySelectorAll("dt")).find(
27+
(el) => el.textContent === "last run"
28+
),
29+
"Expected to find a 'last run' dt"
2730
);
2831

29-
assert(dt, "Expected to find a 'last run' dt");
30-
31-
const dd = dt.parentElement?.querySelector("dd");
32-
33-
assert(dd, "Expected to find a 'last run' dt's dd");
34-
35-
const lastRunText = dd.textContent;
32+
const dd = assertAndReturn(
33+
dt.parentElement?.querySelector("dd"),
34+
"Expected to find a 'last run' dt's dd"
35+
);
3636

37-
assert(lastRunText, "Expected to find 'XX seconds ago'");
37+
const lastRunText = assertAndReturn(
38+
dd.textContent,
39+
"Expected to find 'XX seconds ago'"
40+
);
3841

3942
assert.match(lastRunText, /\d+ seconds? ago/);
4043
});
4144

45+
Then(
46+
"the HTML should display {int} executed scenario(s)",
47+
async function (n: number) {
48+
const dom = await JSDOM.fromFile(
49+
path.join(this.tmpDir, "cucumber-report.html"),
50+
{ runScripts: "dangerously" }
51+
);
52+
53+
const dt = assertAndReturn(
54+
Array.from(dom.window.document.querySelectorAll("dt")).find(
55+
(el) => el.textContent && /\d+ executed/.test(el.textContent)
56+
),
57+
"Expected to find a 'XX executed' dt"
58+
);
59+
60+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61+
const actual = parseInt(dt.textContent!, 10);
62+
63+
assert.equal(actual, n);
64+
}
65+
);
66+
4267
Then("the report should have an image attachment", async function () {
4368
const dom = await JSDOM.fromFile(
4469
path.join(this.tmpDir, "cucumber-report.html"),

lib/browser-runtime.ts

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function createPickle(context: CompositionContext, pickle: messages.Pickle) {
297297
...afterHooks.map((hook) => ({ hook })),
298298
];
299299

300-
if (!testFilter.evaluate(tags) || tags.includes("@skip")) {
300+
if (shouldSkipPickle(testFilter, pickle)) {
301301
if (!context.omitFiltered) {
302302
it.skip(scenarioName);
303303
}
@@ -565,6 +565,12 @@ function createTestFilter(
565565
}
566566
}
567567

568+
function shouldSkipPickle(testFilter: Node, pickle: messages.Pickle) {
569+
const tags = collectTagNames(pickle.tags);
570+
571+
return !testFilter.evaluate(tags) || tags.includes("@skip");
572+
}
573+
568574
function beforeHandler(context: CompositionContext) {
569575
if (!retrieveInternalSuiteProperties()?.isEventHandlersAttached) {
570576
fail(
@@ -749,6 +755,8 @@ export default function createTests(
749755

750756
registry.finalize(newId);
751757

758+
const testFilter = createTestFilter(gherkinDocument, Cypress.env());
759+
752760
const stepDefinitions: messages.StepDefinition[] =
753761
registry.stepDefinitions.map((stepDefinition) => {
754762
const type: messages.StepDefinitionPatternType =
@@ -766,42 +774,46 @@ export default function createTests(
766774
};
767775
});
768776

769-
const testCases: messages.TestCase[] = pickles.map((pickle) => {
770-
const tags = collectTagNames(pickle.tags);
771-
const beforeHooks = registry.resolveBeforeHooks(tags);
772-
const afterHooks = registry.resolveAfterHooks(tags);
773-
774-
const hooksToStep = (hook: IHook): messages.TestStep => {
775-
return {
776-
id: hook.id,
777-
hookId: hook.id,
777+
const testCases: messages.TestCase[] = pickles
778+
.filter((pickle) => {
779+
return !omitFiltered || !shouldSkipPickle(testFilter, pickle);
780+
})
781+
.map((pickle) => {
782+
const tags = collectTagNames(pickle.tags);
783+
const beforeHooks = registry.resolveBeforeHooks(tags);
784+
const afterHooks = registry.resolveAfterHooks(tags);
785+
786+
const hooksToStep = (hook: IHook): messages.TestStep => {
787+
return {
788+
id: hook.id,
789+
hookId: hook.id,
790+
};
778791
};
779-
};
780792

781-
const pickleStepToTestStep = (
782-
pickleStep: messages.PickleStep
783-
): messages.TestStep => {
784-
const stepDefinitionIds = registry
785-
.getMatchingStepDefinitions(pickleStep.text)
786-
.map((stepDefinition) => stepDefinition.id);
793+
const pickleStepToTestStep = (
794+
pickleStep: messages.PickleStep
795+
): messages.TestStep => {
796+
const stepDefinitionIds = registry
797+
.getMatchingStepDefinitions(pickleStep.text)
798+
.map((stepDefinition) => stepDefinition.id);
799+
800+
return {
801+
id: pickleStep.id,
802+
pickleStepId: pickleStep.id,
803+
stepDefinitionIds,
804+
};
805+
};
787806

788807
return {
789-
id: pickleStep.id,
790-
pickleStepId: pickleStep.id,
791-
stepDefinitionIds,
808+
id: pickle.id,
809+
pickleId: pickle.id,
810+
testSteps: [
811+
...beforeHooks.map(hooksToStep),
812+
...pickle.steps.map(pickleStepToTestStep),
813+
...afterHooks.map(hooksToStep),
814+
],
792815
};
793-
};
794-
795-
return {
796-
id: pickle.id,
797-
pickleId: pickle.id,
798-
testSteps: [
799-
...beforeHooks.map(hooksToStep),
800-
...pickle.steps.map(pickleStepToTestStep),
801-
...afterHooks.map(hooksToStep),
802-
],
803-
};
804-
});
816+
});
805817

806818
const specEnvelopes: messages.Envelope[] = [];
807819

@@ -848,8 +860,6 @@ export default function createTests(
848860
});
849861
}
850862

851-
const testFilter = createTestFilter(gherkinDocument, Cypress.env());
852-
853863
const context: CompositionContext = {
854864
registry,
855865
newId,

0 commit comments

Comments
 (0)