从命令行运行 jest 时“未找到测试”

Mar*_*vic 3 typescript reactjs jestjs react-scripts

在构建的 React 项目create-react-app中,测试文件位于他们需要测试的代码旁边的同一文件夹中,如下所示:

|- /src/path
|  |- List.tsx
|  |- List.test.tsx
Run Code Online (Sandbox Code Playgroud)

尝试运行npx jest或使用 global 时jest,我得到以下结果:

No tests found
In C:\src\path
  34 files checked.
  testMatch: **\*test.tsx,**/*test.tsx,src\**\*.test.tsx,src/**/*.test.tsx,C:\src\path\src\**\*.test.tsx,C:\src\path\src\**\*.test.tsx - 0 matches
  testPathIgnorePatterns: \\node_modules\\ - 34 matches
Pattern:  - 0 matches
Run Code Online (Sandbox Code Playgroud)

运行npm test-这在转弯运行脚本react-scripts testpackage.json-工作正常,并能够找到并运行项目中的所有测试(手表模式)。

知道如何解决这个问题吗?

环境

  • 节点: 10.15.0
  • npm: 6.4.1
  • 反应脚本: 2.1.3
  • 操作系统: Windows 10 1809 17763.316

开玩笑的配置文件

module.exports = {
  testMatch: [
    '**\\*test.tsx',
    '**/*test.tsx',
    'src\\**\\*.test.tsx',
    'src/**/*.test.tsx',
    '<rootDir>\\src\\**\\*.test.tsx',
    '<rootDir>/src/**/*.test.tsx',
    'src/.*|(.|/)(.test).tsx?$'
  ],
};
Run Code Online (Sandbox Code Playgroud)

Mar*_*vic 5

根据这个答案,用 构建的项目create-react-app并不意味着直接用jest.

react-scripts test应该使用而不是jest使用 CRA 设置生成的 Jest 配置。