-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added type safety to custom data-cy command
As data-cy attributes are the preferred way to select element within Cypress, I've enhanced the custom `cy.dataCy()` command to use constants instead of a string. This ensures that custom data selectors cannot have typos or accidential changes. To make this work, I had to consolidate cypress.d.ts and commands.ts as a d.ts file cannot import other files dynamically.
- Loading branch information
1 parent
060d62d
commit 4d62af4
Showing
5 changed files
with
20 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
Cypress.Commands.add("dataCy", (selector: string) => { | ||
/* eslint-disable @typescript-eslint/no-namespace */ | ||
import { CyKey } from "./constants" | ||
|
||
declare global { | ||
namespace Cypress { | ||
interface Chainable { | ||
dataCy(value: CyKey): Chainable<JQuery<HTMLElement>> | ||
} | ||
} | ||
} | ||
|
||
Cypress.Commands.add("dataCy", selector => { | ||
return cy.get(`[data-cy="${selector}"]`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const cyKeys = { | ||
"go-logo": "go-logo", | ||
} as const | ||
|
||
export type CyKey = keyof typeof cyKeys |