/
dependencyExtractor.test.js
286 lines (255 loc) · 7.96 KB
/
dependencyExtractor.test.js
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {extract} from '../dependencyExtractor';
import isRegExpSupported from '../isRegExpSupported';
const COMMENT_NO_NEG_LB = isRegExpSupported('(?<!\\.\\s*)') ? '' : '//';
describe('dependencyExtractor', () => {
it('should not extract dependencies inside comments', () => {
const code = `
// import a from 'ignore-line-comment';
// import 'ignore-line-comment';
// import './ignore-line-comment';
// require('ignore-line-comment');
/*
* import a from 'ignore-block-comment';
* import './ignore-block-comment';
* import 'ignore-block-comment';
* require('ignore-block-comment');
*/
`;
expect(extract(code)).toEqual(new Set());
});
it('should not extract dependencies inside comments (windows line endings)', () => {
const code = [
'// const module1 = require("module1");',
'/**',
' * const module2 = require("module2");',
' */',
].join('\r\n');
expect(extract(code)).toEqual(new Set([]));
});
it('should not extract dependencies inside comments (unicode line endings)', () => {
const code = [
'// const module1 = require("module1");\u2028',
'// const module1 = require("module2");\u2029',
'/*\u2028',
'const module2 = require("module3");\u2029',
' */',
].join('');
expect(extract(code)).toEqual(new Set([]));
});
it('should extract dependencies from `import` statements', () => {
const code = `
// Good
import * as depNS from 'dep1';
import {
a as aliased_a,
b,
} from 'dep2';
import depDefault from 'dep3';
import * as depNS, {
a as aliased_a,
b,
}, depDefault from 'dep4';
// Bad
${COMMENT_NO_NEG_LB} foo . import ('inv1');
${COMMENT_NO_NEG_LB} foo . export ('inv2');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});
// https://github.com/facebook/jest/issues/8547
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_a_module_for_its_side_effects_only
it('should extract dependencies from side-effect only `import` statements', () => {
const code = `
// Good
import './side-effect-dep1';
import 'side-effect-dep2';
// Bad
import ./inv1;
import inv2
`;
expect(extract(code)).toEqual(
new Set(['./side-effect-dep1', 'side-effect-dep2']),
);
});
it('should not extract dependencies from `import type/typeof` statements', () => {
const code = `
// Bad
import typeof {foo} from 'inv1';
import type {foo} from 'inv2';
`;
expect(extract(code)).toEqual(new Set([]));
});
it('should extract dependencies from `export` statements', () => {
const code = `
// Good
export * as depNS from 'dep1';
export {
a as aliased_a,
b,
} from 'dep2';
export depDefault from 'dep3';
export * as depNS, {
a as aliased_a,
b,
}, depDefault from 'dep4';
// Bad
${COMMENT_NO_NEG_LB} foo . export ('inv1');
${COMMENT_NO_NEG_LB} foo . export ('inv2');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});
it('should extract dependencies from `export-from` statements', () => {
const code = `
// Good
export * as depNS from 'dep1';
export {
a as aliased_a,
b,
} from 'dep2';
export depDefault from 'dep3';
export * as depNS, {
a as aliased_a,
b,
}, depDefault from 'dep4';
// Bad
${COMMENT_NO_NEG_LB} foo . export ('inv1');
${COMMENT_NO_NEG_LB} foo . export ('inv2');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});
it('should not extract dependencies from `export type/typeof` statements', () => {
const code = `
// Bad
export typeof {foo} from 'inv1';
export type {foo} from 'inv2';
`;
expect(extract(code)).toEqual(new Set([]));
});
it('should extract dependencies from dynamic `import` calls', () => {
const code = `
// Good
import('dep1').then();
const dep2 = await import(
"dep2",
);
if (await import(\`dep3\`)) {}
// Bad
${COMMENT_NO_NEG_LB} await foo . import('inv1')
await ximport('inv2');
importx('inv3');
import('inv4', 'inv5');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3']));
});
it('should extract dependencies from `require` calls', () => {
const code = `
// Good
require('dep1');
const dep2 = require(
"dep2",
);
if (require(\`dep3\`).cond) {}
// Bad
${COMMENT_NO_NEG_LB} foo . require('inv1')
xrequire('inv2');
requirex('inv3');
require('inv4', 'inv5');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3']));
});
it('should extract dependencies from `require.requireActual` calls', () => {
const code = `
// Good
require.requireActual('dep1');
const dep2 = require.requireActual(
"dep2",
);
if (require.requireActual(\`dep3\`).cond) {}
require
.requireActual('dep4');
// Bad
${COMMENT_NO_NEG_LB} foo . require.requireActual('inv1')
xrequire.requireActual('inv2');
require.requireActualx('inv3');
require.requireActual('inv4', 'inv5');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});
it('should extract dependencies from `require.requireMock` calls', () => {
const code = `
// Good
require.requireMock('dep1');
const dep2 = require.requireMock(
"dep2",
);
if (require.requireMock(\`dep3\`).cond) {}
require
.requireMock('dep4');
// Bad
${COMMENT_NO_NEG_LB} foo . require.requireMock('inv1')
xrequire.requireMock('inv2');
require.requireMockx('inv3');
require.requireMock('inv4', 'inv5');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});
it('should extract dependencies from `jest.requireActual` calls', () => {
const code = `
// Good
jest.requireActual('dep1');
const dep2 = jest.requireActual(
"dep2",
);
if (jest.requireActual(\`dep3\`).cond) {}
require
.requireActual('dep4');
// Bad
${COMMENT_NO_NEG_LB} foo . jest.requireActual('inv1')
xjest.requireActual('inv2');
jest.requireActualx('inv3');
jest.requireActual('inv4', 'inv5');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});
it('should extract dependencies from `jest.requireMock` calls', () => {
const code = `
// Good
jest.requireMock('dep1');
const dep2 = jest.requireMock(
"dep2",
);
if (jest.requireMock(\`dep3\`).cond) {}
require
.requireMock('dep4');
// Bad
${COMMENT_NO_NEG_LB} foo . jest.requireMock('inv1')
xjest.requireMock('inv2');
jest.requireMockx('inv3');
jest.requireMock('inv4', 'inv5');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});
it('should extract dependencies from `jest.genMockFromModule` calls', () => {
const code = `
// Good
jest.genMockFromModule('dep1');
const dep2 = jest.genMockFromModule(
"dep2",
);
if (jest.genMockFromModule(\`dep3\`).cond) {}
require
.requireMock('dep4');
// Bad
${COMMENT_NO_NEG_LB} foo . jest.genMockFromModule('inv1')
xjest.genMockFromModule('inv2');
jest.genMockFromModulex('inv3');
jest.genMockFromModule('inv4', 'inv5');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});
});