Skip to content

Commit f4521c3

Browse files
committed
Move default stepDefinitions determination to PreprocessorConfiguration
1 parent 0888442 commit f4521c3

File tree

6 files changed

+114
-53
lines changed

6 files changed

+114
-53
lines changed

lib/add-cucumber-preprocessor-plugin.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ let currentTestStepStartedId: string;
5454
let currentSpecMessages: messages.IEnvelope[];
5555

5656
export async function beforeRunHandler(config: Cypress.PluginConfigOptions) {
57-
const preprocessor = await resolve(config.projectRoot, config.env);
57+
const preprocessor = await resolve(config, config.env);
5858

5959
if (!preprocessor.messages.enabled) {
6060
return;
@@ -69,7 +69,7 @@ export async function beforeRunHandler(config: Cypress.PluginConfigOptions) {
6969
}
7070

7171
export async function afterRunHandler(config: Cypress.PluginConfigOptions) {
72-
const preprocessor = await resolve(config.projectRoot, config.env);
72+
const preprocessor = await resolve(config, config.env);
7373

7474
if (!preprocessor.json.enabled) {
7575
return;
@@ -136,7 +136,7 @@ export async function afterSpecHandler(
136136
spec: Cypress.Spec,
137137
results: CypressCommandLine.RunResult
138138
) {
139-
const preprocessor = await resolve(config.projectRoot, config.env);
139+
const preprocessor = await resolve(config, config.env);
140140

141141
const messagesPath = ensureIsAbsolute(
142142
config.projectRoot,
@@ -176,7 +176,7 @@ export async function afterScreenshotHandler(
176176
config: Cypress.PluginConfigOptions,
177177
details: Cypress.ScreenshotDetails
178178
) {
179-
const preprocessor = await resolve(config.projectRoot, config.env);
179+
const preprocessor = await resolve(config, config.env);
180180

181181
if (!preprocessor.messages.enabled || !currentSpecMessages) {
182182
return details;
@@ -218,7 +218,7 @@ export default async function addCucumberPreprocessorPlugin(
218218
config: Cypress.PluginConfigOptions,
219219
options: AddOptions = {}
220220
) {
221-
const preprocessor = await resolve(config.projectRoot, config.env);
221+
const preprocessor = await resolve(config, config.env);
222222

223223
if (!options.omitBeforeRunHandler) {
224224
on("before:run", () => beforeRunHandler(config));

lib/preprocessor-configuration.test.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
import { ICypressPost10Configuration } from "@badeball/cypress-configuration";
2+
13
import assert from "assert";
24

35
import { resolve } from "./preprocessor-configuration";
46

7+
const DUMMY_POST10_CONFIG: ICypressPost10Configuration = {
8+
projectRoot: "",
9+
specPattern: [],
10+
excludeSpecPattern: [],
11+
env: {},
12+
};
13+
514
describe("resolve()", () => {
615
it("overriding stepDefinitions", async () => {
716
const { stepDefinitions } = await resolve(
8-
"/foo/bar",
17+
DUMMY_POST10_CONFIG,
918
{ stepDefinitions: "foo/bar/**" },
1019
() => null
1120
);
@@ -16,7 +25,7 @@ describe("resolve()", () => {
1625
it("overriding messages.enabled (1)", async () => {
1726
const {
1827
messages: { enabled },
19-
} = await resolve("/foo/bar", { messagesEnabled: "" }, () => ({
28+
} = await resolve(DUMMY_POST10_CONFIG, { messagesEnabled: "" }, () => ({
2029
messages: { enabled: true },
2130
}));
2231

@@ -26,7 +35,7 @@ describe("resolve()", () => {
2635
it("overriding messages.enabled (2)", async () => {
2736
const {
2837
messages: { enabled },
29-
} = await resolve("/foo/bar", { messagesEnabled: "true" }, () => ({
38+
} = await resolve(DUMMY_POST10_CONFIG, { messagesEnabled: "true" }, () => ({
3039
messages: { enabled: false },
3140
}));
3241

@@ -36,17 +45,21 @@ describe("resolve()", () => {
3645
it("overriding messages.enabled (3)", async () => {
3746
const {
3847
messages: { enabled },
39-
} = await resolve("/foo/bar", { messagesEnabled: "foobar" }, () => ({
40-
messages: { enabled: false },
41-
}));
48+
} = await resolve(
49+
DUMMY_POST10_CONFIG,
50+
{ messagesEnabled: "foobar" },
51+
() => ({
52+
messages: { enabled: false },
53+
})
54+
);
4255

4356
assert.strictEqual(enabled, true);
4457
});
4558

4659
it("overriding messages.enabled (4)", async () => {
4760
const {
4861
messages: { enabled },
49-
} = await resolve("/foo/bar", { messagesEnabled: true }, () => ({
62+
} = await resolve(DUMMY_POST10_CONFIG, { messagesEnabled: true }, () => ({
5063
messages: { enabled: false },
5164
}));
5265

@@ -56,27 +69,35 @@ describe("resolve()", () => {
5669
it("overriding messages.enabled (5)", async () => {
5770
const {
5871
messages: { enabled },
59-
} = await resolve("/foo/bar", { messagesEnabled: "false" }, () => ({
60-
messages: { enabled: true },
61-
}));
72+
} = await resolve(
73+
DUMMY_POST10_CONFIG,
74+
{ messagesEnabled: "false" },
75+
() => ({
76+
messages: { enabled: true },
77+
})
78+
);
6279

6380
assert.strictEqual(enabled, false);
6481
});
6582

6683
it("overriding messages.enabled (6)", async () => {
6784
const {
6885
messages: { enabled },
69-
} = await resolve("/foo/bar", { messagesEnabled: "false" }, () => ({
70-
messages: { enabled: true },
71-
}));
86+
} = await resolve(
87+
DUMMY_POST10_CONFIG,
88+
{ messagesEnabled: "false" },
89+
() => ({
90+
messages: { enabled: true },
91+
})
92+
);
7293

7394
assert.strictEqual(enabled, false);
7495
});
7596

7697
it("overriding messages.enabled (7)", async () => {
7798
const {
7899
messages: { enabled },
79-
} = await resolve("/foo/bar", { messagesEnabled: false }, () => ({
100+
} = await resolve(DUMMY_POST10_CONFIG, { messagesEnabled: false }, () => ({
80101
messages: { enabled: true },
81102
}));
82103

lib/preprocessor-configuration.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { ICypressConfiguration } from "@badeball/cypress-configuration";
2+
13
import { cosmiconfig } from "cosmiconfig";
24

35
import util from "util";
46

57
import debug from "./debug";
68

9+
import { ensureIsRelative } from "./helpers";
10+
711
import { isString, isStringOrStringArray, isBoolean } from "./type-guards";
812

913
function hasOwnProperty<X extends {}, Y extends string>(
@@ -287,7 +291,7 @@ export function stringToMaybeBoolean(value: string): boolean | undefined {
287291
}
288292

289293
export interface IPreprocessorConfiguration {
290-
readonly stepDefinitions?: string | string[];
294+
readonly stepDefinitions: string | string[];
291295
readonly messages?: {
292296
enabled: boolean;
293297
output?: string;
@@ -329,14 +333,31 @@ export const DEFAULT_POST_10_STEP_DEFINITIONS = [
329333
export class PreprocessorConfiguration implements IPreprocessorConfiguration {
330334
constructor(
331335
private explicitValues: Partial<IPreprocessorConfiguration>,
332-
private environmentOverrides: IEnvironmentOverrides
336+
private environmentOverrides: IEnvironmentOverrides,
337+
private cypressConfiguration: ICypressConfiguration
333338
) {}
334339

335340
get stepDefinitions() {
336-
return (
341+
const explicit =
337342
this.environmentOverrides.stepDefinitions ??
338-
this.explicitValues.stepDefinitions
339-
);
343+
this.explicitValues.stepDefinitions;
344+
345+
if (explicit) {
346+
return explicit;
347+
}
348+
349+
const config = this.cypressConfiguration;
350+
351+
if ("specPattern" in config) {
352+
return DEFAULT_POST_10_STEP_DEFINITIONS;
353+
} else {
354+
return DEFAULT_PRE_10_STEP_DEFINITIONS.map((pattern) =>
355+
pattern.replace(
356+
"[integration-directory]",
357+
ensureIsRelative(config.projectRoot, config.integrationFolder)
358+
)
359+
);
360+
}
340361
}
341362

342363
get messages() {
@@ -403,11 +424,11 @@ export type ConfigurationFileResolver = (
403424
) => any | Promise<any>;
404425

405426
export async function resolve(
406-
projectRoot: string,
427+
cypressConfig: ICypressConfiguration,
407428
environment: Record<string, unknown>,
408429
configurationFileResolver: ConfigurationFileResolver = cosmiconfigResolver
409430
) {
410-
const result = await configurationFileResolver(projectRoot);
431+
const result = await configurationFileResolver(cypressConfig.projectRoot);
411432

412433
const environmentOverrides = validateEnvironmentOverrides(environment);
413434

@@ -429,10 +450,18 @@ export async function resolve(
429450

430451
debug(`resolved configuration ${util.inspect(config)}`);
431452

432-
return new PreprocessorConfiguration(config, environmentOverrides);
453+
return new PreprocessorConfiguration(
454+
config,
455+
environmentOverrides,
456+
cypressConfig
457+
);
433458
} else {
434459
debug("resolved no configuration");
435460

436-
return new PreprocessorConfiguration({}, environmentOverrides);
461+
return new PreprocessorConfiguration(
462+
{},
463+
environmentOverrides,
464+
cypressConfig
465+
);
437466
}
438467
}

lib/step-definitions.test.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,27 @@ import {
1818
pathParts,
1919
} from "./step-definitions";
2020

21+
const DUMMY_PRE10_CONFIG: ICypressPre10Configuration = {
22+
projectRoot: "",
23+
integrationFolder: "",
24+
fixturesFolder: "",
25+
supportFile: false,
26+
testFiles: [],
27+
ignoreTestFiles: [],
28+
env: {},
29+
};
30+
2131
function pre10example(
2232
filepath: string,
23-
cypressConfiguration: Pick<
24-
ICypressPre10Configuration,
25-
"projectRoot" | "integrationFolder"
26-
>,
33+
partialCypressConfiguration: Partial<ICypressPre10Configuration>,
2734
preprocessorConfiguration: Partial<IPreprocessorConfiguration>,
2835
expected: string[]
2936
) {
37+
const cypressConfiguration: ICypressPre10Configuration = {
38+
...DUMMY_PRE10_CONFIG,
39+
...partialCypressConfiguration,
40+
};
41+
3042
it(`should return [${expected.join(
3143
", "
3244
)}] for ${filepath} with ${util.inspect(preprocessorConfiguration)} in ${
@@ -37,7 +49,8 @@ function pre10example(
3749
cypress: cypressConfiguration,
3850
preprocessor: new PreprocessorConfiguration(
3951
preprocessorConfiguration,
40-
{}
52+
{},
53+
cypressConfiguration
4154
),
4255
},
4356
filepath
@@ -61,12 +74,24 @@ function pre10example(
6174
});
6275
}
6376

77+
const DUMMY_POST10_CONFIG: ICypressPost10Configuration = {
78+
projectRoot: "",
79+
specPattern: [],
80+
excludeSpecPattern: [],
81+
env: {},
82+
};
83+
6484
function post10example(
6585
filepath: string,
66-
cypressConfiguration: Pick<ICypressPost10Configuration, "projectRoot">,
86+
partialCypressConfiguration: Partial<ICypressPost10Configuration>,
6787
preprocessorConfiguration: Partial<IPreprocessorConfiguration>,
6888
expected: string[]
6989
) {
90+
const cypressConfiguration: ICypressPost10Configuration = {
91+
...DUMMY_POST10_CONFIG,
92+
...partialCypressConfiguration,
93+
};
94+
7095
it(`should return [${expected.join(
7196
", "
7297
)}] for ${filepath} with ${util.inspect(preprocessorConfiguration)} in ${
@@ -77,7 +102,8 @@ function post10example(
77102
cypress: cypressConfiguration,
78103
preprocessor: new PreprocessorConfiguration(
79104
preprocessorConfiguration,
80-
{}
105+
{},
106+
cypressConfiguration
81107
),
82108
},
83109
filepath

lib/step-definitions.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ export function getStepDefinitionPatternsPost10(
108108

109109
debug(`replacing [filepart] with ${util.inspect(parts)}`);
110110

111-
const stepDefinitions = configuration.preprocessor.stepDefinitions
112-
? [configuration.preprocessor.stepDefinitions].flat()
113-
: DEFAULT_POST_10_STEP_DEFINITIONS;
111+
const stepDefinitions = [configuration.preprocessor.stepDefinitions].flat();
114112

115113
debug(`looking for step definitions using ${util.inspect(stepDefinitions)}`);
116114

@@ -165,17 +163,7 @@ export function getStepDefinitionPatternsPre10(
165163

166164
debug(`replacing [filepart] with ${util.inspect(parts)}`);
167165

168-
const stepDefinitions = configuration.preprocessor.stepDefinitions
169-
? [configuration.preprocessor.stepDefinitions].flat()
170-
: DEFAULT_PRE_10_STEP_DEFINITIONS.map((pattern) =>
171-
pattern.replace(
172-
"[integration-directory]",
173-
ensureIsRelative(
174-
configuration.cypress.projectRoot,
175-
configuration.cypress.integrationFolder
176-
)
177-
)
178-
);
166+
const stepDefinitions = [configuration.preprocessor.stepDefinitions].flat();
179167

180168
debug(`looking for step definitions using ${util.inspect(stepDefinitions)}`);
181169

lib/template.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ export async function compile(
4949

5050
const pickles = envelopes.map((envelope) => envelope.pickle).filter(notNull);
5151

52-
const preprocessor = await resolve(
53-
configuration.projectRoot,
54-
configuration.env
55-
);
52+
const preprocessor = await resolve(configuration, configuration.env);
5653

5754
const stepDefinitions = await getStepDefinitionPaths(
5855
{

0 commit comments

Comments
 (0)