Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: polling usage in watchFiles option #3366

Merged
merged 4 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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,
}
`;
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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);
});
});
});