Skip to content

Commit

Permalink
Allow doesFeatureMatch to be called in support files
Browse files Browse the repository at this point in the history
This feature broke during 084897f of #908 [1].

This patch fixes #1025 [2].

[1] #908
[2] #1025
  • Loading branch information
badeball committed Jun 2, 2023
1 parent baeb520 commit 8f790a5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.

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

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

## v17.2.0

- Add BeforeStep and AfterStep hooks, fixes [#847](https://github.com/badeball/cypress-cucumber-preprocessor/issues/847).
Expand Down
30 changes: 25 additions & 5 deletions features/mixing_types.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature: mixing feature and non-feature specs
Feature: mixing feature and non-feature specs: API
Background:
Given additional Cypress configuration
"""
Expand All @@ -9,7 +9,7 @@ Feature: mixing feature and non-feature specs
}
"""

Scenario: one feature and non-feature specs
Scenario: feature
Given a file named "cypress/e2e/a.feature" with:
"""
@foo
Expand All @@ -25,13 +25,33 @@ Feature: mixing feature and non-feature specs
expect(doesFeatureMatch("@foo")).to.be.true;
});
"""
And a file named "cypress/e2e/b.spec.js" with:
And a file named "cypress/support/e2e.js" with:
"""
const { isFeature } = require("@badeball/cypress-cucumber-preprocessor");
const { isFeature, doesFeatureMatch } = require("@badeball/cypress-cucumber-preprocessor");
beforeEach(() => {
expect(isFeature()).to.be.true;
expect(doesFeatureMatch("@foo")).to.be.true;
});
"""
When I run cypress
Then it passes

Scenario: non-feature
Given a file named "cypress/e2e/a.spec.js" with:
"""
const { isFeature, doesFeatureMatch } = require("@badeball/cypress-cucumber-preprocessor");
it("should work", () => {
expect(isFeature()).to.be.false;
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");
});
"""
And a file named "cypress/support/e2e.js" with:
"""
const { isFeature, doesFeatureMatch } = require("@badeball/cypress-cucumber-preprocessor");
beforeEach(() => {
expect(isFeature()).to.be.false;
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");
});
"""
When I run cypress
Then it passes
And it should appear to have ran spec "a.feature" and "b.spec.js"
26 changes: 7 additions & 19 deletions lib/browser-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,37 +94,25 @@ interface IStep {
pickleStep?: messages.PickleStep;
}

const internalPropertiesReplacementText =
"Internal properties of cypress-cucumber-preprocessor omitted from report.";

export interface InternalSpecProperties {
pickle: messages.Pickle;
testCaseStartedId: string;
currentStepStartedAt?: StrictTimestamp;
currentStep?: IStep;
allSteps: IStep[];
remainingSteps: IStep[];
toJSON(): typeof internalPropertiesReplacementText;
}

export interface InternalSuiteProperties {
isEventHandlersAttached?: boolean;
}

let specId = 0;

const internalSpecProperties = new Map<number, InternalSpecProperties>();

function createInternalSpecProperties(
properties: InternalSpecProperties
): number {
internalSpecProperties.set(++specId, properties);
return specId;
}

export function retrieveInternalSpecProperties(): InternalSpecProperties {
const reference = Cypress.env(INTERNAL_SPEC_PROPERTIES) as number;

return assertAndReturn(
internalSpecProperties.get(reference),
`Expected to find internal spec properties with reference = ${reference}`
);
return Cypress.env(INTERNAL_SPEC_PROPERTIES) as InternalSpecProperties;
}

function updateInternalSpecProperties(
Expand Down Expand Up @@ -315,11 +303,11 @@ function createPickle(context: CompositionContext, pickle: messages.Pickle) {
testCaseStartedId: context.newId(),
allSteps: steps,
remainingSteps: [...steps],
toJSON: () => internalPropertiesReplacementText,
};

const internalEnv = {
[INTERNAL_SPEC_PROPERTIES]:
createInternalSpecProperties(internalProperties),
[INTERNAL_SPEC_PROPERTIES]: internalProperties,
};

const suiteOptions = tags
Expand Down

0 comments on commit 8f790a5

Please sign in to comment.