Cri*_*rin 7 intellisense cypress
我在使用 JS 时尝试为 Cypress 完成代码时遇到问题。我已经尝试遵循我能找到的所有文档,但我觉得这些文档不够全面。
这些都不适合我,最终给我智能感知的是添加一个tsconfig.json文件到 Cypress 文件夹,根据 Cypress 文档:
{
"compilerOptions": {
"allowJs": true,
"baseUrl": "../node_modules",
"types": ["cypress"]
},
"include": ["**/*.*"]
}
Run Code Online (Sandbox Code Playgroud)
Ser*_*eon -2
我在让赛普拉斯智能感知工作时也遇到了问题。我获得智能感知的方法很复杂,而且可能是错误的,但我没有让它以任何其他方式工作。
\n\ncypress.d.ts使用以下类型语法在我的项目的根目录中添加一个文件。这宣告了cy类型,因此您可以自动完成大多数 Cypress 内容:declare var Cypress: any;\n\ninterface CypressElement {\n type(value: string, options?: any): CypressElement,\n clear(options?: {force: boolean}): CypressElement,\n click(options?: {force: boolean}): CypressElement,\n should(...args: any): CypressElement,\n selectValue(option: number, optionsClass: string):CypressElement,\n fillInput(value: string):CypressElement,\n eq(index: number): CypressElement,\n contains(value: any): CypressElement,\n within(...args: any): any,\n trigger(...args: any): any;\n first(): CypressElement;\n}\n\ndeclare var cy: {\n get(select: any): CypressElement;\n window(): Promise<any>;\n visit(path: any): void;\n request(options: any): Promise<any>;\n wait(time: string |\xc2\xa0number): any;\n server(): any;\n route(...options: any): any;\n log(...messages: string[]): any;\n contains(selector: string, value: any): any;\n stub(...args: any): any;\n on(event: string, callback: any): any;\n url(): CypressElement;\n};\n\nRun Code Online (Sandbox Code Playgroud)\n\n(以这种方式手动声明 Cypress 类型充其量看起来很陌生。但是,尝试使用本机类型会产生很多问题)
\n\n通过以下方式在编译器选项中引用该文件tsconf.json:
"typeRoots": ["cypress.d.ts"],
这只是为 Cypress 启用智能感知,即使 cypress 代码是用 javaScript 编写的,因为 vscode 的智能感知引擎很大程度上依赖于 typescript。
\n\n我猜由于您没有使用 typeScript,您可能需要tsconfig在根目录添加一个非常简单的文件(以便您的编辑器可以读取其配置),例如:
{\n "compilerOptions": {\n "typeRoots": ["cypress.d.ts"],\n "target": "es5",\n "module": "commonjs",\n "lib": [\n "es6"\n ],\n "declaration": true,\n "removeComments": false,\n "stripInternal": true,\n // since 2.3\n // "strict": true,\n "alwaysStrict": true,\n "noImplicitAny": true,\n "noImplicitThis": true,\n "strictNullChecks": true\n },\n "files": [\n "./index.ts"\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n\n也许你可以指示你的编辑器加载cypress.d.ts为typeRoots,我不知道。
之后,您应该对cy变量和来自的对象cy.get()(上面在该类型定义中调用CypressElement)具有智能感知。
关于这一点的一个重要警告是,每当您使用新的 Cypress 功能时,您都需要手动添加其类型以cypress.d.ts获取智能感知。
| 归档时间: |
|
| 查看次数: |
5699 次 |
| 最近记录: |