版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1 год назад
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { readFileSync, writeFileSync } from 'node:fs';
  2. const THRESHOLD_KEYS = ["lines", "functions", "statements", "branches"];
  3. class BaseCoverageProvider {
  4. updateThresholds({ configurationFile, coverageMap, thresholds }) {
  5. if (!configurationFile)
  6. throw new Error('Missing configurationFile. The "coverage.thresholdAutoUpdate" can only be enabled when configuration file is used.');
  7. const summary = coverageMap.getCoverageSummary();
  8. const thresholdsToUpdate = [];
  9. for (const key of THRESHOLD_KEYS) {
  10. const threshold = thresholds[key] || 100;
  11. const actual = summary[key].pct;
  12. if (actual > threshold)
  13. thresholdsToUpdate.push(key);
  14. }
  15. if (thresholdsToUpdate.length === 0)
  16. return;
  17. const originalConfig = readFileSync(configurationFile, "utf8");
  18. let updatedConfig = originalConfig;
  19. for (const threshold of thresholdsToUpdate) {
  20. const previousThreshold = (thresholds[threshold] || 100).toString();
  21. const pattern = new RegExp(`(${threshold}\\s*:\\s*)${previousThreshold.replace(".", "\\.")}`);
  22. const matches = originalConfig.match(pattern);
  23. if (matches)
  24. updatedConfig = updatedConfig.replace(matches[0], matches[1] + summary[threshold].pct);
  25. else
  26. console.error(`Unable to update coverage threshold ${threshold}. No threshold found using pattern ${pattern}`);
  27. }
  28. if (updatedConfig !== originalConfig) {
  29. console.log("Updating thresholds to configuration file. You may want to push with updated coverage thresholds.");
  30. writeFileSync(configurationFile, updatedConfig, "utf-8");
  31. }
  32. }
  33. resolveReporters(configReporters) {
  34. if (!Array.isArray(configReporters))
  35. return [[configReporters, {}]];
  36. const resolvedReporters = [];
  37. for (const reporter of configReporters) {
  38. if (Array.isArray(reporter)) {
  39. resolvedReporters.push([reporter[0], reporter[1] || {}]);
  40. } else {
  41. resolvedReporters.push([reporter, {}]);
  42. }
  43. }
  44. return resolvedReporters;
  45. }
  46. }
  47. export { BaseCoverageProvider };