Having issues with TypeScript custom commands in Cypress?

Update: https://github.com/cypress-io/cypress/issues/1065#issuecomment-351769720 Removing an import from my commands.ts fixed it. Thanks

In the process of transitioning my cypress project to use TypeScript, I am following the guidelines provided at https://docs.cypress.io/guides/tooling/typescript-support#Types-for-custom-commands

All other compilation errors have been resolved except for the issue with custom commands not functioning as expected. For instance:

commands.ts:

declare namespace Cypress {
  interface Chainable {
    clickByLinkText(link: string): Chainable<Element>;
  }
}

// Finds and clicks a given link by it's link text
Cypress.Commands.add("clickByLinkText", (link: string) => {
  cy.get("a").contains(link).click();
});

Despite implementing the function in my test, I encounter the error message:

TS2339: Property 'clickByLinkText' does not exist on type 'cy & EventEmitter'.

The line import "./commands"; is included in my support/index.ts

Answer №1

To customize your commands, follow this example. For more details, refer to the discussion available at https://github.com/cypress-io/cypress/issues/1065#issuecomment-351769720.

function clickByLinkText(link: string) {
  cy.get("a").contains(link).click();
}
Cypress.Commands.add("clickByLinkText", clickByLinkText)

Don't forget to update your tsconfig.json file by adding "node_modules/cypress" under includes. Ensure that both node_modules and tsconfig.json file are located at the same level.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Accessing Nested Arrays in Angular 8: Retrieving Data in HTML Template from Multiple Layers of Arrays

Hello there. I'm using an API that gives me the following data: (4) [{…}, {…}, {…}, {…}] 0: dueDate: "2018-03-26T00:00:00" priority: {priorityId: 1, priorityName: "Critical", priorityColor: "red"} statuses: Array(1) 0: ...

Designations for removing an item at a targeted subdirectory location

type Taillet<T extends any[]> = ((...t: T) => void) extends (( h: any, ...r: infer R ) => void) ? R : never; type NestedOmit<T, Path extends string[]> = T extends object ? { 0: Omit<T, Path[0]>; 1: { [ ...

Setting up VSCode to run various tasks

My TypeScript project in Visual Studio Code has a specific task outlined as follows: { "version": "0.1.0", // The command is tsc. "command": "tsc", // Show the output window only if unrecognized errors occur. "showOutput": "silent", // Und ...

Angular 2 code test coverage

Looking to calculate the code coverage of my Angular 2 code. Wondering if there are any plugins available for VS Code or WebStorm that can assist with this. My unit testing is done using Jasmine and Karma. ...

Module '@types/mongodb' could not be located

Currently, I am working on a Node.js application using Typescript with a MongoDb database. Unfortunately, I encountered an issue today related to importing the type definitions of MongoDb. When I try to import the Db type like this: import { Db } from "@ ...

Encountering an error when attempting to include React TypeScript onChange with a Material UI switch component

I'm working on implementing a show/hide functionality using a switch. I want the component to be displayed when the switch is turned on and hidden when it's turned off. Here's the code I've written: const VirtualEventSection = ({ con ...

Changing field visibility in Angular Reactive form (form validation) by toggling based on a checkbox in another component

I'm facing a challenge with a complex form where the fields depend on toggling checkboxes in a separate component (parent). My goal is to dynamically validate the form, with some fields being enabled and others disabled based on the toggling of the ch ...

Is it necessary to include a package.json file in the /src directory when creating a package?

I am facing a situation where I have a package.json file in both the root directory and the /src folder within my projects. However, during the build process, the /src package.json is the one that gets copied to the /dist folder (and eventually published t ...

Tips on resolving the 404 path error in Angular2/TypeScript ASP.NET 4.6.1 on Visual Studio 2015

I'm facing a challenge while developing a new application using TypeScript, Angular2, and ASP.NET 4.6.1 on VS2015. Two issues have come up in the process. First problem: I keep encountering 404 errors with the include files in my index.html file. Upo ...

How can I incorporate an interface and specify a particular type as the return value in TypeScript?

interface Inter{ str:string } function func(){ let v:Inter={ str:'abc' }; return v; } func()//Is there a way to ensure that the type of value returned from `func` is {str:'abc'} without explicitly declaring it i ...

Creating a function that operates according to the input parameter

Imagine a scenario where I am working with the following JS function: function fetchValue(keyName) { return x => x[keyName]; } Is it possible to define fetchValue in such a way that Typescript's type inference automatically determines the outp ...

Exploring the functionalities of React Native with react-hook-form and implementing them with TypeScript

I've been working on creating a custom Input component in react native using typescript for the react-hook-form library. type CustomInputProps = { name: any, control: any } const CustomInput: FC<CustomInputProps> = ({name, control, ...p ...

injecting a variable from the configuration service into a TypeScript decorator

I am interested in setting up a scheduled task for my NestJs application to run at regular intervals. I found information on how to use intervals in the NestJs documentation. Since my application uses configuration files, I want to keep the interval value ...

Issue with Ionic 3 types while using the copyTo function on fileEntry

While working on my Ionic 3 app, I encountered an issue when trying to copy a file to a new directory using the Cordova plugin file. The error message states: Argument of type 'Entry' is not assignable to parameter of type 'DirectoryEntry&a ...

Is it possible to utilize a const as both an object and a type within TypeScript?

In our code, we encountered a scenario where we had a class that needed to serve as both an object and an interface. The class had a cumbersome long name, so we decided to assign it to a constant. However, when we attempted to use this constant, we faced s ...

What is the best way to remove all attributes from one interface when comparing to another?

Consider the following two interfaces: interface A { a: number; b: string; } interface B { b: string; } I am interested in creating a new type that includes all the keys from interface A, but excludes any keys that are also present in interface B. ...

Dragging element position updated

After implementing a ngFor loop in my component to render multiple CdkDrag elements from an array, I encountered the issue of their positions updating when deleting one element and splicing the array. Is there a way to prevent this unwanted position update ...

Is it possible for a redis client to function without having a redis datastore installed?

Currently in my node web server, I am utilizing the npm module known as redis. Upon executing my code... const client = redis.createClient(); client.on("error", function (err) { console.log("Error " + err); }); client.hmset(["key", "test keys 1", "t ...

The function signature '({ articles }: Props) => JSX.Element' does not match the type 'NextPage<{}, {}>'

Recently, I've decided to delve into the world of React.js and Next.js after being familiar with Vue.js. Encountering a peculiar typescript error has left me scratching my head, but surprisingly, the code actually compiles despite Visual Studio Code w ...

Exploring Angular (5) http client capabilities with the options to observe and specify the response type as 'blob'

Situation: I'm facing a challenge in downloading a binary file from a backend system that requires certain data to be posted as JSON-body. The goal is to save this file using File-Saver with the filename specified by the backend in the content-disposi ...