英文:
Cypress terminal error: Property does not exist on type 'cy & CyEventEmitter
问题
以下是您提供的内容的中文翻译:
技术堆栈: Angular v15 和 Cypress V12
我的组件和端到端测试似乎运行正常,但在运行测试时出现了奇怪的终端错误。
这可能与我为共享命令配置的 Cypress 配置有关。
这是我的共享命令:
declare global {
namespace Cypress {
interface Chainable {
loginAs(role?: string): Chainable<any>,
isWelcomePage(): Chainable<any>,
openMenu(): Chainable<any>,
}
}
}
Cypress.Commands.add('loginAs', (role?: string) => {
const user = Cypress.env('user');
cy.visit(Cypress.env('local'));
cy.get('#mat-input-0', { timeout: 30000 }).should('be.visible').type(user.email);
cy.get('#mat-input-1', { timeout: 30000 }).should('be.visible').type(user.password);
cy.get('.btn').click({ force: true });
});
这是我的 Cypress 配置:
import { defineConfig } from "cypress";
const { verifyDownloadTasks } = require('cy-verify-downloads');
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
on('task', verifyDownloadTasks)
},
specPattern: "cypress/e2e/**/*.cy.ts",
},
chromeWebSecurity: false,
screenshotsFolder: "cypress/snapshots",
trashAssetsBeforeRuns: true,
viewportWidth: 1400,
viewportHeight: 1200,
video: false,
defaultCommandTimeout: 10000,
env: {
local: "http://localhost:4200/",
staging: "hidden",
backend: "hidden",
user: {
email: "hidden",
password: "hidden",
},
},
component: {
devServer: {
framework: "angular",
bundler: "webpack",
},
specPattern: "**/*.cy.ts",
},
});
这是终端错误:
确切的错误消息
- 类型 'cy & CyEventEmitter' 上不存在属性 'loginAs'。
当前尝试:
Tsconfig:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": false,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "ES2022",
"module": "es2020",
"allowJs": true,
"lib": [
"es2020",
"dom"
],
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"types": ["cypress", "cy-verify-downloads"],
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": false,
"strictInputAccessModifiers": false,
"strictTemplates": false
},
"include": [
"./src",
"./cypress/support/index.d.ts"
]
}
index.d.ts 文件:
/// <reference types="cypress" />
export { }
declare global {
namespace Cypress {
interface Chainable {
loginAs(role?: string): Chainable<any>,
isWelcomePage(): Chainable<any>,
openMenu(): Chainable<any>,
}
}
}
当我运行组件测试时,我得到了相同的终端错误。
英文:
Tech stack: Angular v15 and Cypress V12
My component and e2e tests seem to run fine but I get these weird terminal errors when running the tests.
It could be an issue with my Cypress configuration for my shared commands.
This is my shared commands:
declare global {
namespace Cypress {
interface Chainable {
loginAs(role?: string): Chainable<any>,
isWelcomePage(): Chainable<any>,
openMenu(): Chainable<any>,
}
}
}
Cypress.Commands.add('loginAs', (role?: string) => {
const user = Cypress.env('user');
cy.visit(Cypress.env('local'));
cy.get('#mat-input-0', { timeout: 30000 }).should('be.visible').type(user.email);
cy.get('#mat-input-1', { timeout: 30000 }).should('be.visible').type(user.password);
cy.get('.btn').click({ force: true });
});
This is my cypress configuration:
import { defineConfig } from "cypress";
const { verifyDownloadTasks } = require('cy-verify-downloads');
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
on('task', verifyDownloadTasks)
},
specPattern: "cypress/e2e/**/*.cy.ts",
},
chromeWebSecurity: false,
screenshotsFolder: "cypress/snapshots",
trashAssetsBeforeRuns: true,
viewportWidth: 1400,
viewportHeight: 1200,
video: false,
defaultCommandTimeout: 10000,
env: {
local: "http://localhost:4200/",
staging: "hidden",
backend: "hidden",
user: {
email: "hidden",
password: "hidden",
},
},
component: {
devServer: {
framework: "angular",
bundler: "webpack",
},
specPattern: "**/*.cy.ts",
},
});
This is the terminal errors:
Exact error messages
- Property 'loginAs' does not exist on type 'cy & CyEventEmitter'.
Current Attempt:
Tsconfig:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": false,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "ES2022",
"module": "es2020",
"allowJs": true,
"lib": [
"es2020",
"dom"
],
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"types": ["cypress", "cy-verify-downloads"],
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": false,
"strictInputAccessModifiers": false,
"strictTemplates": false
},
"include": [
"./src",
"./cypress/support/index.d.ts"
]
}
index.d.ts file:
/// <reference types="cypress" />
export { }
declare global {
namespace Cypress {
interface Chainable {
loginAs(role?: string): Chainable<any>,
isWelcomePage(): Chainable<any>,
openMenu(): Chainable<any>,
}
}
}
I got the same terminal errors when I run the component tests.
答案1
得分: 3
以下是翻译好的部分:
-
将自定义命令类型放在
cypress/support/index.d.ts
中 -
添加对基本 Cypress 类型的引用
-
将
.d.ts
文件声明为一个带有虚拟导出的模块
/// <reference types="cypress" />
export {}
declare global {
namespace Cypress {
interface Chainable {
loginAs(role?: string): Chainable<any>,
isWelcomePage(): Chainable<any>,
openMenu(): Chainable<any>,
}
}
}
在 tsconfig.json
中添加您的 index.d.ts
{
...
"include": [ "./src/app", "./cypress/support/index.d.ts"],
}
英文:
There's probably a few ways to tackle it, here's one
-
put the custom command types in
cypress/support/index.d.ts
-
add a reference to base cypress types
-
make the
.d.ts
a module with a dummy export
/// <reference types="cypress" />
export {}
declare global {
namespace Cypress {
interface Chainable {
loginAs(role?: string): Chainable<any>,
isWelcomePage(): Chainable<any>,
openMenu(): Chainable<any>,
}
}
}
In tsconfig.json
add your index.d.ts
{
...
"include": [ "./src/app", "./cypress/support/index.d.ts"],
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论