Skip to content

Commit 475f4ad

Browse files
committed
Allow doesFeatureMatch to be called in support files
This feature broke during 084897f of #908 [1]. This patch fixes #1025 [2]. [1] #908 [2] #1025
1 parent baeb520 commit 475f4ad

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

CHANGELOG.md

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

99
- Fix error in non-feature specs under certain conditions, fixes [#1028](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1028).
1010

11+
- Allow doesFeatureMatch to be called in support files, fixes [#1025](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1025).
12+
1113
## v17.2.0
1214

1315
- Add BeforeStep and AfterStep hooks, fixes [#847](https://github.com/badeball/cypress-cucumber-preprocessor/issues/847).

features/issues/908.feature

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ Feature: hide internals from cypress environment
66
"""
77
Feature: a feature
88
Scenario: hide internal state by default
9-
Then the visible internal state should be a mere reference
9+
Then the visible internal state should be stringified to a replacement text
1010
"""
1111
And a file named "cypress/support/step_definitions/steps.js" with:
1212
"""
1313
const { Then } = require("@badeball/cypress-cucumber-preprocessor");
14-
Then("the visible internal state should be a mere reference", () => {
15-
const properties = Cypress.env("__cypress_cucumber_preprocessor_dont_use_this_spec");
16-
expect(properties).to.be.a("number");
14+
Then("the visible internal state should be stringified to a replacement text", () => {
15+
const {
16+
__cypress_cucumber_preprocessor_dont_use_this_spec: internalProperties
17+
} = JSON.parse(JSON.stringify(Cypress.env()));
18+
19+
expect(internalProperties).to.equal("Internal properties of cypress-cucumber-preprocessor omitted from report.");
1720
});
1821
"""
1922
When I run cypress

features/mixing_types.feature

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Feature: mixing feature and non-feature specs
1+
Feature: mixing feature and non-feature specs: API
22
Background:
33
Given additional Cypress configuration
44
"""
@@ -9,7 +9,7 @@ Feature: mixing feature and non-feature specs
99
}
1010
"""
1111

12-
Scenario: one feature and non-feature specs
12+
Scenario: feature
1313
Given a file named "cypress/e2e/a.feature" with:
1414
"""
1515
@foo
@@ -25,13 +25,33 @@ Feature: mixing feature and non-feature specs
2525
expect(doesFeatureMatch("@foo")).to.be.true;
2626
});
2727
"""
28-
And a file named "cypress/e2e/b.spec.js" with:
28+
And a file named "cypress/support/e2e.js" with:
2929
"""
30-
const { isFeature } = require("@badeball/cypress-cucumber-preprocessor");
30+
const { isFeature, doesFeatureMatch } = require("@badeball/cypress-cucumber-preprocessor");
31+
beforeEach(() => {
32+
expect(isFeature()).to.be.true;
33+
expect(doesFeatureMatch("@foo")).to.be.true;
34+
});
35+
"""
36+
When I run cypress
37+
Then it passes
38+
39+
Scenario: non-feature
40+
Given a file named "cypress/e2e/a.spec.js" with:
41+
"""
42+
const { isFeature, doesFeatureMatch } = require("@badeball/cypress-cucumber-preprocessor");
3143
it("should work", () => {
3244
expect(isFeature()).to.be.false;
45+
expect(doesFeatureMatch).to.throw("Expected to find internal properties, but didn't. This is likely because you're calling doesFeatureMatch() in a non-feature spec. Use doesFeatureMatch() in combination with isFeature() if you have both feature and non-feature specs");
46+
});
47+
"""
48+
And a file named "cypress/support/e2e.js" with:
49+
"""
50+
const { isFeature, doesFeatureMatch } = require("@badeball/cypress-cucumber-preprocessor");
51+
beforeEach(() => {
52+
expect(isFeature()).to.be.false;
53+
expect(doesFeatureMatch).to.throw("Expected to find internal properties, but didn't. This is likely because you're calling doesFeatureMatch() in a non-feature spec. Use doesFeatureMatch() in combination with isFeature() if you have both feature and non-feature specs");
3354
});
3455
"""
3556
When I run cypress
3657
Then it passes
37-
And it should appear to have ran spec "a.feature" and "b.spec.js"

lib/browser-runtime.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,37 +94,25 @@ interface IStep {
9494
pickleStep?: messages.PickleStep;
9595
}
9696

97+
const internalPropertiesReplacementText =
98+
"Internal properties of cypress-cucumber-preprocessor omitted from report.";
99+
97100
export interface InternalSpecProperties {
98101
pickle: messages.Pickle;
99102
testCaseStartedId: string;
100103
currentStepStartedAt?: StrictTimestamp;
101104
currentStep?: IStep;
102105
allSteps: IStep[];
103106
remainingSteps: IStep[];
107+
toJSON(): typeof internalPropertiesReplacementText;
104108
}
105109

106110
export interface InternalSuiteProperties {
107111
isEventHandlersAttached?: boolean;
108112
}
109113

110-
let specId = 0;
111-
112-
const internalSpecProperties = new Map<number, InternalSpecProperties>();
113-
114-
function createInternalSpecProperties(
115-
properties: InternalSpecProperties
116-
): number {
117-
internalSpecProperties.set(++specId, properties);
118-
return specId;
119-
}
120-
121114
export function retrieveInternalSpecProperties(): InternalSpecProperties {
122-
const reference = Cypress.env(INTERNAL_SPEC_PROPERTIES) as number;
123-
124-
return assertAndReturn(
125-
internalSpecProperties.get(reference),
126-
`Expected to find internal spec properties with reference = ${reference}`
127-
);
115+
return Cypress.env(INTERNAL_SPEC_PROPERTIES) as InternalSpecProperties;
128116
}
129117

130118
function updateInternalSpecProperties(
@@ -315,11 +303,11 @@ function createPickle(context: CompositionContext, pickle: messages.Pickle) {
315303
testCaseStartedId: context.newId(),
316304
allSteps: steps,
317305
remainingSteps: [...steps],
306+
toJSON: () => internalPropertiesReplacementText,
318307
};
319308

320309
const internalEnv = {
321-
[INTERNAL_SPEC_PROPERTIES]:
322-
createInternalSpecProperties(internalProperties),
310+
[INTERNAL_SPEC_PROPERTIES]: internalProperties,
323311
};
324312

325313
const suiteOptions = tags

0 commit comments

Comments
 (0)