Skip to content

Commit ddc6693

Browse files
committed
Fix up and reuse base graph objects in tests
1 parent cabee8e commit ddc6693

File tree

6 files changed

+101
-286
lines changed

6 files changed

+101
-286
lines changed

packages/knip/test/graph-explorer/contention.test.ts

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import assert from 'node:assert/strict';
22
import test from 'node:test';
33
import { createGraphExplorer } from '../../src/graph-explorer/explorer.js';
4-
import type { Export, FileNode, ImportMaps, ModuleGraph } from '../../src/types/module-graph.js';
4+
import type { ModuleGraph } from '../../src/types/module-graph.js';
5+
import { baseExport, baseFileNode, baseImportMaps } from '../helpers/baseNodeObjects.js';
56
import { resolve } from '../helpers/resolve.js';
67

78
const createGraph = (): ModuleGraph => new Map();
@@ -11,36 +12,6 @@ const filePath2 = resolve('module-2.ts');
1112
const filePath3 = resolve('module-3.ts');
1213
const 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 baseExport: Export = {
33-
identifier: 'identifier',
34-
pos: 0,
35-
line: 1,
36-
col: 0,
37-
type: 'unknown',
38-
members: [],
39-
jsDocTags: new Set(),
40-
refs: [0, false],
41-
fixes: [],
42-
};
43-
4415
test('should detect branching (export re-exported through multiple paths)', () => {
4516
const graph = createGraph();
4617
const entryPaths = new Set<string>();
@@ -54,42 +25,33 @@ test('should detect branching (export re-exported through multiple paths)', () =
5425
...baseFileNode,
5526
exports: new Map([['identifier', baseExport]]),
5627
imports: {
28+
...baseFileNode.imports,
5729
internal: new Map([
5830
[filePath1, { ...baseImportMaps, reExport: new Map([['identifier', new Set([filePath2])]]) }],
5931
]),
60-
external: new Set(),
61-
unresolved: new Set(),
62-
resolved: new Set(),
63-
imports: new Set(),
6432
},
6533
});
6634

6735
graph.set(filePath3, {
6836
...baseFileNode,
6937
exports: new Map([['identifier', baseExport]]),
7038
imports: {
39+
...baseFileNode.imports,
7140
internal: new Map([
7241
[filePath1, { ...baseImportMaps, reExport: new Map([['identifier', new Set([filePath3])]]) }],
7342
]),
74-
external: new Set(),
75-
unresolved: new Set(),
76-
resolved: new Set(),
77-
imports: new Set(),
7843
},
7944
});
8045

8146
graph.set(filePath4, {
8247
...baseFileNode,
8348
exports: new Map([['identifier', baseExport]]),
8449
imports: {
50+
...baseFileNode.imports,
8551
internal: new Map([
8652
[filePath2, { ...baseImportMaps, reExport: new Map([['identifier', new Set([filePath4])]]) }],
8753
[filePath3, { ...baseImportMaps, reExport: new Map([['identifier', new Set([filePath4])]]) }],
8854
]),
89-
external: new Set(),
90-
unresolved: new Set(),
91-
resolved: new Set(),
92-
imports: new Set(),
9355
},
9456
});
9557

@@ -119,14 +81,11 @@ test('should detect conflict (same identifier defined in multiple files)', () =>
11981
...baseFileNode,
12082
exports: new Map([['identifier', baseExport]]),
12183
imports: {
84+
...baseFileNode.imports,
12285
internal: new Map([
12386
[filePath1, { ...baseImportMaps, reExport: new Map([['identifier', new Set([filePath3])]]) }],
12487
[filePath2, { ...baseImportMaps, reExport: new Map([['identifier', new Set([filePath3])]]) }],
12588
]),
126-
external: new Set(),
127-
unresolved: new Set(),
128-
resolved: new Set(),
129-
imports: new Set(),
13089
},
13190
});
13291

@@ -153,13 +112,10 @@ test('should return empty map when no contention exists', () => {
153112
...baseFileNode,
154113
exports: new Map([['identifier', { ...baseExport, isReExport: true }]]),
155114
imports: {
115+
...baseFileNode.imports,
156116
internal: new Map([
157117
[filePath1, { ...baseImportMaps, reExport: new Map([['identifier', new Set([filePath2])]]) }],
158118
]),
159-
external: new Set(),
160-
unresolved: new Set(),
161-
resolved: new Set(),
162-
imports: new Set(),
163119
},
164120
});
165121

@@ -214,23 +170,17 @@ test('should detect conflict through star re-exports chain', () => {
214170
...baseFileNode,
215171
exports: new Map([['CONFLICT', { ...baseExport, identifier: 'CONFLICT' }]]),
216172
imports: {
173+
...baseFileNode.imports,
217174
internal: new Map([[filePath3, { ...baseImportMaps, reExport: new Map([['*', new Set([filePath2])]]) }]]),
218-
external: new Set(),
219-
unresolved: new Set(),
220-
resolved: new Set(),
221-
imports: new Set(),
222175
},
223176
});
224177

225178
graph.set(filePath1, {
226179
...baseFileNode,
227180
exports: new Map([['CONFLICT', { ...baseExport, identifier: 'CONFLICT' }]]),
228181
imports: {
182+
...baseFileNode.imports,
229183
internal: new Map([[filePath2, { ...baseImportMaps, reExport: new Map([['*', new Set([filePath1])]]) }]]),
230-
external: new Set(),
231-
unresolved: new Set(),
232-
resolved: new Set(),
233-
imports: new Set(),
234184
},
235185
});
236186

@@ -305,15 +255,12 @@ test('should detect conflict from source file aggregated by consumer', () => {
305255
...baseFileNode,
306256
exports: new Map([['identifier', { ...baseExport, isReExport: true }]]),
307257
imports: {
258+
...baseFileNode.imports,
308259
internal: new Map([
309260
[fileA, { ...baseImportMaps, reExport: new Map([['*', new Set([indexFile])]]) }],
310261
[fileB, { ...baseImportMaps, reExport: new Map([['*', new Set([indexFile])]]) }],
311262
[fileC, { ...baseImportMaps, reExport: new Map([['*', new Set([indexFile])]]) }],
312263
]),
313-
external: new Set(),
314-
unresolved: new Set(),
315-
resolved: new Set(),
316-
imports: new Set(),
317264
},
318265
});
319266

packages/knip/test/graph-explorer/cycles.test.ts

Lines changed: 16 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import assert from 'node:assert/strict';
22
import test from 'node:test';
33
import { 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';
56
import { resolve } from '../helpers/resolve.js';
67

78
const createGraph = (): ModuleGraph => new Map();
@@ -11,33 +12,7 @@ const filePath2 = resolve('module-2.ts');
1112
const filePath3 = resolve('module-3.ts');
1213
const 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

4217
test('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

Comments
 (0)