coverage-exempt.spec.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Mechanical guard for the coverage-exempt roster: each entry's positional
  3. * filter and exclude glob must select the same non-empty file set out of the
  4. * repository's spec inventory, so a renamed suite cannot silently fall out of
  5. * the uninstrumented gate while its exclude goes stale.
  6. */
  7. import { globSync } from 'node:fs'
  8. import { resolve } from 'node:path'
  9. import { describe, expect, it } from 'vitest'
  10. import { coverageExemptHeavySuites } from './coverage-exempt.ts'
  11. const root = resolve(import.meta.dirname, '..')
  12. /** The spec inventory mirrored from vitest.config.ts testIncludes. */
  13. const allSpecs = new Set([
  14. ...globSync('packages/*/*/tests/**/*.spec.ts', { cwd: root }),
  15. ...globSync('packages/*/*/tests/**/*.spec.tsx', { cwd: root }),
  16. ...globSync('apps/*/tests/**/*.spec.ts', { cwd: root }),
  17. ...globSync('scripts/**/*.spec.ts', { cwd: root }),
  18. ].map(path => path.replaceAll('\\', '/')))
  19. function excludeMatches(exclude: string): string[] {
  20. return globSync(exclude, { cwd: root })
  21. .map(path => path.replaceAll('\\', '/'))
  22. .filter(path => allSpecs.has(path))
  23. .sort()
  24. }
  25. function filterMatches(filter: string): string[] {
  26. return [...allSpecs].filter(spec => spec.startsWith(filter)).sort()
  27. }
  28. describe('coverage-exempt roster', () => {
  29. it.each(coverageExemptHeavySuites.map(suite => [suite.filter, suite] as const))(
  30. 'filter and exclude select the same non-empty spec set for %s',
  31. (_filter, suite) => {
  32. const fromExclude = excludeMatches(suite.exclude)
  33. const fromFilter = filterMatches(suite.filter)
  34. expect(fromExclude.length).toBeGreaterThan(0)
  35. expect(fromFilter).toEqual(fromExclude)
  36. },
  37. )
  38. it('entries never overlap, so no suite is double-run or double-excluded', () => {
  39. const seen = new Map<string, string>()
  40. for (const suite of coverageExemptHeavySuites) {
  41. for (const spec of excludeMatches(suite.exclude)) {
  42. expect(seen.get(spec), `${spec} matched by ${seen.get(spec) ?? ''} and ${suite.exclude}`).toBeUndefined()
  43. seen.set(spec, suite.exclude)
  44. }
  45. }
  46. })
  47. })