coverage-exempt.spec.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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('examples/*/tests/**/*.spec.ts', { cwd: root }),
  18. ...globSync('scripts/**/*.spec.ts', { cwd: root }),
  19. ].map(path => path.replaceAll('\\', '/')))
  20. function excludeMatches(exclude: string): string[] {
  21. return globSync(exclude, { cwd: root })
  22. .map(path => path.replaceAll('\\', '/'))
  23. .filter(path => allSpecs.has(path))
  24. .sort()
  25. }
  26. function filterMatches(filter: string): string[] {
  27. return [...allSpecs].filter(spec => spec.startsWith(filter)).sort()
  28. }
  29. describe('coverage-exempt roster', () => {
  30. it.each(coverageExemptHeavySuites.map(suite => [suite.filter, suite] as const))(
  31. 'filter and exclude select the same non-empty spec set for %s',
  32. (_filter, suite) => {
  33. const fromExclude = excludeMatches(suite.exclude)
  34. const fromFilter = filterMatches(suite.filter)
  35. expect(fromExclude.length).toBeGreaterThan(0)
  36. expect(fromFilter).toEqual(fromExclude)
  37. },
  38. )
  39. it('entries never overlap, so no suite is double-run or double-excluded', () => {
  40. const seen = new Map<string, string>()
  41. for (const suite of coverageExemptHeavySuites) {
  42. for (const spec of excludeMatches(suite.exclude)) {
  43. expect(seen.get(spec), `${spec} matched by ${seen.get(spec) ?? ''} and ${suite.exclude}`).toBeUndefined()
  44. seen.set(spec, suite.exclude)
  45. }
  46. }
  47. })
  48. })