coverage-exempt.spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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('selects every Typert suite for the uninstrumented gate', () => {
  30. const typertSpecs = filterMatches('packages/typert/')
  31. const exemptSpecs = coverageExemptHeavySuites.flatMap(suite => excludeMatches(suite.exclude))
  32. .filter(spec => spec.startsWith('packages/typert/')).sort()
  33. expect(typertSpecs.length).toBeGreaterThan(0)
  34. expect(exemptSpecs).toEqual(typertSpecs)
  35. })
  36. it.each(coverageExemptHeavySuites.map(suite => [suite.filter, suite] as const))(
  37. 'filter and exclude select the same non-empty spec set for %s',
  38. (_filter, suite) => {
  39. const fromExclude = excludeMatches(suite.exclude)
  40. const fromFilter = filterMatches(suite.filter)
  41. expect(fromExclude.length).toBeGreaterThan(0)
  42. expect(fromFilter).toEqual(fromExclude)
  43. },
  44. )
  45. it('entries never overlap, so no suite is double-run or double-excluded', () => {
  46. const seen = new Map<string, string>()
  47. for (const suite of coverageExemptHeavySuites) {
  48. for (const spec of excludeMatches(suite.exclude)) {
  49. expect(seen.get(spec), `${spec} matched by ${seen.get(spec) ?? ''} and ${suite.exclude}`).toBeUndefined()
  50. seen.set(spec, suite.exclude)
  51. }
  52. }
  53. })
  54. })