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

时间:2019-03-07 16:07:46

标签: reactjs typescript jestjs react-scripts

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

|- /src/path
|  |- List.tsx
|  |- List.test.tsx

当尝试运行npx jest或使用全局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

运行npm test(依次运行react-scripts test中的脚本package.json)可以正常运行,并且能够找到并运行项目中的所有测试(以监视方式)。 / p>

有什么办法解决这个问题吗?

环境

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

jest.config.js

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

2 个答案:

答案 0 :(得分:1)

根据this answer,用create-react-app构建的项目并不打算直接用jest进行测试。

react-scripts test应该代替jest来利用CRA设置生成的Jest配置。

答案 1 :(得分:0)

您是否尝试过使用/作为目录分隔符?

来自Jest docs

  

有关可指定模式的详细信息,请参见micromatch软件包。

来自Micromatch docs

  

Micromatch专门并明确保留反斜杠,以用于以glob模式(即使在Windows上)转义字符。这与bash行为一致。

     

...

     

换句话说,由于\\被保留为glob中的转义字符,因此在窗口path.join('foo', '*')上将产生foo\\*,这告诉微匹配将*匹配为文字字符。这与bash相同。

所以我会尝试:

module.exports = {
  testMatch: [ '**/*.test.tsx' ]
};