You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否应在仓库添加test.log?如何配置Jest输出不含测试时长

Answers

1. Stripping Duration from Jest Verbose Output

Jest doesn’t have a built-in flag to remove just the duration timestamps from verbose mode output, but you’ve got two solid options to fix this:

Option 1: Custom Reporter

Create a lightweight custom reporter that mimics verbose mode but omits durations. Make a file called custom-test-reporter.js in your project root:

const DefaultReporter = require('jest-cli/build/reporters/default_reporter').default;

class NoDurationReporter extends DefaultReporter {
  logTestResult(testResult) {
    // Print the test file path
    this.log(testResult.testPath);
    // Print each test name with status, no duration
    testResult.testResults.forEach(({ fullName, status }) => {
      const statusIcon = status === 'passed' ? '✓' : '✗';
      this.log(`  ${statusIcon} ${fullName}`);
    });
    this.log(); // Add spacing between test files
  }
}

module.exports = NoDurationReporter;

Update your Jest config (jest.config.js) to use this reporter instead of the default verbose setup:

module.exports = {
  reporters: [['./custom-test-reporter.js']],
  verbose: false, // Disable default verbose since we're using our custom reporter
};

Option 2: Post-Process Output with a Script

If you don’t want to mess with custom reporters, run Jest with verbose mode and pipe the output through a regex to strip durations:

jest --verbose | sed -E 's/ \([0-9]+ms\)$//' > test.log

This command removes any trailing (Xms) from each test line before saving to test.log.


2. Is Tracking Test Logs in the Repo a Good Idea?

Your approach is similar to snapshot testing for test structure, and it’s absolutely valid—here’s the breakdown of pros, cons, and critical things you might have missed:

Upsides

  • Transparent PR Reviews: Test additions, deletions, or renames become instantly visible in PR diffs, so reviewers can catch unintended changes quickly.
  • Prevent Accidental Breakage: The CI check ensures no one accidentally deletes a test or renames one without updating the log, which helps maintain test coverage integrity.
  • Human-Readable Record: Unlike machine-generated snapshots, the log is easy to scan and understand at a glance.

Critical Pitfalls to Fix

  • Test Order Randomization: Jest runs tests in random order by default. This means your test.log will have a different order every run, even if no tests changed—breaking your CI check immediately. Fix this by:
    • Adding --no-randomize to your Jest command to disable random ordering, or
    • Modifying your custom reporter to sort test names alphabetically before logging.
  • Merge Conflicts: If multiple PRs modify the test suite, test.log will almost certainly have merge conflicts that need manual fixing. Consider adding a pre-commit hook to auto-regenerate the log to reduce this pain.
  • Dynamic Test Noise: If you have tests generated from dynamic data (e.g., JSON files), the log will change whenever that data updates. Either exclude these tests from the log or handle them explicitly to avoid false positives.
  • Status vs Structure: Don’t include pass/fail statuses in the log—failing tests will trigger CI failures even if the test structure hasn’t changed. Stick to logging only test paths and full names.

Final Verdict

This approach works well if you address the test ordering issue and keep the log focused on static test structure. It’s a simple way to add guardrails for your test suite and make PR reviews more informed. Just automate the log regeneration as much as possible to minimize developer friction.


内容的提问来源于stack exchange,提问作者Robert Dorna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 16:23:30