首页 > 解决方案 > 类型“cy & EventEmitter”上不存在属性“app”

问题描述

来自Cypress on Rails的源代码

// spec/cypress/support/on-rails.ts

Cypress.Commands.add('app', function (name, command_options) {
  return cy
    .appCommands({ name: name, options: command_options })
    .then((body) => {
      return body[0];
    });
});

spec/cypress/tsconfig.json

{
  "compilerOptions": {
    "strict": true,
    "target": "es5",
    "lib": ["es6", "dom"],
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": false,
    "types": ["cypress"],
    "downlevelIteration": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "include": ["**/*.ts"]
}

结果是

 Property 'app' does not exist on type 'cy & EventEmitter'.

标签: cypress

解决方案


相当老,但我最近遇到了这个问题。

这些文档显示了一种处理命令的方法。这是他们文档的链接:https ://docs.cypress.io/guides/tooling/typescript-support#Adding-child-or-dual-commands

赛普拉斯/支持/index.ts

// load type definitions that come with Cypress module
/// <reference types="cypress" />

declare global {
  namespace Cypress {
    interface Chainable {
      /**
       * Custom command to type a few random words into input elements
       * @param count=3
       * @example cy.get('input').typeRandomWords()
       */
      typeRandomWords(
        count?: number,
        options?: Partial<TypeOptions>
      ): Chainable<Element>
    }
  }
}

赛普拉斯/支持/index.ts

Cypress.Commands.add('typeRandomWords', { prevSubject: 'element' }, (
  subject /* :JQuery<HTMLElement> */,
  count = 3,
  options?
) => {
  return cy.wrap(subject).type(generateRandomWords(count), options)
})

推荐阅读