Skip to content

Commit ac61c7c

Browse files
committed
fix: polling tests
1 parent a83228b commit ac61c7c

File tree

4 files changed

+271
-47
lines changed

4 files changed

+271
-47
lines changed

lib/Server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,10 @@ class Server {
973973
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
974974
// this isn't an elegant solution, but we'll improve it in the future
975975
// eslint-disable-next-line no-undefined
976-
const usePolling = watchOptions.usePolling || Boolean(watchOptions.poll);
976+
const usePolling =
977+
typeof watchOptions.usePolling !== 'undefined'
978+
? watchOptions.usePolling
979+
: Boolean(watchOptions.poll);
977980
const interval =
978981
// eslint-disable-next-line no-nested-ternary
979982
typeof watchOptions.interval !== 'undefined'
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`'watchFiles' option should work with options {"poll":200} should pass correct options to chokidar config 1`] = `
4+
Array [
5+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
6+
Object {
7+
"alwaysStat": true,
8+
"atomic": false,
9+
"followSymlinks": false,
10+
"ignoreInitial": true,
11+
"ignorePermissionErrors": true,
12+
"ignored": undefined,
13+
"interval": 200,
14+
"persistent": true,
15+
"usePolling": true,
16+
},
17+
]
18+
`;
19+
20+
exports[`'watchFiles' option should work with options {"poll":true} should pass correct options to chokidar config 1`] = `
21+
Array [
22+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
23+
Object {
24+
"alwaysStat": true,
25+
"atomic": false,
26+
"followSymlinks": false,
27+
"ignoreInitial": true,
28+
"ignorePermissionErrors": true,
29+
"ignored": undefined,
30+
"interval": undefined,
31+
"persistent": true,
32+
"usePolling": true,
33+
},
34+
]
35+
`;
36+
37+
exports[`'watchFiles' option should work with options {"usePolling":false,"poll":true} should pass correct options to chokidar config 1`] = `
38+
Array [
39+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
40+
Object {
41+
"alwaysStat": true,
42+
"atomic": false,
43+
"followSymlinks": false,
44+
"ignoreInitial": true,
45+
"ignorePermissionErrors": true,
46+
"ignored": undefined,
47+
"interval": undefined,
48+
"persistent": true,
49+
"usePolling": false,
50+
},
51+
]
52+
`;
53+
54+
exports[`'watchFiles' option should work with options {"usePolling":false} should pass correct options to chokidar config 1`] = `
55+
Array [
56+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
57+
Object {
58+
"alwaysStat": true,
59+
"atomic": false,
60+
"followSymlinks": false,
61+
"ignoreInitial": true,
62+
"ignorePermissionErrors": true,
63+
"ignored": undefined,
64+
"interval": undefined,
65+
"persistent": true,
66+
"usePolling": false,
67+
},
68+
]
69+
`;
70+
71+
exports[`'watchFiles' option should work with options {"usePolling":true,"interval":200,"poll":400} should pass correct options to chokidar config 1`] = `
72+
Array [
73+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
74+
Object {
75+
"alwaysStat": true,
76+
"atomic": false,
77+
"followSymlinks": false,
78+
"ignoreInitial": true,
79+
"ignorePermissionErrors": true,
80+
"ignored": undefined,
81+
"interval": 200,
82+
"persistent": true,
83+
"usePolling": true,
84+
},
85+
]
86+
`;
87+
88+
exports[`'watchFiles' option should work with options {"usePolling":true} should pass correct options to chokidar config 1`] = `
89+
Array [
90+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
91+
Object {
92+
"alwaysStat": true,
93+
"atomic": false,
94+
"followSymlinks": false,
95+
"ignoreInitial": true,
96+
"ignorePermissionErrors": true,
97+
"ignored": undefined,
98+
"interval": undefined,
99+
"persistent": true,
100+
"usePolling": true,
101+
},
102+
]
103+
`;
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`'watchFiles' option should work with options {"poll":200} should pass correct options to chokidar config 1`] = `
4+
Array [
5+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
6+
Object {
7+
"alwaysStat": true,
8+
"atomic": false,
9+
"followSymlinks": false,
10+
"ignoreInitial": true,
11+
"ignorePermissionErrors": true,
12+
"ignored": undefined,
13+
"interval": 200,
14+
"persistent": true,
15+
"usePolling": true,
16+
},
17+
]
18+
`;
19+
20+
exports[`'watchFiles' option should work with options {"poll":true} should pass correct options to chokidar config 1`] = `
21+
Array [
22+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
23+
Object {
24+
"alwaysStat": true,
25+
"atomic": false,
26+
"followSymlinks": false,
27+
"ignoreInitial": true,
28+
"ignorePermissionErrors": true,
29+
"ignored": undefined,
30+
"interval": undefined,
31+
"persistent": true,
32+
"usePolling": true,
33+
},
34+
]
35+
`;
36+
37+
exports[`'watchFiles' option should work with options {"usePolling":false,"poll":true} should pass correct options to chokidar config 1`] = `
38+
Array [
39+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
40+
Object {
41+
"alwaysStat": true,
42+
"atomic": false,
43+
"followSymlinks": false,
44+
"ignoreInitial": true,
45+
"ignorePermissionErrors": true,
46+
"ignored": undefined,
47+
"interval": undefined,
48+
"persistent": true,
49+
"usePolling": false,
50+
},
51+
]
52+
`;
53+
54+
exports[`'watchFiles' option should work with options {"usePolling":false} should pass correct options to chokidar config 1`] = `
55+
Array [
56+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
57+
Object {
58+
"alwaysStat": true,
59+
"atomic": false,
60+
"followSymlinks": false,
61+
"ignoreInitial": true,
62+
"ignorePermissionErrors": true,
63+
"ignored": undefined,
64+
"interval": undefined,
65+
"persistent": true,
66+
"usePolling": false,
67+
},
68+
]
69+
`;
70+
71+
exports[`'watchFiles' option should work with options {"usePolling":true,"interval":200,"poll":400} should pass correct options to chokidar config 1`] = `
72+
Array [
73+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
74+
Object {
75+
"alwaysStat": true,
76+
"atomic": false,
77+
"followSymlinks": false,
78+
"ignoreInitial": true,
79+
"ignorePermissionErrors": true,
80+
"ignored": undefined,
81+
"interval": 200,
82+
"persistent": true,
83+
"usePolling": true,
84+
},
85+
]
86+
`;
87+
88+
exports[`'watchFiles' option should work with options {"usePolling":true} should pass correct options to chokidar config 1`] = `
89+
Array [
90+
"/Users/anshumanv/Code/GitHub/webpack-dev-server/test/fixtures/contentbase-config/public/assets/example.txt",
91+
Object {
92+
"alwaysStat": true,
93+
"atomic": false,
94+
"followSymlinks": false,
95+
"ignoreInitial": true,
96+
"ignorePermissionErrors": true,
97+
"ignored": undefined,
98+
"interval": undefined,
99+
"persistent": true,
100+
"usePolling": true,
101+
},
102+
]
103+
`;

test/server/watchFiles-option.test.js

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -280,54 +280,69 @@ describe("'watchFiles' option", () => {
280280

281281
const chokidarMock = jest.spyOn(chokidar, 'watch');
282282

283-
beforeAll((done) => {
284-
server = testServer.start(
285-
config,
286-
{
287-
watchFiles: {
288-
paths: file,
289-
options: {
290-
usePolling: true,
291-
interval: 400,
292-
},
293-
},
294-
port,
295-
},
296-
done
297-
);
298-
});
299-
300-
afterAll((done) => {
301-
testServer.close(done);
302-
fs.truncateSync(file);
303-
});
304-
305-
it('should pass correct options to chokidar config', () => {
306-
expect(chokidarMock).toHaveBeenCalledWith(file, {
307-
ignoreInitial: true,
308-
persistent: true,
309-
followSymlinks: false,
310-
atomic: false,
311-
alwaysStat: true,
312-
ignorePermissionErrors: true,
313-
// eslint-disable-next-line no-undefined
314-
ignored: undefined,
283+
const optionCases = [
284+
{
285+
poll: true,
286+
},
287+
{
288+
poll: 200,
289+
},
290+
{
315291
usePolling: true,
316-
interval: 400,
317-
});
318-
});
319-
320-
it('should reload on file content changed', (done) => {
321-
server.staticWatchers[0].on('change', (changedPath) => {
322-
expect(changedPath).toBe(file);
323-
324-
done();
292+
},
293+
{
294+
usePolling: false,
295+
},
296+
{
297+
usePolling: false,
298+
poll: true,
299+
},
300+
{
301+
usePolling: true,
302+
interval: 200,
303+
poll: 400,
304+
},
305+
];
306+
307+
optionCases.forEach((optionCase) => {
308+
describe(JSON.stringify(optionCase), () => {
309+
beforeAll((done) => {
310+
chokidarMock.mockClear();
311+
server = testServer.start(
312+
config,
313+
{
314+
watchFiles: {
315+
paths: file,
316+
options: optionCase,
317+
},
318+
port,
319+
},
320+
done
321+
);
322+
});
323+
324+
afterAll((done) => {
325+
testServer.close(done);
326+
fs.truncateSync(file);
327+
});
328+
329+
it('should pass correct options to chokidar config', () => {
330+
expect(chokidarMock.mock.calls[0]).toMatchSnapshot();
331+
});
332+
333+
it('should reload on file content changed', (done) => {
334+
server.staticWatchers[0].on('change', (changedPath) => {
335+
expect(changedPath).toBe(file);
336+
337+
done();
338+
});
339+
340+
// change file content
341+
setTimeout(() => {
342+
fs.writeFileSync(file, 'Kurosaki Ichigo', 'utf8');
343+
}, 1000);
344+
});
325345
});
326-
327-
// change file content
328-
setTimeout(() => {
329-
fs.writeFileSync(file, 'Kurosaki Ichigo', 'utf8');
330-
}, 1000);
331346
});
332347
});
333348
});

0 commit comments

Comments
 (0)