I'm having a problem trying to get code completion for Cypress while I'm using JS. I've tried following every bit of documentation I could found, but I don't find these comprehensive enough.
None of these worked for me, what finally gave me Intellisense was adding a tsconfig.json
file to the Cypress folder, as per the Cypress docs:
{
"compilerOptions": {
"allowJs": true,
"baseUrl": "../node_modules",
"types": ["cypress"]
},
"include": ["**/*.*"]
}
files
in the cypress/tsconfig.json
might also give you trouble. Have to use include/exclude
. Wierd, and hard to figure out. –
Broadsword I just added
"compilerOptions": {
"types": ["cypress"]
}
to the object in the tsconfig.json file that was in the root cypress directory. I then chose "Restart TS Server" from the Command Palette and that sorted things out for me.
This is because you installed Cypress via the .exe
, which is not the recommended way of installing Cypress.
Cypress type definitions are only installed when you install cypress via npm
For VSCode to find the type definitions, you should install cypress as a dev dependency to your project (preferred form of installation according to the docs):
npm i -D cypress
npm i -D cypress
–
Greenberg Hi I was able to set it on vs code by simple adding /// I've written it previously with C from Cyberpress in lower case, after adding the C in upper case it solved my problem
How I get Cypress typings to work in vscode
I had problems getting Cypress intellisense to work too. My way to get intellisense is convoluted and probably wrong, but I didn't get it to work in any other way.
- add a
cypress.d.ts
file in the root of my project with the following types syntax. This declare thecy
type, so you get autocompletion for most Cypress stuff:
declare var Cypress: any;
interface CypressElement {
type(value: string, options?: any): CypressElement,
clear(options?: {force: boolean}): CypressElement,
click(options?: {force: boolean}): CypressElement,
should(...args: any): CypressElement,
selectValue(option: number, optionsClass: string):CypressElement,
fillInput(value: string):CypressElement,
eq(index: number): CypressElement,
contains(value: any): CypressElement,
within(...args: any): any,
trigger(...args: any): any;
first(): CypressElement;
}
declare var cy: {
get(select: any): CypressElement;
window(): Promise<any>;
visit(path: any): void;
request(options: any): Promise<any>;
wait(time: string | number): any;
server(): any;
route(...options: any): any;
log(...messages: string[]): any;
contains(selector: string, value: any): any;
stub(...args: any): any;
on(event: string, callback: any): any;
url(): CypressElement;
};
(Declare Cypress typings manually this way seems alien at best. However, trying to use the native ones generated a lot of problems)
Reference that file in the
tsconf.json
compiler options via:"typeRoots": ["cypress.d.ts"],
This just enables intellisense to Cypress, even if the cypress code is written in javaScript, since vscode relies a lot in typescript for its intellisense engine.
Since you're not using typeScript I guess, you may need to add a very simple tsconfig
file at the root (so your editor can read its configuration), something like:
{
"compilerOptions": {
"typeRoots": ["cypress.d.ts"],
"target": "es5",
"module": "commonjs",
"lib": [
"es6"
],
"declaration": true,
"removeComments": false,
"stripInternal": true,
// since 2.3
// "strict": true,
"alwaysStrict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true
},
"files": [
"./index.ts"
]
}
Maybe you can instruct your editor to load cypress.d.ts
as typeRoots
, I don't know.
After this, you should have intellisense for your cy
variable and for the objets that comes from cy.get()
(which above are called CypressElement
in that types definition).
One big caveat about this, is that whenever you use a new Cypress feature, you need to manually add its type to cypress.d.ts
to get the intellisense.
© 2022 - 2024 — McMap. All rights reserved.
node_modules/cypress/types/index.d.ts
exist? does vscode colorize the/// <reference types="cypress"/>
line? or is it all one color? – Greenberg