require-glob.js
3.19 KB
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
import path from 'path';
import test from 'ava';
import requireGlob from '../src/require-glob';
test('should require no modules', async assert => {
const noneA = await requireGlob('./fixtures/bogu*.js');
const noneB = requireGlob.sync('./fixtures/bogu*.js');
assert.same(noneA, {});
assert.same(noneB, {});
});
test('should require a module', async assert => {
const oneA = await requireGlob('./fixtures/rand*.js');
const oneB = requireGlob.sync('./fixtures/rand*.js');
assert.same(typeof oneA.random, 'number');
assert.same(typeof oneB.random, 'number');
});
test('should require multiple modules', async assert => {
const shallowA = await requireGlob('./fixtures/shallow/**/*.js');
const shallowB = requireGlob.sync('./fixtures/shallow/**/*.js');
const expected = {
a: 'a',
b: 'b',
c: 'c',
d: {
e: 'f'
}
};
assert.same(shallowA, expected);
assert.same(shallowB, expected);
});
test('should require nested modules', async assert => {
const deepA = await requireGlob('./fixtures/deep/**/*.js');
const deepB = requireGlob.sync('./fixtures/deep/**/*.js');
const expected = {
a: {
a1: 'a1',
a2: 'a2'
},
b: {
b_bB: { // eslint-disable-line camelcase
_bB1: '_b.b1',
bB2: 'b.b2'
},
b1: 'b1',
b2: 'b2'
}
};
assert.same(deepA, expected);
assert.same(deepB, expected);
});
test('should bust cache', async assert => {
const a = await requireGlob('./fixtures/rand*.js');
const b = await requireGlob('./fixtures/rand*.js');
const c = await requireGlob('./fixtures/rand*.js', {bustCache: true});
const d = await requireGlob('./fixtures/rand*.js', {bustCache: true});
const e = await requireGlob('./fixtures/rand*.js');
assert.is(a.random, b.random);
assert.not(b.random, c.random);
assert.not(c.random, d.random);
assert.is(d.random, e.random);
});
test('should use custom cwd', async assert => {
const deep = await requireGlob('./test/**/deep/**/*.js', {cwd: path.dirname(__dirname)});
const expected = {
a: {
a1: 'a1',
a2: 'a2'
},
b: {
b_bB: { // eslint-disable-line camelcase
_bB1: '_b.b1',
bB2: 'b.b2'
},
b1: 'b1',
b2: 'b2'
}
};
assert.same(deep, expected);
});
test('should use custom mapper', async assert => {
function mapper(filePath, i) {
return {
shortPath: path.basename(filePath).toUpperCase(),
exports: i
};
}
const deep = requireGlob.sync('./fixtures/deep/**/*.js', {mapper});
const expected = {
A1: 0,
A2: 1,
_BB1: 2,
BB2: 3,
B1: 4,
B2: 5
};
assert.same(deep, expected);
});
test('should use custom reducer', async assert => {
function reducer(tree, file) {
if (!Array.isArray(tree)) {
tree = [];
}
tree.push(file.exports);
return tree;
}
const deep = await requireGlob('./fixtures/deep/**/*.js', {reducer});
const expected = [
'a1',
'a2',
'_b.b1',
'b.b2',
'b1',
'b2'
];
assert.same(deep, expected);
});
test('should use custom keygen', async assert => {
function keygen(file) {
return file.shortPath;
}
const deep = await requireGlob('./fixtures/deep/**/*.js', {keygen});
const expected = {
'a/a1.js': 'a1',
'a/a2.js': 'a2',
'b/b_b-b/_b.b1.js': '_b.b1',
'b/b_b-b/b.b2.js': 'b.b2',
'b/b1.js': 'b1',
'b/b2.js': 'b2'
};
assert.same(deep, expected);
});