Cypress 终端错误:属性在类型 ‘cy & CyEventEmitter’ 上不存在。

huangapple go评论107阅读模式
英文:

Cypress terminal error: Property does not exist on type 'cy & CyEventEmitter

问题

以下是您提供的内容的中文翻译:

技术堆栈: Angular v15 和 Cypress V12

我的组件和端到端测试似乎运行正常,但在运行测试时出现了奇怪的终端错误。

这可能与我为共享命令配置的 Cypress 配置有关。

这是我的共享命令:

  1. declare global {
  2. namespace Cypress {
  3. interface Chainable {
  4. loginAs(role?: string): Chainable<any>,
  5. isWelcomePage(): Chainable<any>,
  6. openMenu(): Chainable<any>,
  7. }
  8. }
  9. }
  10. Cypress.Commands.add('loginAs', (role?: string) => {
  11. const user = Cypress.env('user');
  12. cy.visit(Cypress.env('local'));
  13. cy.get('#mat-input-0', { timeout: 30000 }).should('be.visible').type(user.email);
  14. cy.get('#mat-input-1', { timeout: 30000 }).should('be.visible').type(user.password);
  15. cy.get('.btn').click({ force: true });
  16. });

这是我的 Cypress 配置:

  1. import { defineConfig } from "cypress";
  2. const { verifyDownloadTasks } = require('cy-verify-downloads');
  3. export default defineConfig({
  4. e2e: {
  5. setupNodeEvents(on, config) {
  6. on('task', verifyDownloadTasks)
  7. },
  8. specPattern: "cypress/e2e/**/*.cy.ts",
  9. },
  10. chromeWebSecurity: false,
  11. screenshotsFolder: "cypress/snapshots",
  12. trashAssetsBeforeRuns: true,
  13. viewportWidth: 1400,
  14. viewportHeight: 1200,
  15. video: false,
  16. defaultCommandTimeout: 10000,
  17. env: {
  18. local: "http://localhost:4200/",
  19. staging: "hidden",
  20. backend: "hidden",
  21. user: {
  22. email: "hidden",
  23. password: "hidden",
  24. },
  25. },
  26. component: {
  27. devServer: {
  28. framework: "angular",
  29. bundler: "webpack",
  30. },
  31. specPattern: "**/*.cy.ts",
  32. },
  33. });

这是终端错误:

Cypress 终端错误:属性在类型 ‘cy & CyEventEmitter’ 上不存在。

确切的错误消息

  • 类型 'cy & CyEventEmitter' 上不存在属性 'loginAs'。

当前尝试:

Tsconfig:

  1. {
  2. "compileOnSave": false,
  3. "compilerOptions": {
  4. "baseUrl": "./",
  5. "outDir": "./dist/out-tsc",
  6. "forceConsistentCasingInFileNames": true,
  7. "strict": false,
  8. "noImplicitOverride": true,
  9. "noPropertyAccessFromIndexSignature": false,
  10. "noImplicitReturns": true,
  11. "noFallthroughCasesInSwitch": true,
  12. "sourceMap": true,
  13. "declaration": false,
  14. "downlevelIteration": true,
  15. "experimentalDecorators": true,
  16. "moduleResolution": "node",
  17. "importHelpers": true,
  18. "target": "ES2022",
  19. "module": "es2020",
  20. "allowJs": true,
  21. "lib": [
  22. "es2020",
  23. "dom"
  24. ],
  25. "esModuleInterop": true,
  26. "allowSyntheticDefaultImports": true,
  27. "types": ["cypress", "cy-verify-downloads"],
  28. },
  29. "angularCompilerOptions": {
  30. "enableI18nLegacyMessageIdFormat": false,
  31. "strictInjectionParameters": false,
  32. "strictInputAccessModifiers": false,
  33. "strictTemplates": false
  34. },
  35. "include": [
  36. "./src",
  37. "./cypress/support/index.d.ts"
  38. ]
  39. }

index.d.ts 文件:

  1. /// <reference types="cypress" />
  2. export { }
  3. declare global {
  4. namespace Cypress {
  5. interface Chainable {
  6. loginAs(role?: string): Chainable<any>,
  7. isWelcomePage(): Chainable<any>,
  8. openMenu(): Chainable<any>,
  9. }
  10. }
  11. }

当我运行组件测试时,我得到了相同的终端错误。

Cypress 终端错误:属性在类型 ‘cy & CyEventEmitter’ 上不存在。

英文:

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:

  1. declare global {
  2. namespace Cypress {
  3. interface Chainable {
  4. loginAs(role?: string): Chainable&lt;any&gt;,
  5. isWelcomePage(): Chainable&lt;any&gt;,
  6. openMenu(): Chainable&lt;any&gt;,
  7. }
  8. }
  9. }
  10. Cypress.Commands.add(&#39;loginAs&#39;, (role?: string) =&gt; {
  11. const user = Cypress.env(&#39;user&#39;);
  12. cy.visit(Cypress.env(&#39;local&#39;));
  13. cy.get(&#39;#mat-input-0&#39;, { timeout: 30000 }).should(&#39;be.visible&#39;).type(user.email);
  14. cy.get(&#39;#mat-input-1&#39;, { timeout: 30000 }).should(&#39;be.visible&#39;).type(user.password);
  15. cy.get(&#39;.btn&#39;).click({ force: true });
  16. });

This is my cypress configuration:

  1. import { defineConfig } from &quot;cypress&quot;;
  2. const { verifyDownloadTasks } = require(&#39;cy-verify-downloads&#39;);
  3. export default defineConfig({
  4. e2e: {
  5. setupNodeEvents(on, config) {
  6. on(&#39;task&#39;, verifyDownloadTasks)
  7. },
  8. specPattern: &quot;cypress/e2e/**/*.cy.ts&quot;,
  9. },
  10. chromeWebSecurity: false,
  11. screenshotsFolder: &quot;cypress/snapshots&quot;,
  12. trashAssetsBeforeRuns: true,
  13. viewportWidth: 1400,
  14. viewportHeight: 1200,
  15. video: false,
  16. defaultCommandTimeout: 10000,
  17. env: {
  18. local: &quot;http://localhost:4200/&quot;,
  19. staging: &quot;hidden&quot;,
  20. backend: &quot;hidden&quot;,
  21. user: {
  22. email: &quot;hidden&quot;,
  23. password: &quot;hidden&quot;,
  24. },
  25. },
  26. component: {
  27. devServer: {
  28. framework: &quot;angular&quot;,
  29. bundler: &quot;webpack&quot;,
  30. },
  31. specPattern: &quot;**/*.cy.ts&quot;,
  32. },
  33. });

This is the terminal errors:

Cypress 终端错误:属性在类型 ‘cy & CyEventEmitter’ 上不存在。

Exact error messages

  • Property 'loginAs' does not exist on type 'cy & CyEventEmitter'.

Current Attempt:

Tsconfig:

  1. {
  2. &quot;compileOnSave&quot;: false,
  3. &quot;compilerOptions&quot;: {
  4. &quot;baseUrl&quot;: &quot;./&quot;,
  5. &quot;outDir&quot;: &quot;./dist/out-tsc&quot;,
  6. &quot;forceConsistentCasingInFileNames&quot;: true,
  7. &quot;strict&quot;: false,
  8. &quot;noImplicitOverride&quot;: true,
  9. &quot;noPropertyAccessFromIndexSignature&quot;: false,
  10. &quot;noImplicitReturns&quot;: true,
  11. &quot;noFallthroughCasesInSwitch&quot;: true,
  12. &quot;sourceMap&quot;: true,
  13. &quot;declaration&quot;: false,
  14. &quot;downlevelIteration&quot;: true,
  15. &quot;experimentalDecorators&quot;: true,
  16. &quot;moduleResolution&quot;: &quot;node&quot;,
  17. &quot;importHelpers&quot;: true,
  18. &quot;target&quot;: &quot;ES2022&quot;,
  19. &quot;module&quot;: &quot;es2020&quot;,
  20. &quot;allowJs&quot;: true,
  21. &quot;lib&quot;: [
  22. &quot;es2020&quot;,
  23. &quot;dom&quot;
  24. ],
  25. &quot;esModuleInterop&quot;: true,
  26. &quot;allowSyntheticDefaultImports&quot;: true,
  27. &quot;types&quot;: [&quot;cypress&quot;, &quot;cy-verify-downloads&quot;],
  28. },
  29. &quot;angularCompilerOptions&quot;: {
  30. &quot;enableI18nLegacyMessageIdFormat&quot;: false,
  31. &quot;strictInjectionParameters&quot;: false,
  32. &quot;strictInputAccessModifiers&quot;: false,
  33. &quot;strictTemplates&quot;: false
  34. },
  35. &quot;include&quot;: [
  36. &quot;./src&quot;,
  37. &quot;./cypress/support/index.d.ts&quot;
  38. ]
  39. }

index.d.ts file:

  1. /// &lt;reference types=&quot;cypress&quot; /&gt;
  2. export { }
  3. declare global {
  4. namespace Cypress {
  5. interface Chainable {
  6. loginAs(role?: string): Chainable&lt;any&gt;,
  7. isWelcomePage(): Chainable&lt;any&gt;,
  8. openMenu(): Chainable&lt;any&gt;,
  9. }
  10. }
  11. }

I got the same terminal errors when I run the component tests.

Cypress 终端错误:属性在类型 ‘cy & CyEventEmitter’ 上不存在。

答案1

得分: 3

以下是翻译好的部分:

  • 将自定义命令类型放在 cypress/support/index.d.ts

  • 添加对基本 Cypress 类型的引用

  • .d.ts 文件声明为一个带有虚拟导出的模块

  1. /// <reference types="cypress" />
  2. export {}
  3. declare global {
  4. namespace Cypress {
  5. interface Chainable {
  6. loginAs(role?: string): Chainable<any>,
  7. isWelcomePage(): Chainable<any>,
  8. openMenu(): Chainable<any>,
  9. }
  10. }
  11. }

tsconfig.json 中添加您的 index.d.ts

  1. {
  2. ...
  3. "include": [ "./src/app", "./cypress/support/index.d.ts"],
  4. }
英文:

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

  1. /// &lt;reference types=&quot;cypress&quot; /&gt;
  2. export {}
  3. declare global {
  4. namespace Cypress {
  5. interface Chainable {
  6. loginAs(role?: string): Chainable&lt;any&gt;,
  7. isWelcomePage(): Chainable&lt;any&gt;,
  8. openMenu(): Chainable&lt;any&gt;,
  9. }
  10. }
  11. }

In tsconfig.json add your index.d.ts

  1. {
  2. ...
  3. &quot;include&quot;: [ &quot;./src/app&quot;, &quot;./cypress/support/index.d.ts&quot;],
  4. }

huangapple
  • 本文由 发表于 2023年6月12日 20:01:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定