forked from sindresorhus/clear-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
99 lines (85 loc) · 2.16 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
import test from 'ava';
import clearModule from '.';
test('clearModule()', t => {
const id = './fixture';
t.is(require(id)(), 1);
t.is(require(id)(), 2);
clearModule(id);
t.is(require(id)(), 1);
});
test('clearModule.all()', t => {
t.true(Object.keys(require.cache).length > 0);
clearModule.all();
t.is(Object.keys(require.cache).length, 0);
});
test('clearModule.match()', t => {
const id = './fixture';
const match = './fixture-match';
t.is(require(id)(), 1);
t.is(require(match)(), 1);
clearModule.match(/match/);
t.is(require(id)(), 2);
t.is(require(match)(), 1);
});
test('clearModule() recursively', t => {
clearModule.all();
const id = './fixture-with-dependency';
t.is(require(id)(), 1);
t.is(require(id)(), 2);
delete require.cache[require.resolve(id)];
t.is(require(id)(), 3);
clearModule(id);
t.is(require(id)(), 1);
});
test('clearModule.single()', t => {
clearModule.all();
const id = './fixture-with-dependency';
t.is(require(id)(), 1);
t.is(require(id)(), 2);
clearModule.single(id);
t.is(require(id)(), 3);
clearModule(id);
t.is(require(id)(), 1);
});
test('works with circular dependencies', t => {
const id1 = './fixture-circular-1';
require(id1);
let parentCalls = 0;
let childrenCalls = 0;
const {children, parents} = require.cache[require.resolve(id1)];
Object.defineProperty(
require.cache[require.resolve(id1)],
'children',
{
get: () => {
childrenCalls++;
return children;
}
}
);
Object.defineProperty(
require.cache[require.resolve(id1)],
'parent',
{
get: () => {
parentCalls++;
return parents;
}
}
);
clearModule(id1);
t.is(parentCalls, 1);
t.is(childrenCalls, 4);
clearModule.all();
});
test('clear with filter', t => {
const testModuleFunctions = t;
const id = './fixture-filter';
require(id);
const parentmodule = Object.keys(require.cache).find(value => value.includes('parent-module'));
testModuleFunctions.not(typeof require.cache[parentmodule] !== 'undefined', false);
clearModule(id, {
filter: absolutePathToCachedFile => !absolutePathToCachedFile.match(/parent-module/)
});
testModuleFunctions.not(typeof require.cache[parentmodule] !== 'undefined', false);
});