Skip to content

Commit 9c5c3ec

Browse files
omacrangerLogan Graham
and
Logan Graham
authored
[Cypress Cucumber] Migrate Cypress config to TypeScript, rework on limitation workaround (#773)
* Migrate Cypress config to TypeScript, rework `on` limitation workaround * update test file to ts * update config file location * update deps in saucectl config for typescript * Move test dependencies to devDependencies --------- Co-authored-by: Logan Graham <[email protected]>
1 parent 8bfbd6f commit 9c5c3ec

File tree

8 files changed

+3225
-1953
lines changed

8 files changed

+3225
-1953
lines changed

cypress-cucumber/.sauce/config.yml

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ sauce:
1010
build: Visual Demo
1111
cypress:
1212
version: 13.13.3 # See https://docs.saucelabs.com/web-apps/automated-testing/cypress/#supported-testing-platforms for a list of supported versions.
13-
configFile: "cypress.config.js"
13+
configFile: "cypress.config.ts"
1414

1515
npm:
1616
packages:
1717
"@saucelabs/cypress-visual-plugin": "^0.6.3"
1818
"@badeball/cypress-cucumber-preprocessor": "^20.1.2"
1919
"@bahmutov/cypress-esbuild-preprocessor": "^2.2.2"
20+
"typescript": "^5.4.3"
21+
"cypress-on-fix": "^1.0.3"
22+
"ts-node": "^10.9.2"
23+
"@tsconfig/node18": "^18.2.4"
2024

2125
rootDir: ./
2226
suites:
@@ -36,4 +40,4 @@ artifacts:
3640
when: never
3741
match:
3842
- console.log
39-
directory: ./artifacts/
43+
directory: ./artifacts/

cypress-cucumber/cypress-on-fix.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="cypress" />
2+
3+
declare module "cypress-on-fix"{
4+
declare function onProxy(on: Cypress.PluginEvents): Cypress.PluginEvents
5+
export = onProxy;
6+
}

cypress-cucumber/cypress.config.js

-67
This file was deleted.

cypress-cucumber/cypress.config.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { defineConfig } from 'cypress';
2+
import createBundler from '@bahmutov/cypress-esbuild-preprocessor';
3+
import { addCucumberPreprocessorPlugin } from '@badeball/cypress-cucumber-preprocessor';
4+
import { createEsbuildPlugin } from '@badeball/cypress-cucumber-preprocessor/esbuild';
5+
import { CypressSauceVisual, DiffingMethod } from '@saucelabs/cypress-visual-plugin';
6+
import cypressOnFix from "cypress-on-fix";
7+
8+
export default defineConfig({
9+
e2e: {
10+
saucelabs: {
11+
project: process.env.GITHUB_REPOSITORY || `Cypress Visual Testing Example for ${process.env.SAUCE_USERNAME}`,
12+
branch: process.env.GITHUB_REF_NAME,
13+
buildName: `Cypress - Sauce Demo Test`,
14+
region: 'us-west-1',
15+
diffingMethod: DiffingMethod.Balanced,
16+
},
17+
specPattern: "cypress/**/*.feature",
18+
supportFile: './cypress/support/e2e.ts',
19+
setupNodeEvents: async function(cypressOn, config) {
20+
// Cypress only allows a single listener for an event at a time. This package will proxy all
21+
// events to all listeners instead of overwriting it. Recommended by the maintainer of the
22+
// Cucumber support: https://github.com/badeball/cypress-cucumber-preprocessor/blob/master/docs/event-handlers.md#workaround-1--2
23+
24+
// Register Cypress's on listener, then use the return value to register plugin listeners
25+
// instead.
26+
const on = cypressOnFix(cypressOn);
27+
await addCucumberPreprocessorPlugin(on, config);
28+
on(
29+
'file:preprocessor',
30+
createBundler({
31+
plugins: [createEsbuildPlugin(config)],
32+
})
33+
);
34+
35+
CypressSauceVisual.register(on, config);
36+
return config;
37+
},
38+
},
39+
})

cypress-cucumber/cypress/e2e/saucedemo.js renamed to cypress-cucumber/cypress/e2e/saucedemo.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ When('on Sauce Demo Page', () => {
66
cy.visit('https://www.saucedemo.com/');
77
});
88

9-
Then('{string} should be visible', (selector) => {
9+
Then('{string} should be visible', (selector: string) => {
1010
cy.get(selector).should('be.visible');
1111
});
1212

13-
Then('I capture a screenshot named {string}', (screenshotName) => {
13+
Then('I capture a screenshot named {string}', (screenshotName: string) => {
1414
cy.sauceVisualCheck(screenshotName, {
1515
captureDom: true
1616
});
1717
});
1818

19-
Then('I log in as {string}', (username) => {
19+
Then('I log in as {string}', (username: string) => {
2020
let user = Cypress.env('VISUAL_CHECK') ? 'visual_user' : 'standard_user';
2121
if (username != 'standard_user') {
2222
user = username;
@@ -25,7 +25,7 @@ Then('I log in as {string}', (username) => {
2525
cy.get('[data-test="password"]').type('secret_sauce').should('have.value', 'secret_sauce');
2626
});
2727

28-
Then('I log in as {string} with a screenshot', (username) => {
28+
Then('I log in as {string} with a screenshot', (username: string) => {
2929
let user = Cypress.env('VISUAL_CHECK') ? 'visual_user' : 'standard_user';
3030
if (username != 'standard_user') {
3131
user = username;

0 commit comments

Comments
 (0)