forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWatchDetection.test.js
More file actions
155 lines (131 loc) · 3.05 KB
/
WatchDetection.test.js
File metadata and controls
155 lines (131 loc) · 3.05 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
154
155
"use strict";
/*globals describe it */
const path = require("path");
const fs = require("fs");
const MemoryFs = require("memory-fs");
const webpack = require("../");
describe("WatchDetection", () => {
if (process.env.NO_WATCH_TESTS) {
it.skip("long running tests excluded", () => {});
return;
}
jest.setTimeout(10000);
for (let changeTimeout = 10; changeTimeout < 100; changeTimeout += 10) {
createTestCase(changeTimeout);
}
for (let changeTimeout = 200; changeTimeout <= 2000; changeTimeout += 200) {
createTestCase(changeTimeout);
}
function createTestCase(changeTimeout) {
describe(`time between changes ${changeTimeout}ms`, () => {
const fixturePath = path.join(
__dirname,
"fixtures",
"temp-" + changeTimeout
);
const filePath = path.join(fixturePath, "file.js");
const file2Path = path.join(fixturePath, "file2.js");
const loaderPath = path.join(__dirname, "fixtures", "delay-loader.js");
beforeAll(() => {
try {
fs.mkdirSync(fixturePath);
} catch (e) {
// empty
}
fs.writeFileSync(filePath, "require('./file2')", "utf-8");
fs.writeFileSync(file2Path, "original", "utf-8");
});
afterAll(done => {
setTimeout(() => {
try {
fs.unlinkSync(filePath);
} catch (e) {
// empty
}
try {
fs.unlinkSync(file2Path);
} catch (e) {
// empty
}
try {
fs.rmdirSync(fixturePath);
} catch (e) {
// empty
}
done();
}, 100); // cool down a bit
});
it("should build the bundle correctly", done => {
const compiler = webpack({
mode: "development",
entry: loaderPath + "!" + filePath,
output: {
path: "/",
filename: "bundle.js"
}
});
const memfs = (compiler.outputFileSystem = new MemoryFs());
let onChange;
compiler.hooks.done.tap("WatchDetectionTest", () => {
if (onChange) onChange();
});
let watcher;
step1();
function step1() {
onChange = () => {
if (
memfs.readFileSync("/bundle.js") &&
memfs
.readFileSync("/bundle.js")
.toString()
.indexOf("original") >= 0
)
step2();
};
watcher = compiler.watch(
{
aggregateTimeout: 50
},
() => {}
);
}
function step2() {
onChange = null;
fs.writeFile(
filePath,
"require('./file2'); again",
"utf-8",
handleError
);
setTimeout(step3, changeTimeout);
}
function step3() {
onChange = null;
fs.writeFile(file2Path, "wrong", "utf-8", handleError);
setTimeout(step4, changeTimeout);
}
function step4() {
onChange = () => {
if (
memfs
.readFileSync("/bundle.js")
.toString()
.indexOf("correct") >= 0
)
step5();
};
fs.writeFile(file2Path, "correct", "utf-8", handleError);
}
function step5() {
onChange = null;
watcher.close(() => {
setTimeout(done, 500);
});
}
function handleError(err) {
if (err) done(err);
}
});
});
}
});