Skip to content

Commit 5b17a41

Browse files
committed
Improve behavior during dry-run when using Cypress globals
This fixes #1120 [1]. [1] #1120
1 parent 93eaacd commit 5b17a41

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
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+
- Mock and imitate Cypress globals during diagnostics / dry run, fixes [#1120](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1120).
8+
59
## v19.1.0
610

711
- Add `BeforeAll(..)` and `AfterAll(..)` hooks, fixes [#758](https://github.com/badeball/cypress-cucumber-preprocessor/issues/758).

docs/diagnostics.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,23 @@ This supports some of the same flags as Cypress, including
1616
The intention here being that whatever flags you use to run `cypress run` can also be consumed by the executable to give appropriate diagnostics.
1717

1818
> :warning: This feature is especially experimental, it's subject to change anytime and is not considered under semver.
19+
20+
## Limitations
21+
22+
In order to obtain structured information about step definitions, these files are resolved and evaluated in a Node environment. This environment differs from the normal Cypress environment in that it's not a browser environment and Cypress globals are mocked and imitated to some degree.
23+
24+
This means that expressions such as that shown below will work.
25+
26+
```ts
27+
import { Given } from "@badeball/cypress-cucumber-preprocessor";
28+
29+
const foo = Cypress.env("foo");
30+
31+
Given("a step", () => {
32+
if (foo) {
33+
// ...
34+
}
35+
});
36+
```
37+
38+
However, other may not. Cypress globals are mocked on a best-effort and need-to-have basis. If you're code doesn't run correctly during diagnostics, you may open up an issue on the tracker.

features/diagnostics.feature

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,77 @@ Feature: diagnostics
215215
│ a step │ cypress/e2e/a.feature:3 │
216216
└────────────────┴─────────────────────────────────────────────┘
217217
"""
218+
219+
Rule: it should works despite accessing a variety of globals on root-level
220+
221+
Scenario: Cypress.env
222+
Given a file named "cypress/e2e/a.feature" with:
223+
"""
224+
Feature: a feature name
225+
Scenario: a scenario name
226+
Given a step
227+
"""
228+
And a file named "cypress/support/step_definitions/steps.js" with:
229+
"""
230+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
231+
const foo = Cypress.env("foo");
232+
Given("a step", function() {});
233+
"""
234+
When I run diagnostics
235+
Then the output should contain
236+
"""
237+
┌────────────────┬─────────────────────────────────────────────┐
238+
│ Pattern / Text │ Location │
239+
├────────────────┼─────────────────────────────────────────────┤
240+
│ 'a step' │ cypress/support/step_definitions/steps.js:3 │
241+
│ a step │ cypress/e2e/a.feature:3 │
242+
└────────────────┴─────────────────────────────────────────────┘
243+
"""
244+
245+
Scenario: Cypress.on
246+
Given a file named "cypress/e2e/a.feature" with:
247+
"""
248+
Feature: a feature name
249+
Scenario: a scenario name
250+
Given a step
251+
"""
252+
And a file named "cypress/support/step_definitions/steps.js" with:
253+
"""
254+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
255+
Cypress.on("uncaught:exception", () => {});
256+
Given("a step", function() {});
257+
"""
258+
When I run diagnostics
259+
Then the output should contain
260+
"""
261+
┌────────────────┬─────────────────────────────────────────────┐
262+
│ Pattern / Text │ Location │
263+
├────────────────┼─────────────────────────────────────────────┤
264+
│ 'a step' │ cypress/support/step_definitions/steps.js:3 │
265+
│ a step │ cypress/e2e/a.feature:3 │
266+
└────────────────┴─────────────────────────────────────────────┘
267+
"""
268+
269+
Scenario: Cypress.config
270+
Given a file named "cypress/e2e/a.feature" with:
271+
"""
272+
Feature: a feature name
273+
Scenario: a scenario name
274+
Given a step
275+
"""
276+
And a file named "cypress/support/step_definitions/steps.js" with:
277+
"""
278+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
279+
const foo = Cypress.config("foo");
280+
Given("a step", function() {});
281+
"""
282+
When I run diagnostics
283+
Then the output should contain
284+
"""
285+
┌────────────────┬─────────────────────────────────────────────┐
286+
│ Pattern / Text │ Location │
287+
├────────────────┼─────────────────────────────────────────────┤
288+
│ 'a step' │ cypress/support/step_definitions/steps.js:3 │
289+
│ a step │ cypress/e2e/a.feature:3 │
290+
└────────────────┴─────────────────────────────────────────────┘
291+
"""

lib/diagnostics/diagnose.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,17 @@ export async function diagnose(configuration: {
161161
);
162162
}
163163

164-
registry = withRegistry(true, () => {
165-
(globalThis as any).Cypress = {};
164+
const cypressMockGlobals = {
165+
Cypress: {
166+
env() {},
167+
on() {},
168+
config() {},
169+
},
170+
};
171+
172+
Object.assign(globalThis, cypressMockGlobals);
166173

174+
registry = withRegistry(true, () => {
167175
try {
168176
require(outputFileName);
169177
} catch (e: unknown) {

0 commit comments

Comments
 (0)