Skip to content

Commit

Permalink
Improve behavior during dry-run when using Cypress globals
Browse files Browse the repository at this point in the history
This fixes #1120 [1].

[1] #1120
  • Loading branch information
badeball committed Nov 16, 2023
1 parent 93eaacd commit 5b17a41
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## Unreleased

- Mock and imitate Cypress globals during diagnostics / dry run, fixes [#1120](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1120).

## v19.1.0

- Add `BeforeAll(..)` and `AfterAll(..)` hooks, fixes [#758](https://github.com/badeball/cypress-cucumber-preprocessor/issues/758).
Expand Down
20 changes: 20 additions & 0 deletions docs/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,23 @@ This supports some of the same flags as Cypress, including
The intention here being that whatever flags you use to run `cypress run` can also be consumed by the executable to give appropriate diagnostics.

> :warning: This feature is especially experimental, it's subject to change anytime and is not considered under semver.
## Limitations

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.

This means that expressions such as that shown below will work.

```ts
import { Given } from "@badeball/cypress-cucumber-preprocessor";

const foo = Cypress.env("foo");

Given("a step", () => {
if (foo) {
// ...
}
});
```

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.
74 changes: 74 additions & 0 deletions features/diagnostics.feature
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,77 @@ Feature: diagnostics
│ a step │ cypress/e2e/a.feature:3 │
└────────────────┴─────────────────────────────────────────────┘
"""

Rule: it should works despite accessing a variety of globals on root-level

Scenario: Cypress.env
Given a file named "cypress/e2e/a.feature" with:
"""
Feature: a feature name
Scenario: a scenario name
Given a step
"""
And a file named "cypress/support/step_definitions/steps.js" with:
"""
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
const foo = Cypress.env("foo");
Given("a step", function() {});
"""
When I run diagnostics
Then the output should contain
"""
┌────────────────┬─────────────────────────────────────────────┐
│ Pattern / Text │ Location │
├────────────────┼─────────────────────────────────────────────┤
│ 'a step' │ cypress/support/step_definitions/steps.js:3 │
│ a step │ cypress/e2e/a.feature:3 │
└────────────────┴─────────────────────────────────────────────┘
"""

Scenario: Cypress.on
Given a file named "cypress/e2e/a.feature" with:
"""
Feature: a feature name
Scenario: a scenario name
Given a step
"""
And a file named "cypress/support/step_definitions/steps.js" with:
"""
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
Cypress.on("uncaught:exception", () => {});
Given("a step", function() {});
"""
When I run diagnostics
Then the output should contain
"""
┌────────────────┬─────────────────────────────────────────────┐
│ Pattern / Text │ Location │
├────────────────┼─────────────────────────────────────────────┤
│ 'a step' │ cypress/support/step_definitions/steps.js:3 │
│ a step │ cypress/e2e/a.feature:3 │
└────────────────┴─────────────────────────────────────────────┘
"""

Scenario: Cypress.config
Given a file named "cypress/e2e/a.feature" with:
"""
Feature: a feature name
Scenario: a scenario name
Given a step
"""
And a file named "cypress/support/step_definitions/steps.js" with:
"""
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
const foo = Cypress.config("foo");
Given("a step", function() {});
"""
When I run diagnostics
Then the output should contain
"""
┌────────────────┬─────────────────────────────────────────────┐
│ Pattern / Text │ Location │
├────────────────┼─────────────────────────────────────────────┤
│ 'a step' │ cypress/support/step_definitions/steps.js:3 │
│ a step │ cypress/e2e/a.feature:3 │
└────────────────┴─────────────────────────────────────────────┘
"""
12 changes: 10 additions & 2 deletions lib/diagnostics/diagnose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,17 @@ export async function diagnose(configuration: {
);
}

registry = withRegistry(true, () => {
(globalThis as any).Cypress = {};
const cypressMockGlobals = {
Cypress: {
env() {},
on() {},
config() {},
},
};

Object.assign(globalThis, cypressMockGlobals);

registry = withRegistry(true, () => {
try {
require(outputFileName);
} catch (e: unknown) {
Expand Down

0 comments on commit 5b17a41

Please sign in to comment.