Cypress TypeError:' jQuery'未定义

发布于 2025-02-09 20:35:58 字数 3151 浏览 1 评论 0 原文

cypress/commands.ts 尝试获取类型 jQuery 中,但它返回 vscode

'JQuery' is not defined.eslint[no-undef](https://eslint.org/docs/rules/no-undef)

i.sstatic.net/dydto.png“ rel =“ nofollow noreferrer ” 代码> jQuery 肯定似乎存在,您可以从上面的屏幕截图中看到,但是也许是因为相同类型的两个定义存在冲突?

command.ts

/// <reference types="cypress" />

declare global {
  namespace Cypress {
    interface Chainable {
      getElByAttr(attr: string, val: string): Chainable<JQuery<HTMLElement>>;
      getByTestId(testId: string, length?: number): Chainable<void>;
    }
  }
}

Cypress.Commands.add('getByTestId', (testId, length = 1) => {
  cy.get(`[data-testid="${testId}"]`).should('have.length', length);
});

Cypress.Commands.add('getElByAttr', (attr = 'name', val) => {
  return cy.get(`[${attr}=${val}]`);
});

export {};

root tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "incremental": true,
    "baseUrl": "."
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules", "babel.config.js", "jest.config.js", "__tests__/**/*.js"]
}

我试图将 tsconfig.json 添加到我的/cypress 文件夹中,但这并不能解决问题,这就是我尝试的。

/cypress/tsonfig.json,

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["es5", "dom"],
    "types": ["cypress", "node"]
  },
  "include": ["**/*.ts"]
}

我尝试每次更改时都尝试在VSCODE中重新启动我的TS服务器,但是似乎没有解决此问题

,看起来其他人也遇到了这个问题此处

也尝试添加“ type”:[“ Cypress”]

还尝试了以下内容;

赛普拉斯版本

10.1.0

,真实地亏本。任何帮助将不胜感激。

In cypress/commands.ts trying to get the type JQuery but it returns an eslint error in VSCode

'JQuery' is not defined.eslint[no-undef](https://eslint.org/docs/rules/no-undef)

JQuery is not defined

JQuery definitely seems to exist as you can see from the screenshot above, but perhaps because there are 2 definitions of the same type there's a conflict?

command.ts

/// <reference types="cypress" />

declare global {
  namespace Cypress {
    interface Chainable {
      getElByAttr(attr: string, val: string): Chainable<JQuery<HTMLElement>>;
      getByTestId(testId: string, length?: number): Chainable<void>;
    }
  }
}

Cypress.Commands.add('getByTestId', (testId, length = 1) => {
  cy.get(`[data-testid="${testId}"]`).should('have.length', length);
});

Cypress.Commands.add('getElByAttr', (attr = 'name', val) => {
  return cy.get(`[${attr}=${val}]`);
});

export {};

root tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "incremental": true,
    "baseUrl": "."
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules", "babel.config.js", "jest.config.js", "__tests__/**/*.js"]
}

I have tried to add a tsconfig.json into my /cypress folder but that didn't resolve the issue, this is what I tried.

/cypress/tsonfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["es5", "dom"],
    "types": ["cypress", "node"]
  },
  "include": ["**/*.ts"]
}

I've tried restarting my TS server in VSCode every time I make any change, but nothing seems to have resolved the issue

It looks like someone else also had this issue here

Also tried adding "types": ["cypress"] into either tsconfig.json

FWIW I've also tried the following;

Cypress Version

10.1.0

Truthfully, at a loss. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忱杏 2025-02-16 20:35:58

因此发现解决方案是我的eslint。

我需要将以下内容添加到我的中扩展选项

'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',

'@typescript-eslint/no-namespace': ['error', { allowDeclarations: true }],

So found the solution was with my eslint.

I needed to add the following to my extends options in my .eslintrc file

'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',

And had to add the following to my rules option

'@typescript-eslint/no-namespace': ['error', { allowDeclarations: true }],
单身情人 2025-02-16 20:35:58

对我有用的唯一一件事是在使用jQuery的打字稿文件的顶部添加/ *全局jQuery */

The only thing that worked for me was to add /* global JQuery */ at the top of the typescript file where JQuery is used.

独闯女儿国 2025-02-16 20:35:58

这不是直接的答案,但它有助于避免完全使用“ jQuery”接口,这样您就不会遇到错误。因此,建议是使用 returnType 来推断类型,而不是直接声明它:

declare global {
  namespace Cypress {
    interface Chainable {
      getElByAttr(attr: string, val: string): ReturnType<typeof cy.get>;
      getByTestId(testId: string, length?: number): ReturnType<typeof cy.get>;
    }
  }
}

This is not a direct answer but it helps to avoid using the 'JQuery' interface completely so you won't encounter the error at all. So the suggestion is to use ReturnType to infer the type instead of declaring it directly:

declare global {
  namespace Cypress {
    interface Chainable {
      getElByAttr(attr: string, val: string): ReturnType<typeof cy.get>;
      getByTestId(testId: string, length?: number): ReturnType<typeof cy.get>;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文