Skip to content

Commit

Permalink
fix: polling usage in watchFiles option (#3366)
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed May 31, 2021
1 parent 63815b7 commit 2afb223
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 32 deletions.
10 changes: 8 additions & 2 deletions lib/Server.js
Expand Up @@ -973,9 +973,15 @@ class Server {
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
// this isn't an elegant solution, but we'll improve it in the future
// eslint-disable-next-line no-undefined
const usePolling = watchOptions.poll ? true : undefined;
const usePolling =
typeof watchOptions.usePolling !== 'undefined'
? watchOptions.usePolling
: Boolean(watchOptions.poll);
const interval =
typeof watchOptions.poll === 'number'
// eslint-disable-next-line no-nested-ternary
typeof watchOptions.interval !== 'undefined'
? watchOptions.interval
: typeof watchOptions.poll === 'number'
? watchOptions.poll
: // eslint-disable-next-line no-undefined
undefined;
Expand Down
85 changes: 85 additions & 0 deletions test/server/__snapshots__/watchFiles-option.test.js.snap.webpack4
@@ -0,0 +1,85 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`'watchFiles' option should work with options {"poll":200} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": 200,
"persistent": true,
"usePolling": true,
}
`;

exports[`'watchFiles' option should work with options {"poll":true} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": undefined,
"persistent": true,
"usePolling": true,
}
`;

exports[`'watchFiles' option should work with options {"usePolling":false,"poll":true} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": undefined,
"persistent": true,
"usePolling": false,
}
`;

exports[`'watchFiles' option should work with options {"usePolling":false} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": undefined,
"persistent": true,
"usePolling": false,
}
`;

exports[`'watchFiles' option should work with options {"usePolling":true,"interval":200,"poll":400} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": 200,
"persistent": true,
"usePolling": true,
}
`;

exports[`'watchFiles' option should work with options {"usePolling":true} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": undefined,
"persistent": true,
"usePolling": true,
}
`;
85 changes: 85 additions & 0 deletions test/server/__snapshots__/watchFiles-option.test.js.snap.webpack5
@@ -0,0 +1,85 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`'watchFiles' option should work with options {"poll":200} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": 200,
"persistent": true,
"usePolling": true,
}
`;

exports[`'watchFiles' option should work with options {"poll":true} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": undefined,
"persistent": true,
"usePolling": true,
}
`;

exports[`'watchFiles' option should work with options {"usePolling":false,"poll":true} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": undefined,
"persistent": true,
"usePolling": false,
}
`;

exports[`'watchFiles' option should work with options {"usePolling":false} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": undefined,
"persistent": true,
"usePolling": false,
}
`;

exports[`'watchFiles' option should work with options {"usePolling":true,"interval":200,"poll":400} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": 200,
"persistent": true,
"usePolling": true,
}
`;

exports[`'watchFiles' option should work with options {"usePolling":true} should pass correct options to chokidar config 1`] = `
Object {
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
"ignoreInitial": true,
"ignorePermissionErrors": true,
"ignored": undefined,
"interval": undefined,
"persistent": true,
"usePolling": true,
}
`;
94 changes: 64 additions & 30 deletions test/server/watchFiles-option.test.js
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const fs = require('graceful-fs');
const chokidar = require('chokidar');
const testServer = require('../helpers/test-server');
const config = require('../fixtures/contentbase-config/webpack.config');
const port = require('../ports-map')['watchFiles-option'];
Expand Down Expand Up @@ -277,38 +278,71 @@ describe("'watchFiles' option", () => {
describe('should work with options', () => {
const file = path.join(watchDir, 'assets/example.txt');

beforeAll((done) => {
server = testServer.start(
config,
{
watchFiles: {
paths: file,
options: {
usePolling: true,
const chokidarMock = jest.spyOn(chokidar, 'watch');

const optionCases = [
{
poll: true,
},
{
poll: 200,
},
{
usePolling: true,
},
{
usePolling: false,
},
{
usePolling: false,
poll: true,
},
{
usePolling: true,
interval: 200,
poll: 400,
},
];

optionCases.forEach((optionCase) => {
describe(JSON.stringify(optionCase), () => {
beforeAll((done) => {
chokidarMock.mockClear();
server = testServer.start(
config,
{
watchFiles: {
paths: file,
options: optionCase,
},
port,
},
},
port,
},
done
);
});

afterAll((done) => {
testServer.close(done);
fs.truncateSync(file);
});

it('should reload on file content changed', (done) => {
server.staticWatchers[0].on('change', (changedPath) => {
expect(changedPath).toBe(file);

done();
done
);
});

afterAll((done) => {
testServer.close(done);
fs.truncateSync(file);
});

it('should pass correct options to chokidar config', () => {
expect(chokidarMock.mock.calls[0][1]).toMatchSnapshot();
});

it('should reload on file content changed', (done) => {
server.staticWatchers[0].on('change', (changedPath) => {
expect(changedPath).toBe(file);

done();
});

// change file content
setTimeout(() => {
fs.writeFileSync(file, 'Kurosaki Ichigo', 'utf8');
}, 1000);
});
});

// change file content
setTimeout(() => {
fs.writeFileSync(file, 'Kurosaki Ichigo', 'utf8');
}, 1000);
});
});
});

0 comments on commit 2afb223

Please sign in to comment.