【发布时间】:2020-06-23 14:51:24
【问题描述】:
我的一个 Cypress 测试在尝试从 create-react-app src 目录的源代码中的文件导入时无法运行。
测试看起来像:
// cypress/integration/this-fails.js
import { MY_CONSTANT } from '../../src/constants';
describe('Cypress', () => {
...
})
导入的源文件如下所示:
// src/constants.ts
export const MY_CONSTANT = 'foo';
Cypress 测试失败是由源目录中的 Jest 测试文件引起的:
ERROR in /my-app/src/App.test.tsx(5,1)
Cannot find name 'test'. Do you need to install type definitions for a test runner? Try`npm i @types/jest`or`npm i @types/mocha`.
已安装 Jest 类型定义。此外,我试图在 Cypress tsconfig 中排除有问题的 Jest 测试,但无济于事。
// cypress/tsconfig.json
{
...
"exclude": [
"../src/App.test.tsx"
],
...
}
这是一个最小的repo that reproduces my problem。
最后,澄清我为什么要从源目录将东西导入 Cypress 测试 - 导入的变量旨在成为 DOM 选择器或返回 DOM 选择器的函数,这样选择器就不会在测试中硬编码。
【问题讨论】:
-
您是否考虑过在 TS 中也编写 cypress 测试?
标签: reactjs typescript create-react-app cypress