Cypress挂载自定义Day.js后出现TS2339类型错误的求助
重构Moment.js为Day.js后触发持续TS2339类型报错
问题核心表现
- 完成测试代码从Moment.js到Day.js的重构后,WebStorm接连抛出大量TS2339类型报错,报错提示为:类型'Cypress & EventEmitter'上不存在属性'dayjs'
- 报错仅为静态类型校验层面的提示,不会阻断代码编译流程,也不影响测试用例的正常运行,但大量无效报错会和真实代码问题的提示混杂,大幅提升排查真实问题的难度。
触发报错的典型代码示例
export function verifyCreatedDate(date: string) { cy.log('Verify item creation date'); cy.get('[data-cy="timeline-date"]').should( 'have.text', Cypress.dayjs(date).format('MM/DD/YY'), ); }
现有配置与排查过程
当前tsconfig.json配置内容
{ "compilerOptions": { "outDir": "./dist/", "noImplicitAny": false, "module": "es2015", "target": "es2015", "jsx": "react", "allowJs": true, "types": ["cypress", "@percy/cypress"], "moduleResolution": "node", "typeRoots": ["node_modules/@types"], "lib": ["es2018", "dom"] }, "include": ["**/*.ts", "node_modules/cypress/types/mocha/index.d.ts"] }
已尝试的无效操作
- 曾将dayjs加入
tsconfig.json的"types"与"include"字段后重启WebStorm,报错仍未消除 - 针对该问题检索近一周,所有找到的公开解决方案均未生效
已实现的全局挂载逻辑(位于support/index.js)
// 多个测试规格文件需要使用dayjs时,在support文件统一引入后挂载到全局Cypress对象 const dayjs = require('dayjs'); Cypress.dayjs = dayjs;
内容的提问来源于stack exchange,提问作者Zachary Costa
相关产品推荐
相关产品推荐

