11import assert from 'node:assert/strict' ;
22import test from 'node:test' ;
33import { createGraphExplorer } from '../../src/graph-explorer/explorer.js' ;
4- import type { FileNode , Import , ImportMaps , ModuleGraph } from '../../src/types/module-graph.js' ;
4+ import type { ModuleGraph } from '../../src/types/module-graph.js' ;
5+ import { baseFileNode , baseImportMaps , getBaseImport } from '../helpers/baseNodeObjects.js' ;
56import { resolve } from '../helpers/resolve.js' ;
67
78const createGraph = ( ) : ModuleGraph => new Map ( ) ;
@@ -11,33 +12,7 @@ const filePath2 = resolve('module-2.ts');
1112const filePath3 = resolve ( 'module-3.ts' ) ;
1213const filePath4 = resolve ( 'module-4.ts' ) ;
1314
14- const baseFileNode : FileNode = {
15- imports : { internal : new Map ( ) , external : new Set ( ) , unresolved : new Set ( ) , resolved : new Set ( ) , imports : new Set ( ) } ,
16- exports : new Map ( ) ,
17- duplicates : [ ] ,
18- scripts : new Set ( ) ,
19- traceRefs : new Set ( ) ,
20- } ;
21-
22- const baseImportMaps : ImportMaps = {
23- refs : new Set ( ) ,
24- import : new Map ( ) ,
25- importAs : new Map ( ) ,
26- importNs : new Map ( ) ,
27- reExport : new Map ( ) ,
28- reExportAs : new Map ( ) ,
29- reExportNs : new Map ( ) ,
30- } ;
31-
32- const baseImport : Import = {
33- specifier : './module-1' ,
34- filePath : filePath1 ,
35- identifier : 'identifier' ,
36- isTypeOnly : false ,
37- pos : 0 ,
38- line : 0 ,
39- col : 1 ,
40- } ;
15+ const baseImport = getBaseImport ( filePath1 ) ;
4116
4217test ( 'should detect simple circular dependency (A -> B -> A)' , ( ) => {
4318 const graph = createGraph ( ) ;
@@ -46,21 +21,17 @@ test('should detect simple circular dependency (A -> B -> A)', () => {
4621 graph . set ( filePath1 , {
4722 ...baseFileNode ,
4823 imports : {
24+ ...baseFileNode . imports ,
4925 internal : new Map ( [ [ filePath2 , { ...baseImportMaps } ] ] ) ,
50- external : new Set ( ) ,
51- unresolved : new Set ( ) ,
52- resolved : new Set ( ) ,
5326 imports : new Set ( [ { ...baseImport , specifier : './module-2' , filePath : filePath2 } ] ) ,
5427 } ,
5528 } ) ;
5629
5730 graph . set ( filePath2 , {
5831 ...baseFileNode ,
5932 imports : {
33+ ...baseFileNode . imports ,
6034 internal : new Map ( [ [ filePath1 , { ...baseImportMaps } ] ] ) ,
61- external : new Set ( ) ,
62- unresolved : new Set ( ) ,
63- resolved : new Set ( ) ,
6435 imports : new Set ( [ { ...baseImport , specifier : './module-1' , filePath : filePath1 , identifier : 'bar' } ] ) ,
6536 } ,
6637 } ) ;
@@ -80,32 +51,26 @@ test('should detect longer circular dependency (A -> B -> C -> A)', () => {
8051 graph . set ( filePath1 , {
8152 ...baseFileNode ,
8253 imports : {
54+ ...baseFileNode . imports ,
8355 internal : new Map ( [ [ filePath2 , { ...baseImportMaps } ] ] ) ,
84- external : new Set ( ) ,
85- unresolved : new Set ( ) ,
86- resolved : new Set ( ) ,
8756 imports : new Set ( [ { ...baseImport , specifier : './module-2' , filePath : filePath2 } ] ) ,
8857 } ,
8958 } ) ;
9059
9160 graph . set ( filePath2 , {
9261 ...baseFileNode ,
9362 imports : {
63+ ...baseFileNode . imports ,
9464 internal : new Map ( [ [ filePath3 , { ...baseImportMaps } ] ] ) ,
95- external : new Set ( ) ,
96- unresolved : new Set ( ) ,
97- resolved : new Set ( ) ,
9865 imports : new Set ( [ { ...baseImport , specifier : './module-3' , filePath : filePath3 , identifier : 'bar' } ] ) ,
9966 } ,
10067 } ) ;
10168
10269 graph . set ( filePath3 , {
10370 ...baseFileNode ,
10471 imports : {
72+ ...baseFileNode . imports ,
10573 internal : new Map ( [ [ filePath1 , { ...baseImportMaps } ] ] ) ,
106- external : new Set ( ) ,
107- unresolved : new Set ( ) ,
108- resolved : new Set ( ) ,
10974 imports : new Set ( [ { ...baseImport , specifier : './module-1' , filePath : filePath1 , identifier : 'baz' } ] ) ,
11075 } ,
11176 } ) ;
@@ -126,21 +91,17 @@ test('should returns empty array when no cycles exist', () => {
12691 graph . set ( filePath1 , {
12792 ...baseFileNode ,
12893 imports : {
94+ ...baseFileNode . imports ,
12995 internal : new Map ( [ [ filePath2 , { ...baseImportMaps } ] ] ) ,
130- external : new Set ( ) ,
131- unresolved : new Set ( ) ,
132- resolved : new Set ( ) ,
13396 imports : new Set ( [ { ...baseImport , specifier : './module-2' , filePath : filePath2 } ] ) ,
13497 } ,
13598 } ) ;
13699
137100 graph . set ( filePath2 , {
138101 ...baseFileNode ,
139102 imports : {
103+ ...baseFileNode . imports ,
140104 internal : new Map ( [ [ filePath3 , { ...baseImportMaps } ] ] ) ,
141- external : new Set ( ) ,
142- unresolved : new Set ( ) ,
143- resolved : new Set ( ) ,
144105 imports : new Set ( [ { ...baseImport , specifier : './module-3' , filePath : filePath3 } ] ) ,
145106 } ,
146107 } ) ;
@@ -160,21 +121,17 @@ test('should skip type-only imports', () => {
160121 graph . set ( filePath1 , {
161122 ...baseFileNode ,
162123 imports : {
124+ ...baseFileNode . imports ,
163125 internal : new Map ( [ [ filePath2 , { ...baseImportMaps } ] ] ) ,
164- external : new Set ( ) ,
165- unresolved : new Set ( ) ,
166- resolved : new Set ( ) ,
167126 imports : new Set ( [ { ...baseImport , specifier : './module-2' , filePath : filePath2 } ] ) ,
168127 } ,
169128 } ) ;
170129
171130 graph . set ( filePath2 , {
172131 ...baseFileNode ,
173132 imports : {
133+ ...baseFileNode . imports ,
174134 internal : new Map ( [ [ filePath1 , { ...baseImportMaps } ] ] ) ,
175- external : new Set ( ) ,
176- unresolved : new Set ( ) ,
177- resolved : new Set ( ) ,
178135 imports : new Set ( [ { ...baseImport , specifier : './module-1' , identifier : 'Type' , isTypeOnly : true } ] ) ,
179136 } ,
180137 } ) ;
@@ -192,43 +149,35 @@ test('should respect maxDepth', () => {
192149 graph . set ( filePath1 , {
193150 ...baseFileNode ,
194151 imports : {
152+ ...baseFileNode . imports ,
195153 internal : new Map ( [ [ filePath2 , { ...baseImportMaps } ] ] ) ,
196- external : new Set ( ) ,
197- unresolved : new Set ( ) ,
198- resolved : new Set ( ) ,
199154 imports : new Set ( [ { ...baseImport , specifier : './module-2' , filePath : filePath2 } ] ) ,
200155 } ,
201156 } ) ;
202157
203158 graph . set ( filePath2 , {
204159 ...baseFileNode ,
205160 imports : {
161+ ...baseFileNode . imports ,
206162 internal : new Map ( [ [ filePath3 , { ...baseImportMaps } ] ] ) ,
207- external : new Set ( ) ,
208- unresolved : new Set ( ) ,
209- resolved : new Set ( ) ,
210163 imports : new Set ( [ { ...baseImport , specifier : './module-3' , filePath : filePath3 , identifier : 'bar' } ] ) ,
211164 } ,
212165 } ) ;
213166
214167 graph . set ( filePath3 , {
215168 ...baseFileNode ,
216169 imports : {
170+ ...baseFileNode . imports ,
217171 internal : new Map ( [ [ filePath4 , { ...baseImportMaps } ] ] ) ,
218- external : new Set ( ) ,
219- unresolved : new Set ( ) ,
220- resolved : new Set ( ) ,
221172 imports : new Set ( [ { ...baseImport , specifier : './module-4' , filePath : filePath4 , identifier : 'baz' } ] ) ,
222173 } ,
223174 } ) ;
224175
225176 graph . set ( filePath4 , {
226177 ...baseFileNode ,
227178 imports : {
179+ ...baseFileNode . imports ,
228180 internal : new Map ( [ [ filePath1 , { ...baseImportMaps } ] ] ) ,
229- external : new Set ( ) ,
230- unresolved : new Set ( ) ,
231- resolved : new Set ( ) ,
232181 imports : new Set ( [ { ...baseImport , specifier : './module-1' , filePath : filePath1 , identifier : 'qux' } ] ) ,
233182 } ,
234183 } ) ;
0 commit comments