-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
197 lines (171 loc) · 7.61 KB
/
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
/* eslint-disable no-multi-spaces, max-len */
import test from 'ava';
import R from 'ramda';
import esDepsDeep from './index';
import kit from 'es-dep-kit';
import { mock as depMock } from 'es-dep-unit';
test('basic one', async t => {
const _ = await esDepsDeep(['./fixtures/basic/first/second/index.js']);
const dep = depMock(['fixtures', 'basic']);
t.deepEqual(_[0], dep(null, null, './first/second/index.js'));
t.is(_.length, 1);
});
test('basic two', async t => {
const _ = await esDepsDeep(['./fixtures/basic/first/index.js']);
const dep = depMock(['fixtures', 'basic']);
t.deepEqual(_[0], dep(null, null, './first/index.js'));
t.deepEqual(_[1], dep('./second/index.js', './first/index.js', './first/second/index.js'));
t.is(_.length, 2);
});
test('basic three', async t => {
const _ = await esDepsDeep(['./fixtures/basic/index.js']);
const dep = depMock(['fixtures', 'basic']);
t.deepEqual(_[0], dep(null, null, './index.js'));
t.deepEqual(_[1], dep('./first/index.js', './index.js', './first/index.js'));
t.deepEqual(_[2], dep('./second/index.js', './first/index.js', './first/second/index.js'));
t.is(_.length, 3);
});
test('basic plus', async t => {
const _ = await esDepsDeep(['./fixtures/basic-plus/index.js']);
const dep = depMock(['fixtures', 'basic-plus']);
t.deepEqual(_[0], dep(null, null, './index.js'));
t.deepEqual(_[1], dep('./first/index.js', './index.js', './first/index.js'));
t.deepEqual(_[2], dep('./second/index.js', './first/index.js', './first/second/index.js'));
t.deepEqual(_[3], dep('./three/index.js', './first/second/index.js', './first/second/three/index.js'));
t.is(_.length, 4);
});
test('extended', async t => {
const _ = await esDepsDeep(['./fixtures/extended/index.js']);
const dep = depMock(['fixtures', 'extended']);
t.deepEqual(_[0], dep(null, null, './index.js'));
t.deepEqual(_[1], dep('./first/index.js', './index.js', './first/index.js'));
t.deepEqual(_[2], dep('./second/index.js', './first/index.js', './first/second/index.js'));
t.deepEqual(_[3], dep('../third/index.js', './first/index.js', './third/index.js'));
t.deepEqual(_[4], dep('../first/fourth/index.js', './third/index.js', './first/fourth/index.js'));
t.is(_.length, 5);
});
test('basic+extended', async t => {
const _ = await esDepsDeep(['./fixtures/basic', './fixtures/extended']);
t.is(_.length, 8);
});
test('resolve', async t => {
const _ = await esDepsDeep(['./fixtures/resolve']);
const dep = depMock(['fixtures', 'resolve']);
t.deepEqual(_[0], dep(null, null, './index.js'));
t.deepEqual(_[1], dep('./a', './index.js', './a.js'));
t.deepEqual(_[2], dep('./b', './index.js', './b/index.js'));
t.is(_.length, 3);
});
test('cyclic', async t => {
const _ = await esDepsDeep(['./fixtures/cyclic/main.js']);
const dep = depMock(['fixtures', 'cyclic']);
t.deepEqual(_[0], dep(null, null, './main.js'));
t.deepEqual(_[1], dep('./a.js', './main.js', './a.js'));
t.deepEqual(_[2], dep('./b.js', './main.js', './b.js'));
t.is(_.length, 3);
});
test('modules', async t => {
const _ = await esDepsDeep(['./fixtures/modules']);
const dep = depMock(['fixtures', 'modules']);
t.deepEqual(_[0], dep(null, null, './index.js'));
t.deepEqual(_[1], dep('deep-modules-unresolved', './index.js', null));
t.deepEqual(_[2], dep('./pew', './index.js', './pew.js'));
t.is(_.length, 3);
});
test('modules nested', async t => {
const _ = await esDepsDeep(['./fixtures/modules-nested']);
const dep = depMock(['fixtures', 'modules-nested']);
t.deepEqual(_[0], dep(null, null, './index.js'));
t.deepEqual(_[1], dep('meow', './index.js', './node_modules/meow/index.js'));
t.deepEqual(_[2], dep('purr', './node_modules/meow/index.js', './node_modules/meow/node_modules/purr/index.js'));
t.deepEqual(_[3], dep('./pew', './node_modules/meow/index.js', './node_modules/meow/pew/index.js'));
t.deepEqual(_[4], dep('./pew', './index.js', './pew.js'));
t.is(_.length, 5);
});
test('missing', async t => {
const _ = await esDepsDeep(['./fixtures/missing']);
const dep = depMock(['fixtures', 'missing']);
t.deepEqual(_[0], dep(null, null, './index.js'));
t.deepEqual(_[1], dep('./one.js', './index.js', './one.js'));
t.deepEqual(_[2], dep('./two.js', './index.js', './two.js'));
t.deepEqual(_[3], dep('./extra.js', './index.js', null));
t.is(_.length, 4);
});
test('builtin', async t => {
const _ = await esDepsDeep(['./fixtures/builtin']);
const dep = depMock(['fixtures', 'builtin']);
t.deepEqual(_[0], dep(null, null, './index.js'));
t.deepEqual(_[1], dep('path', './index.js', 'path'));
t.is(_.length, 2);
});
test('json', async t => {
const _ = await esDepsDeep(['./fixtures/json']);
const dep = depMock(['fixtures', 'json']);
t.deepEqual(_[0], dep(null, null, './index.js'));
t.deepEqual(_[1], dep('./person.json', './index.js', './person.json'));
t.deepEqual(_[2], dep('./list.json', './index.js', './list.json'));
t.is(_.length, 3);
});
test('not exclude at all, by default', async t => {
const _ = await esDepsDeep(['./fixtures/exclude']);
t.is(_.length, 5);
});
test('not exclude at all', async t => {
const _ = await esDepsDeep(['./fixtures/exclude'], { excludeFn: R.F });
t.is(_.length, 5);
});
test('exclude everything', async t => {
const _ = await esDepsDeep(['./fixtures/exclude'], { excludeFn: R.T });
t.is(_.length, 0);
});
test('exclude entry', async t => {
const _ = await esDepsDeep(['./fixtures/exclude'], { excludeFn: kit.isEntry });
t.is(_.length, 0);
});
test('exclude modules', async t => {
const _ = await esDepsDeep(['./fixtures/exclude'], { excludeFn: kit.isRequestedPackage });
t.is(_.length, 3);
});
test('exclude local files', async t => {
const _ = await esDepsDeep(['./fixtures/exclude'], { excludeFn: kit.isRequestedLocalFile });
t.is(_.length, 3);
});
test('exclude node_modules', async t => {
const _ = await esDepsDeep(['./fixtures/exclude'], { excludeFn: kit.isResolvedInNM });
t.is(_.length, 2);
});
test('exclude resolved', async t => {
const _ = await esDepsDeep(['./fixtures/exclude'], { excludeFn: kit.isResolved });
t.is(_.length, 0);
});
test('exclude not resolved', async t => {
const _ = await esDepsDeep(['./fixtures/exclude'], { excludeFn: kit.isNotResolved });
t.is(_.length, 5);
});
test('exclude third parties', async t => {
const _ = await esDepsDeep(['./fixtures/exclude'], { excludeFn: kit.isThirdParty });
const dep = depMock(['fixtures', 'exclude']);
t.deepEqual(_[0], dep(null, null, './index.js'));
t.deepEqual(_[1], dep('meow', './index.js', './node_modules/meow/index.js'));
t.deepEqual(_[2], dep('./pew', './index.js', './pew.js'));
t.is(_.length, 3);
});
test('filtering', async t => {
const _ = await esDepsDeep(['./fixtures/filtering']);
const files = _.filter(R.either(kit.isEntry, kit.isRequestedLocalFile));
const usedFiles = files.filter(kit.isResolved);
const extraFiles = files.filter(kit.isNotResolved);
t.is(usedFiles.length, 3);
t.is(extraFiles.length, 1);
const modules = _.filter(kit.isRequestedPackage);
const usedModules = modules.filter(kit.isResolved);
const extraModules = modules.filter(kit.isNotResolved);
t.is(usedModules.length, 2);
t.is(extraModules.length, 1);
});
test('unresolved', t => t.throws(esDepsDeep(['./fixtures/unresolved']), Error));
test('empty input', t => t.throws(esDepsDeep(), TypeError));
test('invalid files', t => t.throws(esDepsDeep(2), TypeError));
test('invalid files[item]', t => t.throws(esDepsDeep([2]), TypeError));
test('invalid options', t => t.throws(esDepsDeep('./fixtures/basic', 2), TypeError));
test('invalid options.excludeFn', t => t.throws(esDepsDeep('./fixtures/basic', { excludeFn: 2 }), TypeError));