Skip to content

Commit 77bc5ef

Browse files
committed
feat: add tests for watchFiles option with ignored glob array support
1 parent 13d0398 commit 77bc5ef

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

test/e2e/__snapshots__/watch-files.test.js.snap.webpack5

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ exports[`watchFiles option should work with directory and ignored option to filt
4242

4343
exports[`watchFiles option should work with directory and ignored option to filter files should reload when file content is changed: response status 1`] = `200`;
4444

45+
exports[`watchFiles option should work with ignored option using glob array should not reload when an ignored glob file is changed: response status 1`] = `200`;
46+
47+
exports[`watchFiles option should work with ignored option using glob array should reload when file content is changed: console messages 1`] = `
48+
[
49+
"Hey.",
50+
]
51+
`;
52+
53+
exports[`watchFiles option should work with ignored option using glob array should reload when file content is changed: page errors 1`] = `[]`;
54+
55+
exports[`watchFiles option should work with ignored option using glob array should reload when file content is changed: response status 1`] = `200`;
56+
4557
exports[`watchFiles option should work with ignored option using glob string should not reload when an ignored glob file is changed: response status 1`] = `200`;
4658

4759
exports[`watchFiles option should work with ignored option using glob string should reload when file content is changed: console messages 1`] = `

test/e2e/watch-files.test.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,119 @@ describe("watchFiles option", () => {
528528
});
529529
});
530530

531+
describe("should work with ignored option using glob array", () => {
532+
const file = path.join(watchDir, "assets/example.txt");
533+
const ignoredFile = path.join(watchDir, "assets/example.js");
534+
let compiler;
535+
let server;
536+
let page;
537+
let browser;
538+
let pageErrors;
539+
let consoleMessages;
540+
541+
beforeEach(async () => {
542+
compiler = webpack(config);
543+
544+
server = new Server(
545+
{
546+
watchFiles: {
547+
paths: watchDir,
548+
options: {
549+
ignored: [`${watchDir}/**/*.js`],
550+
},
551+
},
552+
port,
553+
},
554+
compiler,
555+
);
556+
557+
await server.start();
558+
559+
({ page, browser } = await runBrowser());
560+
561+
pageErrors = [];
562+
consoleMessages = [];
563+
});
564+
565+
afterEach(async () => {
566+
await browser.close();
567+
await server.stop();
568+
fs.truncateSync(file);
569+
});
570+
571+
it("should reload when file content is changed", async () => {
572+
page
573+
.on("console", (message) => {
574+
consoleMessages.push(message);
575+
})
576+
.on("pageerror", (error) => {
577+
pageErrors.push(error);
578+
});
579+
580+
const response = await page.goto(`http://localhost:${port}/`, {
581+
waitUntil: "networkidle0",
582+
});
583+
584+
expect(response.status()).toMatchSnapshot("response status");
585+
586+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
587+
"console messages",
588+
);
589+
590+
expect(pageErrors).toMatchSnapshot("page errors");
591+
592+
// change file content
593+
fs.writeFileSync(file, "Kurosaki Ichigo", "utf8");
594+
595+
await new Promise((resolve) => {
596+
server.staticWatchers[0].on("change", async (changedPath) => {
597+
// page reload
598+
await page.waitForNavigation({ waitUntil: "networkidle0" });
599+
600+
expect(changedPath).toBe(file);
601+
602+
resolve();
603+
});
604+
});
605+
});
606+
607+
it("should not reload when an ignored glob file is changed", async () => {
608+
page
609+
.on("console", (message) => {
610+
consoleMessages.push(message);
611+
})
612+
.on("pageerror", (error) => {
613+
pageErrors.push(error);
614+
});
615+
616+
const response = await page.goto(`http://localhost:${port}/`, {
617+
waitUntil: "networkidle0",
618+
});
619+
620+
expect(response.status()).toMatchSnapshot("response status");
621+
622+
// change ignored file content
623+
fs.writeFileSync(ignoredFile, "// changed", "utf8");
624+
625+
// wait a bit to ensure no reload happens
626+
await new Promise((resolve) => {
627+
let changed = false;
628+
629+
server.staticWatchers[0].on("change", () => {
630+
changed = true;
631+
});
632+
633+
setTimeout(() => {
634+
expect(changed).toBe(false);
635+
resolve();
636+
}, 2000);
637+
});
638+
639+
// restore file
640+
fs.writeFileSync(ignoredFile, "// test file\n", "utf8");
641+
});
642+
});
643+
531644
describe("should not crash if file doesn't exist", () => {
532645
const nonExistFile = path.join(watchDir, "assets/non-exist.txt");
533646
let compiler;

0 commit comments

Comments
 (0)