|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { toProjectGraph } from '@code-pushup/test-utils'; |
| 3 | +import { filterProjectGraph } from './filter-project-graph'; |
| 4 | + |
| 5 | +describe('filterProjectGraph', () => { |
| 6 | + it('should exclude specified projects from nodes', () => { |
| 7 | + const projectGraph = toProjectGraph([ |
| 8 | + { name: 'client', type: 'app', data: { root: 'apps/client' } }, |
| 9 | + { name: 'server', type: 'app', data: { root: 'apps/server' } }, |
| 10 | + { name: 'models', type: 'lib', data: { root: 'libs/models' } }, |
| 11 | + ]); |
| 12 | + |
| 13 | + const filteredGraph = filterProjectGraph(projectGraph, ['client']); |
| 14 | + |
| 15 | + expect(Object.keys(filteredGraph.nodes)).not.toContain('client'); |
| 16 | + expect(Object.keys(filteredGraph.nodes)).toContain('server'); |
| 17 | + expect(Object.keys(filteredGraph.nodes)).toContain('models'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should exclude dependencies of excluded projects', () => { |
| 21 | + const projectGraph = toProjectGraph( |
| 22 | + [ |
| 23 | + { name: 'client', type: 'app', data: { root: 'apps/client' } }, |
| 24 | + { name: 'server', type: 'app', data: { root: 'apps/server' } }, |
| 25 | + { name: 'models', type: 'lib', data: { root: 'libs/models' } }, |
| 26 | + ], |
| 27 | + { |
| 28 | + client: ['server'], |
| 29 | + server: ['models'], |
| 30 | + }, |
| 31 | + ); |
| 32 | + |
| 33 | + const filteredGraph = filterProjectGraph(projectGraph, ['client']); |
| 34 | + |
| 35 | + expect(Object.keys(filteredGraph.dependencies)).not.toContain('client'); |
| 36 | + expect(filteredGraph.dependencies['server']).toEqual([ |
| 37 | + { source: 'server', target: 'models', type: 'static' }, |
| 38 | + ]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should include all projects if exclude list is empty', () => { |
| 42 | + const projectGraph = toProjectGraph([ |
| 43 | + { name: 'client', type: 'app', data: { root: 'apps/client' } }, |
| 44 | + { name: 'server', type: 'app', data: { root: 'apps/server' } }, |
| 45 | + { name: 'models', type: 'lib', data: { root: 'libs/models' } }, |
| 46 | + ]); |
| 47 | + |
| 48 | + const filteredGraph = filterProjectGraph(projectGraph, []); |
| 49 | + |
| 50 | + expect(Object.keys(filteredGraph.nodes)).toContain('client'); |
| 51 | + expect(Object.keys(filteredGraph.nodes)).toContain('server'); |
| 52 | + expect(Object.keys(filteredGraph.nodes)).toContain('models'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should ignore non-existent projects in the exclude list', () => { |
| 56 | + const projectGraph = toProjectGraph([ |
| 57 | + { name: 'client', type: 'app', data: { root: 'apps/client' } }, |
| 58 | + { name: 'server', type: 'app', data: { root: 'apps/server' } }, |
| 59 | + { name: 'models', type: 'lib', data: { root: 'libs/models' } }, |
| 60 | + ]); |
| 61 | + |
| 62 | + const filteredGraph = filterProjectGraph(projectGraph, ['non-existent']); |
| 63 | + |
| 64 | + expect(Object.keys(filteredGraph.nodes)).toContain('client'); |
| 65 | + expect(Object.keys(filteredGraph.nodes)).toContain('server'); |
| 66 | + expect(Object.keys(filteredGraph.nodes)).toContain('models'); |
| 67 | + }); |
| 68 | +}); |
0 commit comments