moduleGraph.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { ModuleGraph } from 'node/utils/moduleGraph'
  2. describe('node/utils/moduleGraph', () => {
  3. let graph: ModuleGraph
  4. beforeEach(() => {
  5. graph = new ModuleGraph()
  6. })
  7. it('should correctly delete a module and its dependents', () => {
  8. graph.add('A', ['B', 'C'])
  9. graph.add('B', ['D'])
  10. graph.add('C', [])
  11. graph.add('D', [])
  12. expect(graph.delete('D')).toEqual(new Set(['D', 'B', 'A']))
  13. })
  14. it('should handle shared dependencies correctly', () => {
  15. graph.add('A', ['B', 'C'])
  16. graph.add('B', ['D'])
  17. graph.add('C', ['D']) // Shared dependency
  18. graph.add('D', [])
  19. expect(graph.delete('D')).toEqual(new Set(['A', 'B', 'C', 'D']))
  20. })
  21. it('merges dependencies correctly', () => {
  22. // Add module A with dependency B
  23. graph.add('A', ['B'])
  24. // Merge new dependency C into module A (B should remain)
  25. graph.add('A', ['C'])
  26. // Deleting B should remove A as well, since A depends on B.
  27. expect(graph.delete('B')).toEqual(new Set(['B', 'A']))
  28. })
  29. it('handles cycles gracefully', () => {
  30. // Create a cycle: A -> B, B -> C, C -> A.
  31. graph.add('A', ['B'])
  32. graph.add('B', ['C'])
  33. graph.add('C', ['A'])
  34. // Deleting any module in the cycle should delete all modules in the cycle.
  35. expect(graph.delete('A')).toEqual(new Set(['A', 'B', 'C']))
  36. })
  37. it('cleans up dependencies when deletion', () => {
  38. // Setup A -> B relationship.
  39. graph.add('A', ['B'])
  40. graph.add('B', [])
  41. // Deleting B should remove both B and A from the graph.
  42. expect(graph.delete('B')).toEqual(new Set(['B', 'A']))
  43. // After deletion, add modules again.
  44. graph.add('C', [])
  45. graph.add('A', ['C']) // Now A depends only on C.
  46. expect(graph.delete('C')).toEqual(new Set(['C', 'A']))
  47. })
  48. it('handles independent modules', () => {
  49. // Modules with no dependencies.
  50. graph.add('X', [])
  51. graph.add('Y', [])
  52. // Deletion of one should only remove that module.
  53. expect(graph.delete('X')).toEqual(new Set(['X']))
  54. expect(graph.delete('Y')).toEqual(new Set(['Y']))
  55. })
  56. })