Repository: facebook/jest
File: packages/jest-test-sequencer/src/index.ts (TypeScript)

Code before:
        return failedA ? -1 : 1;
      } else if (hasTimeA != (testB.duration != null)) {
        // If only one of two tests has timing information, run it last
        return hasTimeA ? 1 : -1;
      } else if (testA.duration != null && testB.duration != null) {
        return testA.duration < testB.duration ? 1 : -1;
      } else {
        return fileSize(testA) < fileSize(testB) ? 1 : -1;
      }
    });
  }

  allFailedTests(tests: Array<Test>): Array<Test> | Promise<Array<Test>> {
    const hasFailed = (cache: Cache, test: Test) =>
      cache[test.path]?.[0] === FAIL;
    return this.sort(
      tests.filter(test => hasFailed(this._getCache(test), test)),
    );
  }

  cacheResults(tests: Array<Test>, results: AggregatedResult): void {
    const map = Object.create(null) as Record<string, Test | undefined>;
    tests.forEach(test => (map[test.path] = test));
    results.testResults.forEach(testResult => {
      const test = map[testResult.testFilePath];
      if (test != null && !testResult.skipped) {
        const cache = this._getCache(test);
        const perf = testResult.perfStats;
        const testRuntime =
          perf.runtime ?? test.duration ?? perf.end - perf.start;
        cache[testResult.testFilePath] = [
          testResult.numFailingTests ? FAIL : SUCCESS,
          testRuntime || 0,
        ];
      }
    });

    this._cache.forEach((cache, context) =>
      fs.writeFileSync(this._getCachePath(context), JSON.stringify(cache)),
    );
  }

  private hasFailed(test: Test) {
    const cache = this._getCache(test);
    return cache[test.path]?.[0] === FAIL;
  }

  private time(test: Test) {
    const cache = this._getCache(test);
    return cache[test.path]?.[1];
  }

Review comment (suggestion):
fix is falling back to `perf.end - perf.start` (as it was before #9366), but also checking `test.duration` seems sensible to me.

Code after:
        return failedA ? -1 : 1;
      } else if (hasTimeA != (testB.duration != null)) {
        // If only one of two tests has timing information, run it last
        return hasTimeA ? 1 : -1;
      } else if (testA.duration != null && testB.duration != null) {
        return testA.duration < testB.duration ? 1 : -1;
      } else {
        return fileSize(testA) < fileSize(testB) ? 1 : -1;
      }
    });
  }

  allFailedTests(tests: Array<Test>): Array<Test> | Promise<Array<Test>> {
    return this.sort(tests.filter(test => this.hasFailed(test)));
  }

  cacheResults(tests: Array<Test>, results: AggregatedResult): void {
    const map = Object.create(null) as Record<string, Test | undefined>;
    tests.forEach(test => (map[test.path] = test));
    results.testResults.forEach(testResult => {
      const test = map[testResult.testFilePath];
      if (test != null && !testResult.skipped) {
        const cache = this._getCache(test);
        const perf = testResult.perfStats;
        const testRuntime =
          perf.runtime ?? test.duration ?? perf.end - perf.start;
        cache[testResult.testFilePath] = [
          testResult.numFailingTests > 0 ? FAIL : SUCCESS,
          testRuntime || 0,
        ];
      }
    });

    this._cache.forEach((cache, context) =>
      fs.writeFileSync(this._getCachePath(context), JSON.stringify(cache)),
    );
  }

  private hasFailed(test: Test) {
    const cache = this._getCache(test);
    return cache[test.path]?.[0] === FAIL;
  }

  private time(test: Test) {
    const cache = this._getCache(test);
    return cache[test.path]?.[1];
  }
}