Jest TypeScript 测试在 Jenkins 中失败

bin*_*les 1 jenkins typescript jestjs jenkins-pipeline ts-jest

我们有一个 React / TypeScript 项目,并使用 Jest + ts-jest 作为我们的测试运行器。在本地一切正常,但在我们的 Jenkins CI 中运行时失败。

以下是 Jenkins 控制台的片段:

No tests found, exiting with code 1
In /var/lib/jenkins/workspace/fix-jenkins-tests
  403 files checked.
  testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[jt]s?(x) - 38 matches
  testPathIgnorePatterns: /lib/, /node_modules/ - 0 matches
  testRegex:  - 0 matches
Pattern:  - 0 matches
error Command failed with exit code 1.
Run Code Online (Sandbox Code Playgroud)

奇怪的是,Jest 说“没有找到测试”,但也说“testMatch:... 38 个匹配”

小智 5

提问者的队友,这最终有点微妙。这里的罪魁祸首是 testPathIgnorePatterns 中的 /lib/。testPathIgnorePatterns与 testMatch 或 testRegex 找到的任何测试文件的完整路径进行匹配。

事实证明,我们的 Jenkins 实例的工作目录类似于/var/lib/jenkins/workspacetestMatch 会找到我们所有的测试文件,但 testPathIgnorePatterns 会在 /lib/ 上匹配它们并将它们全部排除。

<rootDir>以与环境无关的方式排除“/lib/”文件夹的正确方法是像这样使用令牌

testPathIgnorePatterns: [ '<rootDir>/lib/', ],