File size: 5,507 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const mockFiles = ( filesToMock ) => {
	Object.entries( filesToMock ).forEach( ( [ file, content ] ) => {
		jest.mock( file, () => content, { virtual: true } );
	} );
};
mockFiles( {
	'/project/root/package.json': {
		name: 'root',
		version: '1.0.0',
		dependencies: {
			a: '^1.0.0',
			b: '^2.0.0',
		},
	},
	'/project/root/node_modules/a/package.json': {
		name: 'a',
		version: '1.1.1',
		dependencies: {},
	},
	'/project/root/node_modules/b/package.json': {
		name: 'b',
		version: '2.2.2',
		dependencies: {
			c: '3.2.1',
		},
	},
	'/project/root/node_modules/c/package.json': {
		name: 'c',
		version: '3.2.1',
	},
} );

const { candidates, getEffectiveTreeAsTree } = require( '../index' );

describe( 'Candidate generation', () => {
	it( 'Traverses the path', () => {
		const paths = Array.from( candidates( '/project/root/a/node_modules/b/node_modules/c' ) );
		expect( paths ).toStrictEqual( [
			'/project/root/a/node_modules/b/node_modules/c/node_modules',
			'/project/root/a/node_modules/b/node_modules',
			'/project/root/a/node_modules',
			'/project/root/node_modules',
			'/project/node_modules',
			'/node_modules',
			'/node_modules',
		] );
	} );
} );

describe( 'Effective tree generation', () => {
	beforeEach( () => {
		jest.spyOn( console, 'warn' ).mockImplementation();
	} );

	describe( 'as tree', () => {
		it( 'Generates the simplified tree', () => {
			const tree = getEffectiveTreeAsTree( '/project/root/package.json' );
			//prettier-ignore
			expect(tree).toEqual([
				'└─ root@1.0.0',
				'   β”œβ”€ a@1.1.1',
				'   └─ b@2.2.2',
				'      └─ c@3.2.1',
			].join('\n'));
		} );

		it( 'Detects circular dependencies', () => {
			mockFiles( {
				'/project/root/node_modules/c/package.json': {
					name: 'c',
					version: '3.2.1',
					dependencies: {
						b: '^2.0.0',
					},
				},
			} );

			jest.resetModules();
			// eslint-disable-next-line no-shadow
			const { getEffectiveTreeAsTree } = require( '../index' );
			const tree = getEffectiveTreeAsTree( '/project/root/package.json' );

			//prettier-ignore
			expect(tree).toEqual([
				'└─ root@1.0.0',
				'   β”œβ”€ a@1.1.1',
				'   └─ b@2.2.2',
				'      └─ c@3.2.1',
				'         └─ b@2.2.2: [Circular]'
			].join('\n'));
		} );

		it( 'Does not cache circular dependencies', async () => {
			jest.resetModules();
			mockFiles( {
				'/project/root/package.json': {
					name: 'root',
					version: '1.0.0',
					dependencies: {
						a: '^1.0.0',
						b: '^2.0.0',
					},
				},
				'/project/root/node_modules/a/package.json': {
					name: 'a',
					version: '1.1.1',
					dependencies: {
						b: '^2.0.0',
					},
				},
				'/project/root/node_modules/b/package.json': {
					name: 'b',
					version: '2.2.2',
					dependencies: {
						c: '3.2.1',
					},
				},
				'/project/root/node_modules/c/package.json': {
					name: 'c',
					version: '3.2.1',
					dependencies: {
						a: '^1.0.0',
					},
				},
			} );

			// eslint-disable-next-line no-shadow
			const { getEffectiveTreeAsTree } = require( '../index' );
			const tree = await getEffectiveTreeAsTree( '/project/root/package.json' );

			/**
			 * Note that when we find 'b' in the second chain (root->b->...), it has been processed previously in the first chain
			 * (root->a->b->...). However, because that first chain ended in a circular dependency, none of the packages in the chain
			 * was cached.
			 *
			 * This is a good thing. Otherwise, when we process 'b' the chain root->b we would pick 'b' from the cache,
			 * (equal to 'b->c->a[Circular]'), and we would end up with the chain root->b->c->a[Circular], which is not true.
			 *
			 * So tldr: branches with [Circular] dependencies are not cached, and this test is asserting that behaviour.
			 */

			//prettier-ignore
			expect(tree).toEqual([
				'└─ root@1.0.0',
				'   β”œβ”€ a@1.1.1',
				'   β”‚  └─ b@2.2.2',
				'   β”‚     └─ c@3.2.1',
				'   β”‚        └─ a@1.1.1: [Circular]',
				'   └─ b@2.2.2',
				'      └─ c@3.2.1',
				'         └─ a@1.1.1',
				'            └─ b@2.2.2: [Circular]',
			].join('\n'));
		} );
	} );

	describe( 'as list', () => {
		it( 'Complex tree', async () => {
			jest.resetModules();
			mockFiles( {
				'/project/root/package.json': {
					name: 'root',
					version: '1.0.0',
					dependencies: {
						a: '^1.0.0',
						b: '^2.0.0',
					},
				},
				'/project/root/node_modules/a/package.json': {
					name: 'a',
					version: '1.1.1',
					dependencies: {
						b: '^2.0.0',
					},
				},
				'/project/root/node_modules/b/package.json': {
					name: 'b',
					version: '2.2.2',
					dependencies: {
						c: '3.2.1',
					},
				},
				'/project/root/node_modules/c/package.json': {
					name: 'c',
					version: '3.2.1',
					dependencies: {
						a: '^1.0.0',
					},
				},
			} );

			// eslint-disable-next-line no-shadow
			const { getEffectiveTreeAsList } = require( '../index' );
			const tree = await getEffectiveTreeAsList( '/project/root/package.json' );

			//prettier-ignore
			expect(tree).toEqual([
				'root@1.0.0',
				'root@1.0.0 a@1.1.1',
				'root@1.0.0 a@1.1.1 b@2.2.2',
				'root@1.0.0 a@1.1.1 b@2.2.2 c@3.2.1',
				'root@1.0.0 a@1.1.1 b@2.2.2 c@3.2.1 a@1.1.1 [Circular]',
				'root@1.0.0 b@2.2.2',
				'root@1.0.0 b@2.2.2 c@3.2.1',
				'root@1.0.0 b@2.2.2 c@3.2.1 a@1.1.1',
				'root@1.0.0 b@2.2.2 c@3.2.1 a@1.1.1 b@2.2.2 [Circular]',
			].join('\n'));
		} );
	} );

	afterEach( () => {
		console.warn.mockRestore();
	} );
} );