Skip to content

Commit 6d3ba51

Browse files
committed
Support config file locations other than project root
This fixes #1243 [1]. [1] #1243
1 parent a8b0926 commit 6d3ba51

6 files changed

+75
-44
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## Unreleased
6+
7+
- Support config file locations other than project root, fixes [#1243](https://github.com/badeball/cypress-cucumber-preprocessor/discussions/1243).
8+
59
## v21.0.0
610

711
Breaking changes:

declarations.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ declare module "find-cypress-specs" {
44
export function getSpecs(
55
config: Cypress.ConfigOptions,
66
type: Cypress.TestingType,
7+
returnAbsolute?: boolean,
78
): string[];
89

9-
export function getSpecs(config: Cypress.PluginConfigOptions): string[];
10+
export function getSpecs(
11+
config: Cypress.PluginConfigOptions,
12+
type: Cypress.TestingType,
13+
returnAbsolute?: boolean,
14+
): string[];
1015

1116
export function getConfig(): Cypress.ConfigOptions;
1217
}

lib/add-cucumber-preprocessor-plugin.ts

+41-39
Original file line numberDiff line numberDiff line change
@@ -138,46 +138,48 @@ export async function addCucumberPreprocessorPlugin(
138138

139139
const node = parse(tags);
140140

141-
const testFiles = getSpecs(config).filter((testFile) => {
142-
if (!testFile.endsWith(".feature")) {
143-
switch (preprocessor.filterSpecsMixedMode) {
144-
case "hide":
145-
return false;
146-
case "show":
147-
return true;
148-
case "empty-set":
149-
return node.evaluate([]);
150-
default:
151-
assertNever(preprocessor.filterSpecsMixedMode);
141+
const testFiles = getSpecs(config, "foobar" as any, true).filter(
142+
(testFile) => {
143+
if (!testFile.endsWith(".feature")) {
144+
switch (preprocessor.filterSpecsMixedMode) {
145+
case "hide":
146+
return false;
147+
case "show":
148+
return true;
149+
case "empty-set":
150+
return node.evaluate([]);
151+
default:
152+
assertNever(preprocessor.filterSpecsMixedMode);
153+
}
152154
}
153-
}
154-
155-
const content = fs.readFileSync(testFile).toString("utf-8");
156-
157-
const options = {
158-
includeSource: false,
159-
includeGherkinDocument: false,
160-
includePickles: true,
161-
newId: IdGenerator.incrementing(),
162-
};
163-
164-
const envelopes = generateMessages(
165-
content,
166-
testFile,
167-
SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN,
168-
options,
169-
);
170-
171-
const pickles = envelopes
172-
.map((envelope) => envelope.pickle)
173-
.filter(notNull);
174-
175-
return pickles.some((pickle) =>
176-
node.evaluate(
177-
pickle.tags?.map((tag) => tag.name).filter(notNull) ?? [],
178-
),
179-
);
180-
});
155+
156+
const content = fs.readFileSync(testFile).toString("utf-8");
157+
158+
const options = {
159+
includeSource: false,
160+
includeGherkinDocument: false,
161+
includePickles: true,
162+
newId: IdGenerator.incrementing(),
163+
};
164+
165+
const envelopes = generateMessages(
166+
content,
167+
testFile,
168+
SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN,
169+
options,
170+
);
171+
172+
const pickles = envelopes
173+
.map((envelope) => envelope.pickle)
174+
.filter(notNull);
175+
176+
return pickles.some((pickle) =>
177+
node.evaluate(
178+
pickle.tags?.map((tag) => tag.name).filter(notNull) ?? [],
179+
),
180+
);
181+
},
182+
);
181183

182184
debug(`Resolved specs ${inspect(testFiles)}`);
183185

lib/step-definitions.ts

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ export function getStepDefinitionPatterns(
5656
>,
5757
filepath: string,
5858
): string[] {
59+
assert(
60+
path.isAbsolute(configuration.implicitIntegrationFolder),
61+
`Expected an absolute path for implicit integration folder but got ${configuration.implicitIntegrationFolder}`,
62+
);
63+
64+
assert(
65+
path.isAbsolute(filepath),
66+
`Expected an absolute path for spec but got ${filepath}`,
67+
);
68+
5969
const filepathReplacement = glob.escape(
6070
trimFeatureExtension(
6171
path.relative(configuration.implicitIntegrationFolder, filepath),

lib/subpath-entrypoints/esbuild.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ export function createEsbuildPlugin(
7575
if (needPrettify) {
7676
debug("esbuild: prettifying sources");
7777

78-
/**
79-
*/
8078
sourceMap.sources = sourceMap.sources.map((source: string) => {
8179
return path.relative(
8280
configuration.projectRoot,
@@ -95,9 +93,17 @@ export function createEsbuildPlugin(
9593
"base64",
9694
);
9795

96+
/**
97+
* Why `${"sourceMappingURL"}` you may ask. This is so esbuild doesn't crap itself upon
98+
* errors, where it would search for source maps and find THIS code line, which is not a
99+
* valid source map (obvously).
100+
*
101+
* Without this, esbuild would error with "Unexpected token z in JSON at position 0" every
102+
* time an error occurred during build time.
103+
*/
98104
await fs.appendFile(
99105
outfile,
100-
`//# sourceMappingURL=data:application/json;base64,${encoded}\n`,
106+
`//# ${"sourceMappingURL"}=data:application/json;base64,${encoded}\n`,
101107
);
102108
});
103109
}

lib/template.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ export async function compile(
7171
const pickles = envelopes.map((envelope) => envelope.pickle).filter(notNull);
7272

7373
const implicitIntegrationFolder = assertAndReturn(
74-
ancestor(...getSpecs(configuration).map(path.dirname).map(path.normalize)),
74+
ancestor(
75+
...getSpecs(configuration, "foobar" as any, true)
76+
.map(path.dirname)
77+
.map(path.normalize),
78+
),
7579
"Expected to find a common ancestor path",
7680
);
7781

0 commit comments

Comments
 (0)