Skip to content

Commit 298eeec

Browse files
committed
Mutate "less destructively"
The value that is sometimes mutated by addCucumberPreprocessorPlugin(), is read by compile() as it attempts to calculate a common ancestor path of all the files. It would erroneously use the filtered specs to determine this value and this obviously adversely affected the ability to resolve step definitions. This fixes #785.
1 parent 5aa1ede commit 298eeec

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
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
- Correct some path handling on Windows, fixes [#788](https://github.com/badeball/cypress-cucumber-preprocessor/issues/788).
1010

11+
- Correct calculation of common ancestor path, even when specs are filtered, fixes [#785](https://github.com/badeball/cypress-cucumber-preprocessor/issues/785).
12+
1113
## v12.0.0
1214

1315
Breaking changes:

features/issues/785.feature

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# https://github.com/badeball/cypress-cucumber-preprocessor/issues/785
2+
3+
Feature: omit filtered tests, creating a different common ancestor path
4+
Background:
5+
Given additional preprocessor configuration
6+
"""
7+
{
8+
"stepDefinitions": "cypress/e2e/[filepath].js",
9+
"filterSpecs": true
10+
}
11+
"""
12+
And a file named "cypress/e2e/foo/bar.feature" with:
13+
"""
14+
Feature: a feature
15+
Scenario: a scenario
16+
Given a step
17+
"""
18+
And a file named "cypress/e2e/foo/bar.js" with:
19+
"""
20+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
21+
Given("a step", () => {})
22+
"""
23+
And a file named "cypress/e2e/baz/qux.feature" with:
24+
"""
25+
@qux
26+
Feature: a feature
27+
Scenario: a scenario
28+
Given a step
29+
"""
30+
And a file named "cypress/e2e/baz/qux.js" with:
31+
"""
32+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
33+
Given("a step", () => {})
34+
"""
35+
36+
Scenario: no filtering
37+
When I run cypress
38+
Then it passes
39+
40+
Scenario: with filtering
41+
When I run cypress with "--env tags=@qux"
42+
Then it passes

lib/add-cucumber-preprocessor-plugin.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727

2828
import {
2929
HOOK_FAILURE_EXPR,
30+
INTERNAL_PROPERTY_NAME,
3031
TASK_APPEND_MESSAGES,
3132
TASK_CREATE_STRING_ATTACHMENT,
3233
TASK_TEST_STEP_STARTED,
@@ -268,6 +269,29 @@ type AddOptions = {
268269
omitAfterScreenshotHandler?: boolean;
269270
};
270271

272+
type PreservedPluginConfigOptions = ICypressConfiguration & {
273+
[INTERNAL_PROPERTY_NAME]?: Partial<ICypressConfiguration>;
274+
};
275+
276+
export function mutateConfigObjectPreservingly<
277+
K extends keyof ICypressConfiguration
278+
>(
279+
config: PreservedPluginConfigOptions,
280+
property: K,
281+
value: PreservedPluginConfigOptions[K]
282+
) {
283+
const preserved =
284+
config[INTERNAL_PROPERTY_NAME] ?? (config[INTERNAL_PROPERTY_NAME] = {});
285+
preserved[property] = config[property];
286+
config[property] = value;
287+
}
288+
289+
export function rebuildOriginalConfigObject(
290+
config: PreservedPluginConfigOptions
291+
): ICypressConfiguration {
292+
return Object.assign({}, config, config[INTERNAL_PROPERTY_NAME]);
293+
}
294+
271295
export default async function addCucumberPreprocessorPlugin(
272296
on: Cypress.PluginEvents,
273297
config: Cypress.PluginConfigOptions,
@@ -345,9 +369,7 @@ export default async function addCucumberPreprocessorPlugin(
345369
if (tags !== null && preprocessor.filterSpecs) {
346370
const node = parse(tags);
347371

348-
const propertyName = "specPattern" in config ? "specPattern" : "testFiles";
349-
350-
(config as any)[propertyName] = getTestFiles(
372+
const testFiles = getTestFiles(
351373
config as unknown as ICypressConfiguration
352374
).filter((testFile) => {
353375
const content = syncFs.readFileSync(testFile).toString("utf-8");
@@ -374,6 +396,14 @@ export default async function addCucumberPreprocessorPlugin(
374396
node.evaluate(pickle.tags?.map((tag) => tag.name).filter(notNull) ?? [])
375397
);
376398
});
399+
400+
const propertyName = "specPattern" in config ? "specPattern" : "testFiles";
401+
402+
mutateConfigObjectPreservingly(
403+
config,
404+
propertyName as keyof ICypressConfiguration,
405+
testFiles
406+
);
377407
}
378408

379409
return config;

lib/template.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { notNull } from "./type-guards";
2424

2525
import { ensureIsRelative } from "./helpers";
2626

27+
import { rebuildOriginalConfigObject } from "./add-cucumber-preprocessor-plugin";
28+
2729
const { stringify } = JSON;
2830

2931
export async function compile(
@@ -32,6 +34,8 @@ export async function compile(
3234
data: string,
3335
uri: string = this.resourcePath
3436
) {
37+
configuration = rebuildOriginalConfigObject(configuration);
38+
3539
const options = {
3640
includeSource: false,
3741
includeGherkinDocument: true,

0 commit comments

Comments
 (0)