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

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

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",
  },
});

这是终端错误:

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

确切的错误消息

  • 类型 '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>,
    }
  }
}

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

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:

declare global {
  namespace Cypress {
    interface Chainable {
      loginAs(role?: string): Chainable&lt;any&gt;,
      isWelcomePage(): Chainable&lt;any&gt;,
      openMenu(): Chainable&lt;any&gt;,
    }
  }
}

Cypress.Commands.add(&#39;loginAs&#39;, (role?: string) =&gt; {
  const user = Cypress.env(&#39;user&#39;);
  cy.visit(Cypress.env(&#39;local&#39;));
  cy.get(&#39;#mat-input-0&#39;, { timeout: 30000 }).should(&#39;be.visible&#39;).type(user.email);
  cy.get(&#39;#mat-input-1&#39;, { timeout: 30000 }).should(&#39;be.visible&#39;).type(user.password);
  cy.get(&#39;.btn&#39;).click({ force: true });
});

This is my cypress configuration:

import { defineConfig } from &quot;cypress&quot;;
const { verifyDownloadTasks } = require(&#39;cy-verify-downloads&#39;);

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on(&#39;task&#39;, verifyDownloadTasks)
    },
    specPattern: &quot;cypress/e2e/**/*.cy.ts&quot;,
  },
  chromeWebSecurity: false,
  screenshotsFolder: &quot;cypress/snapshots&quot;,
  trashAssetsBeforeRuns: true,
  viewportWidth: 1400,
  viewportHeight: 1200,
  video: false,
  defaultCommandTimeout: 10000,
  env: {
    local: &quot;http://localhost:4200/&quot;,
    staging: &quot;hidden&quot;,
    backend: &quot;hidden&quot;,
    user: {
      email: &quot;hidden&quot;,
      password: &quot;hidden&quot;,
    },
  },
  component: {
    devServer: {
      framework: &quot;angular&quot;,
      bundler: &quot;webpack&quot;,
    },
    specPattern: &quot;**/*.cy.ts&quot;,
  },
});

This is the terminal errors:

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

Exact error messages

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

Current Attempt:

Tsconfig:

{
  &quot;compileOnSave&quot;: false,
  &quot;compilerOptions&quot;: {
    &quot;baseUrl&quot;: &quot;./&quot;,
    &quot;outDir&quot;: &quot;./dist/out-tsc&quot;,
    &quot;forceConsistentCasingInFileNames&quot;: true,
    &quot;strict&quot;: false,
    &quot;noImplicitOverride&quot;: true,
    &quot;noPropertyAccessFromIndexSignature&quot;: false,
    &quot;noImplicitReturns&quot;: true,
    &quot;noFallthroughCasesInSwitch&quot;: true,
    &quot;sourceMap&quot;: true,
    &quot;declaration&quot;: false,
    &quot;downlevelIteration&quot;: true,
    &quot;experimentalDecorators&quot;: true,
    &quot;moduleResolution&quot;: &quot;node&quot;,
    &quot;importHelpers&quot;: true,
    &quot;target&quot;: &quot;ES2022&quot;,
    &quot;module&quot;: &quot;es2020&quot;,
    &quot;allowJs&quot;: true,
    &quot;lib&quot;: [
      &quot;es2020&quot;,
      &quot;dom&quot;
    ],
    &quot;esModuleInterop&quot;: true,
    &quot;allowSyntheticDefaultImports&quot;: true,
    &quot;types&quot;: [&quot;cypress&quot;, &quot;cy-verify-downloads&quot;],
  },
  &quot;angularCompilerOptions&quot;: {
    &quot;enableI18nLegacyMessageIdFormat&quot;: false,
    &quot;strictInjectionParameters&quot;: false,
    &quot;strictInputAccessModifiers&quot;: false,
    &quot;strictTemplates&quot;: false
  },
  &quot;include&quot;: [
    &quot;./src&quot;,
    &quot;./cypress/support/index.d.ts&quot;
  ]
}

index.d.ts file:

/// &lt;reference types=&quot;cypress&quot; /&gt;
export { }

declare global {
  namespace Cypress {
    interface Chainable {
      loginAs(role?: string): Chainable&lt;any&gt;,
      isWelcomePage(): Chainable&lt;any&gt;,
      openMenu(): Chainable&lt;any&gt;,
    }
  }
}

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 文件声明为一个带有虚拟导出的模块

/// <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

/// &lt;reference types=&quot;cypress&quot; /&gt;
export {}

declare global {
  namespace Cypress {
    interface Chainable {
      loginAs(role?: string): Chainable&lt;any&gt;,
      isWelcomePage(): Chainable&lt;any&gt;,
      openMenu(): Chainable&lt;any&gt;,
    }
  }
}

In tsconfig.json add your index.d.ts

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

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:

确定