VS Code Jest和Cypress intellisense与Chai无法正常工作

问题描述 投票:1回答:1

我使用Jest作为单元测试框架,并且bellow intellisense是正确的:

enter image description here

但是,当我安装赛普拉斯"cypress": "^3.2.0"时,相同的代码现在显示错误Property 'toMatch' does not exist on type 'Assertion'. Did you mean 'match'?。这个IMO的原因是赛普拉斯在node_modules/cypress/types/chai/index.d.ts和VS Code下的安装类型正在选择它们用于intellisense。 Jest和Cypress都依赖于Chai断言库。安装赛普拉斯后的智能感知:

enter image description here

有没有办法告诉VS Code哪个Chai intellisense在特定文件夹中使用?或者有没有办法在jsconfig.json文件中指定它?

visual-studio-code jestjs intellisense cypress
1个回答
2
投票

解决方案是为Jest中公开的全局变量创建别名,并在JSDoc中使用@type修饰这些变量。所以,我在我的测试所在的同一目录中创建了一个文件jestGlobals.js

jestGlobals.js文件:(为简单起见,我只包含一个全局,但你会为所有全局变量做同样的事情):

/** @type {jest.Expect} */
// @ts-ignore
let expect = global.expect

export { expect }

然后我只是在我的*.spec.js文件中导入这些变量:

import { expect } from './jestGlobals'

现在,当我使用这个别名时,我得到了正确的智能感知:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.