From 7abcde17e409eb00280328936b126fcf6ebb7ada Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 11 Dec 2021 20:10:51 +0530 Subject: [PATCH] test: add e2e tests for `watchFiles` option (#4081) --- .../watch-files.test.js.snap.webpack4 | 311 ++++++++ .../watch-files.test.js.snap.webpack5 | 311 ++++++++ test/e2e/watch-files.test.js | 679 ++++++++++++++++++ .../watchFiles-option.test.js.snap.webpack4 | 141 ---- .../watchFiles-option.test.js.snap.webpack5 | 141 ---- test/server/watchFiles-option.test.js | 407 ----------- 6 files changed, 1301 insertions(+), 689 deletions(-) create mode 100644 test/e2e/__snapshots__/watch-files.test.js.snap.webpack4 create mode 100644 test/e2e/__snapshots__/watch-files.test.js.snap.webpack5 create mode 100644 test/e2e/watch-files.test.js delete mode 100644 test/server/__snapshots__/watchFiles-option.test.js.snap.webpack4 delete mode 100644 test/server/__snapshots__/watchFiles-option.test.js.snap.webpack5 delete mode 100644 test/server/watchFiles-option.test.js diff --git a/test/e2e/__snapshots__/watch-files.test.js.snap.webpack4 b/test/e2e/__snapshots__/watch-files.test.js.snap.webpack4 new file mode 100644 index 0000000000..8936bb5991 --- /dev/null +++ b/test/e2e/__snapshots__/watch-files.test.js.snap.webpack4 @@ -0,0 +1,311 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`watchFiles option should not crash if file doesn't exist should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should not crash if file doesn't exist should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should not crash if file doesn't exist should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with array config should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with array config should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with array config should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with object with multiple paths should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with object with multiple paths should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with object with multiple paths should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with object with single path should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with object with single path should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with object with single path should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"interval":400,"poll":200} should reload when file content is changed 1`] = ` +Object { + "alwaysStat": true, + "atomic": false, + "followSymlinks": false, + "ignoreInitial": true, + "ignorePermissionErrors": true, + "ignored": undefined, + "interval": 400, + "persistent": true, + "usePolling": true, +} +`; + +exports[`watchFiles option should work with options {"interval":400,"poll":200} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"interval":400,"poll":200} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"interval":400,"poll":200} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"poll":200} should reload when file content is changed 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":200} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"poll":200} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"poll":200} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"poll":true} should reload when file content is changed 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 {"poll":true} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"poll":true} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"poll":true} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":false,"interval":200,"poll":400} should reload when file content is changed 1`] = ` +Object { + "alwaysStat": true, + "atomic": false, + "followSymlinks": false, + "ignoreInitial": true, + "ignorePermissionErrors": true, + "ignored": undefined, + "interval": 200, + "persistent": true, + "usePolling": false, +} +`; + +exports[`watchFiles option should work with options {"usePolling":false,"interval":200,"poll":400} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":false,"interval":200,"poll":400} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":false,"interval":200,"poll":400} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":200} should reload when file content is changed 1`] = ` +Object { + "alwaysStat": true, + "atomic": false, + "followSymlinks": false, + "ignoreInitial": true, + "ignorePermissionErrors": true, + "ignored": undefined, + "interval": 200, + "persistent": true, + "usePolling": false, +} +`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":200} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":200} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":200} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":true} should reload when file content is changed 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,"poll":true} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":true} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":true} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":false} should reload when file content is changed 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 reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":false} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":false} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":true,"interval":200,"poll":400} should reload when file content is changed 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,"interval":200,"poll":400} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":true,"interval":200,"poll":400} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":true,"interval":200,"poll":400} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":true,"poll":200} should reload when file content is changed 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,"poll":200} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":true,"poll":200} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":true,"poll":200} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":true} should reload when file content is changed 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":true} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":true} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":true} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with string and glob should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with string and glob should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with string and glob should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with string and path to directory should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with string and path to directory should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with string and path to directory should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with string and path to file should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with string and path to file should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with string and path to file should reload when file content is changed: response status 1`] = `200`; diff --git a/test/e2e/__snapshots__/watch-files.test.js.snap.webpack5 b/test/e2e/__snapshots__/watch-files.test.js.snap.webpack5 new file mode 100644 index 0000000000..8936bb5991 --- /dev/null +++ b/test/e2e/__snapshots__/watch-files.test.js.snap.webpack5 @@ -0,0 +1,311 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`watchFiles option should not crash if file doesn't exist should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should not crash if file doesn't exist should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should not crash if file doesn't exist should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with array config should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with array config should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with array config should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with object with multiple paths should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with object with multiple paths should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with object with multiple paths should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with object with single path should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with object with single path should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with object with single path should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"interval":400,"poll":200} should reload when file content is changed 1`] = ` +Object { + "alwaysStat": true, + "atomic": false, + "followSymlinks": false, + "ignoreInitial": true, + "ignorePermissionErrors": true, + "ignored": undefined, + "interval": 400, + "persistent": true, + "usePolling": true, +} +`; + +exports[`watchFiles option should work with options {"interval":400,"poll":200} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"interval":400,"poll":200} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"interval":400,"poll":200} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"poll":200} should reload when file content is changed 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":200} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"poll":200} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"poll":200} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"poll":true} should reload when file content is changed 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 {"poll":true} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"poll":true} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"poll":true} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":false,"interval":200,"poll":400} should reload when file content is changed 1`] = ` +Object { + "alwaysStat": true, + "atomic": false, + "followSymlinks": false, + "ignoreInitial": true, + "ignorePermissionErrors": true, + "ignored": undefined, + "interval": 200, + "persistent": true, + "usePolling": false, +} +`; + +exports[`watchFiles option should work with options {"usePolling":false,"interval":200,"poll":400} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":false,"interval":200,"poll":400} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":false,"interval":200,"poll":400} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":200} should reload when file content is changed 1`] = ` +Object { + "alwaysStat": true, + "atomic": false, + "followSymlinks": false, + "ignoreInitial": true, + "ignorePermissionErrors": true, + "ignored": undefined, + "interval": 200, + "persistent": true, + "usePolling": false, +} +`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":200} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":200} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":200} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":true} should reload when file content is changed 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,"poll":true} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":true} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":false,"poll":true} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":false} should reload when file content is changed 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 reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":false} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":false} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":true,"interval":200,"poll":400} should reload when file content is changed 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,"interval":200,"poll":400} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":true,"interval":200,"poll":400} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":true,"interval":200,"poll":400} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":true,"poll":200} should reload when file content is changed 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,"poll":200} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":true,"poll":200} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":true,"poll":200} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with options {"usePolling":true} should reload when file content is changed 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":true} should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with options {"usePolling":true} should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with options {"usePolling":true} should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with string and glob should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with string and glob should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with string and glob should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with string and path to directory should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with string and path to directory should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with string and path to directory should reload when file content is changed: response status 1`] = `200`; + +exports[`watchFiles option should work with string and path to file should reload when file content is changed: console messages 1`] = ` +Array [ + "Hey.", +] +`; + +exports[`watchFiles option should work with string and path to file should reload when file content is changed: page errors 1`] = `Array []`; + +exports[`watchFiles option should work with string and path to file should reload when file content is changed: response status 1`] = `200`; diff --git a/test/e2e/watch-files.test.js b/test/e2e/watch-files.test.js new file mode 100644 index 0000000000..8031a3a963 --- /dev/null +++ b/test/e2e/watch-files.test.js @@ -0,0 +1,679 @@ +"use strict"; + +const path = require("path"); +const chokidar = require("chokidar"); +const fs = require("graceful-fs"); +const webpack = require("webpack"); +const Server = require("../../lib/Server"); +const config = require("../fixtures/static-config/webpack.config"); +const runBrowser = require("../helpers/run-browser"); +const port = require("../ports-map")["watch-files-option"]; + +const watchDir = path.resolve(__dirname, "../fixtures/static-config/public"); + +describe("watchFiles option", () => { + describe("should work with string and path to file", () => { + const file = path.join(watchDir, "assets/example.txt"); + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + compiler = webpack(config); + + server = new Server( + { + watchFiles: file, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + fs.truncateSync(file); + }); + + it("should reload when file content is changed", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + + // change file content + fs.writeFileSync(file, "Kurosaki Ichigo", "utf8"); + + await new Promise((resolve) => { + server.staticWatchers[0].on("change", async (changedPath) => { + // page reload + await page.waitForNavigation({ waitUntil: "networkidle0" }); + + expect(changedPath).toBe(file); + + resolve(); + }); + }); + }); + }); + + describe("should work with string and path to directory", () => { + const file = path.join(watchDir, "assets/example.txt"); + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + compiler = webpack(config); + + server = new Server( + { + watchFiles: watchDir, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + fs.truncateSync(file); + }); + + it("should reload when file content is changed", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + + // change file content + fs.writeFileSync(file, "Kurosaki Ichigo", "utf8"); + + await new Promise((resolve) => { + server.staticWatchers[0].on("change", async (changedPath) => { + // page reload + await page.waitForNavigation({ waitUntil: "networkidle0" }); + + expect(changedPath).toBe(file); + + resolve(); + }); + }); + }); + }); + + describe("should work with string and glob", () => { + const file = path.join(watchDir, "assets/example.txt"); + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + compiler = webpack(config); + + server = new Server( + { + watchFiles: `${watchDir}/**/*`, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + fs.truncateSync(file); + }); + + it("should reload when file content is changed", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + + // change file content + fs.writeFileSync(file, "Kurosaki Ichigo", "utf8"); + + await new Promise((resolve) => { + server.staticWatchers[0].on("change", async (changedPath) => { + // page reload + await page.waitForNavigation({ waitUntil: "networkidle0" }); + + expect(changedPath).toBe(file); + + resolve(); + }); + }); + }); + }); + + describe("should not crash if file doesn't exist", () => { + const nonExistFile = path.join(watchDir, "assets/non-exist.txt"); + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + try { + fs.unlinkSync(nonExistFile); + } catch (error) { + // ignore + } + + compiler = webpack(config); + + server = new Server( + { + watchFiles: nonExistFile, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + }); + + it("should reload when file content is changed", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + + await new Promise((resolve) => { + server.staticWatchers[0].on("change", async (changedPath) => { + // page reload + await page.waitForNavigation({ waitUntil: "networkidle0" }); + + expect(changedPath).toBe(nonExistFile); + resolve(); + }); + + // create file content + setTimeout(() => { + fs.writeFileSync(nonExistFile, "Kurosaki Ichigo", "utf8"); + // change file content + setTimeout(() => { + fs.writeFileSync(nonExistFile, "Kurosaki Ichigo", "utf8"); + }, 1000); + }, 1000); + }); + }); + }); + + describe("should work with object with single path", () => { + const file = path.join(watchDir, "assets/example.txt"); + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + compiler = webpack(config); + + server = new Server( + { + watchFiles: { paths: file }, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + fs.truncateSync(file); + }); + + it("should reload when file content is changed", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + + // change file content + fs.writeFileSync(file, "Kurosaki Ichigo", "utf8"); + + await new Promise((resolve) => { + server.staticWatchers[0].on("change", async (changedPath) => { + // page reload + await page.waitForNavigation({ waitUntil: "networkidle0" }); + + expect(changedPath).toBe(file); + + resolve(); + }); + }); + }); + }); + + describe("should work with object with multiple paths", () => { + const file = path.join(watchDir, "assets/example.txt"); + const other = path.join(watchDir, "assets/other.txt"); + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + compiler = webpack(config); + + server = new Server( + { + watchFiles: { paths: [file, other] }, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + fs.truncateSync(file); + fs.truncateSync(other); + }); + + it("should reload when file content is changed", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + + // change file content + fs.writeFileSync(file, "foo", "utf8"); + fs.writeFileSync(other, "bar", "utf8"); + + await new Promise((resolve) => { + const expected = [file, other]; + let changed = 0; + + server.staticWatchers[0].on("change", async (changedPath) => { + // page reload + await page.waitForNavigation({ waitUntil: "networkidle0" }); + + expect(expected.includes(changedPath)).toBeTruthy(); + + changed += 1; + + if (changed === 2) { + resolve(); + } + }); + }); + }); + }); + + describe("should work with array config", () => { + const file = path.join(watchDir, "assets/example.txt"); + const other = path.join(watchDir, "assets/other.txt"); + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + compiler = webpack(config); + + server = new Server( + { + watchFiles: [{ paths: [file] }, other], + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + fs.truncateSync(file); + fs.truncateSync(other); + }); + + it("should reload when file content is changed", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + + // change file content + fs.writeFileSync(file, "foo", "utf8"); + fs.writeFileSync(other, "bar", "utf8"); + + await new Promise((resolve) => { + let changed = 0; + + server.staticWatchers[0].on("change", async (changedPath) => { + // page reload + await page.waitForNavigation({ waitUntil: "networkidle0" }); + + expect(changedPath).toBe(file); + + changed += 1; + + if (changed === 2) { + resolve(); + } + }); + server.staticWatchers[1].on("change", async (changedPath) => { + // page reload + await page.waitForNavigation({ waitUntil: "networkidle0" }); + + expect(changedPath).toBe(other); + + changed += 1; + + if (changed === 2) { + resolve(); + } + }); + }); + }); + }); + + describe("should work with options", () => { + const file = path.join(watchDir, "assets/example.txt"); + + const chokidarMock = jest.spyOn(chokidar, "watch"); + + const optionCases = [ + { + poll: true, + }, + { + poll: 200, + }, + { + usePolling: true, + }, + { + usePolling: true, + poll: 200, + }, + { + usePolling: false, + }, + { + usePolling: false, + poll: 200, + }, + { + usePolling: false, + poll: true, + }, + { + interval: 400, + poll: 200, + }, + { + usePolling: true, + interval: 200, + poll: 400, + }, + { + usePolling: false, + interval: 200, + poll: 400, + }, + ]; + + optionCases.forEach((optionCase) => { + describe(JSON.stringify(optionCase), () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + chokidarMock.mockClear(); + + compiler = webpack(config); + + server = new Server( + { + watchFiles: { + paths: file, + options: optionCase, + }, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await server.stop(); + await browser.close(); + fs.truncateSync(file); + }); + + it("should reload when file content is changed", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + // should pass correct options to chokidar config + expect(chokidarMock.mock.calls[0][1]).toMatchSnapshot(); + + expect(response.status()).toMatchSnapshot("response status"); + + expect( + consoleMessages.map((message) => message.text()) + ).toMatchSnapshot("console messages"); + + expect(pageErrors).toMatchSnapshot("page errors"); + + // change file content + fs.writeFileSync(file, "Kurosaki Ichigo", "utf8"); + + await new Promise((resolve) => { + server.staticWatchers[0].on("change", async (changedPath) => { + // page reload + await page.waitForNavigation({ waitUntil: "networkidle0" }); + + expect(changedPath).toBe(file); + + resolve(); + }); + }); + }); + }); + }); + }); +}); diff --git a/test/server/__snapshots__/watchFiles-option.test.js.snap.webpack4 b/test/server/__snapshots__/watchFiles-option.test.js.snap.webpack4 deleted file mode 100644 index 785a259628..0000000000 --- a/test/server/__snapshots__/watchFiles-option.test.js.snap.webpack4 +++ /dev/null @@ -1,141 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`'watchFiles' option should work with options {"interval":400,"poll":200} should pass correct options to chokidar config 1`] = ` -Object { - "alwaysStat": true, - "atomic": false, - "followSymlinks": false, - "ignoreInitial": true, - "ignorePermissionErrors": true, - "ignored": undefined, - "interval": 400, - "persistent": true, - "usePolling": true, -} -`; - -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,"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": false, -} -`; - -exports[`'watchFiles' option should work with options {"usePolling":false,"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": false, -} -`; - -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,"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 {"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, -} -`; diff --git a/test/server/__snapshots__/watchFiles-option.test.js.snap.webpack5 b/test/server/__snapshots__/watchFiles-option.test.js.snap.webpack5 deleted file mode 100644 index 785a259628..0000000000 --- a/test/server/__snapshots__/watchFiles-option.test.js.snap.webpack5 +++ /dev/null @@ -1,141 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`'watchFiles' option should work with options {"interval":400,"poll":200} should pass correct options to chokidar config 1`] = ` -Object { - "alwaysStat": true, - "atomic": false, - "followSymlinks": false, - "ignoreInitial": true, - "ignorePermissionErrors": true, - "ignored": undefined, - "interval": 400, - "persistent": true, - "usePolling": true, -} -`; - -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,"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": false, -} -`; - -exports[`'watchFiles' option should work with options {"usePolling":false,"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": false, -} -`; - -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,"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 {"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, -} -`; diff --git a/test/server/watchFiles-option.test.js b/test/server/watchFiles-option.test.js deleted file mode 100644 index 7a16f89f45..0000000000 --- a/test/server/watchFiles-option.test.js +++ /dev/null @@ -1,407 +0,0 @@ -"use strict"; - -const path = require("path"); -const webpack = require("webpack"); -const fs = require("graceful-fs"); -const chokidar = require("chokidar"); -const Server = require("../../lib/Server"); -const config = require("../fixtures/static-config/webpack.config"); -const port = require("../ports-map")["watch-files-option"]; - -const watchDir = path.resolve(__dirname, "../fixtures/static-config/public"); - -describe("'watchFiles' option", () => { - let server; - - describe("should work with string and path to file", () => { - const file = path.join(watchDir, "assets/example.txt"); - - beforeAll(async () => { - const compiler = webpack(config); - - server = new Server( - { - watchFiles: file, - port, - }, - compiler - ); - - await server.start(); - }); - - afterAll(async () => { - await server.stop(); - - fs.truncateSync(file); - }); - - 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); - }); - }); - - describe("should work with string and path to dir", () => { - const file = path.join(watchDir, "assets/example.txt"); - - beforeAll(async () => { - const compiler = webpack(config); - - server = new Server( - { - watchFiles: watchDir, - port, - }, - compiler - ); - - await server.start(); - }); - - afterAll(async () => { - await server.stop(); - - fs.truncateSync(file); - }); - - 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); - }); - }); - - describe("should work with string and glob", () => { - const file = path.join(watchDir, "assets/example.txt"); - - beforeAll(async () => { - const compiler = webpack(config); - - server = new Server( - { - watchFiles: `${watchDir}/**/*`, - port, - }, - compiler - ); - - await server.start(); - }); - - afterAll(async () => { - await server.stop(); - - fs.truncateSync(file); - }); - - 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); - }); - }); - - describe("should work not crash on non exist file", () => { - const nonExistFile = path.join(watchDir, "assets/non-exist.txt"); - - beforeAll(async () => { - try { - fs.unlinkSync(nonExistFile); - } catch (error) { - // ignore - } - - const compiler = webpack(config); - - server = new Server( - { - watchFiles: nonExistFile, - port, - }, - compiler - ); - - await server.start(); - }); - - afterAll(async () => { - await server.stop(); - - fs.truncateSync(nonExistFile); - }); - - it("should reload on file content changed", (done) => { - server.staticWatchers[0].once("change", (changedPath) => { - expect(changedPath).toBe(nonExistFile); - - done(); - }); - - // create file content - setTimeout(() => { - fs.writeFileSync(nonExistFile, "Kurosaki Ichigo", "utf8"); - - // change file content - setTimeout(() => { - fs.writeFileSync(nonExistFile, "Kurosaki Ichigo", "utf8"); - }, 1000); - }, 1000); - }); - }); - - describe("should work with object with single path", () => { - const file = path.join(watchDir, "assets/example.txt"); - - beforeAll(async () => { - const compiler = webpack(config); - - server = new Server( - { - watchFiles: { paths: file }, - port, - }, - compiler - ); - - await server.start(); - }); - - afterAll(async () => { - await server.stop(); - - fs.truncateSync(file); - }); - - it("should reload on file content channge", (done) => { - server.staticWatchers[0].on("change", (changedPath) => { - expect(changedPath).toBe(file); - - done(); - }); - - // change file content - setTimeout(() => { - fs.writeFileSync(file, "Kurosaki Ichigo", "utf8"); - }, 1000); - }); - }); - - describe("should work with object with multiple paths", () => { - const file = path.join(watchDir, "assets/example.txt"); - const other = path.join(watchDir, "assets/other.txt"); - - beforeAll(async () => { - const compiler = webpack(config); - - server = new Server( - { - watchFiles: { paths: [file, other] }, - port, - }, - compiler - ); - - await server.start(); - }); - - afterAll(async () => { - await server.stop(); - - fs.truncateSync(file); - }); - - it("should reload on file content channge", (done) => { - const expected = [file, other]; - - let changed = 0; - - server.staticWatchers[0].on("change", (changedPath) => { - expect(expected.includes(changedPath)).toBeTruthy(); - - changed += 1; - - if (changed === 2) { - done(); - } - }); - - // change file content - setTimeout(() => { - fs.writeFileSync(file, "Kurosaki Ichigo", "utf8"); - fs.writeFileSync(other, "Kurosaki Ichigo", "utf8"); - }, 1000); - }); - }); - - describe("should work with array config", () => { - const file = path.join(watchDir, "assets/example.txt"); - const other = path.join(watchDir, "assets/other.txt"); - - beforeAll(async () => { - const compiler = webpack(config); - - server = new Server( - { - watchFiles: [{ paths: [file] }, other], - port, - }, - compiler - ); - - await server.start(); - }); - - afterAll(async () => { - await server.stop(); - - fs.truncateSync(file); - fs.truncateSync(other); - }); - - it("should reload on file content change", (done) => { - let changed = 0; - - server.staticWatchers[0].on("change", (changedPath) => { - expect(changedPath).toBe(file); - - changed += 1; - - if (changed === 2) { - done(); - } - }); - - server.staticWatchers[1].on("change", (changedPath) => { - expect(changedPath).toBe(other); - - changed += 1; - - if (changed === 2) { - done(); - } - }); - - // change file content - setTimeout(() => { - fs.writeFileSync(file, "Kurosaki Ichigo", "utf8"); - fs.writeFileSync(other, "Kurosaki Ichigo", "utf8"); - }, 1000); - }); - }); - - describe("should work with options", () => { - const file = path.join(watchDir, "assets/example.txt"); - - const chokidarMock = jest.spyOn(chokidar, "watch"); - - const optionCases = [ - { - poll: true, - }, - { - poll: 200, - }, - { - usePolling: true, - }, - { - usePolling: true, - poll: 200, - }, - { - usePolling: false, - }, - { - usePolling: false, - poll: 200, - }, - { - usePolling: false, - poll: true, - }, - { - interval: 400, - poll: 200, - }, - { - usePolling: true, - interval: 200, - poll: 400, - }, - { - usePolling: false, - interval: 200, - poll: 400, - }, - ]; - - optionCases.forEach((optionCase) => { - describe(JSON.stringify(optionCase), () => { - beforeAll(async () => { - chokidarMock.mockClear(); - - const compiler = webpack(config); - - server = new Server( - { - watchFiles: { - paths: file, - options: optionCase, - }, - port, - }, - compiler - ); - - await server.start(); - }); - - afterAll(async () => { - await server.stop(); - - 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); - }); - }); - }); - }); -});