Skip to content

Commit

Permalink
add test cases from #12897 and #12881
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>
  • Loading branch information
sokra and vladar committed May 17, 2021
1 parent 57d79f1 commit 749cfa6
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions test/WatchSuspend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ describe("WatchSuspend", () => {
"temp-watch-" + Date.now()
);
const filePath = path.join(fixturePath, "file.js");
const file2Path = path.join(fixturePath, "file2.js");
const file3Path = path.join(fixturePath, "file3.js");
const outputPath = path.join(fixturePath, "bundle.js");
let compiler = null;
let watching = null;
Expand All @@ -33,6 +35,8 @@ describe("WatchSuspend", () => {
}
try {
fs.writeFileSync(filePath, "'foo'", "utf-8");
fs.writeFileSync(file2Path, "'file2'", "utf-8");
fs.writeFileSync(file3Path, "'file3'", "utf-8");
} catch (e) {
// skip
}
Expand All @@ -45,6 +49,7 @@ describe("WatchSuspend", () => {
}
});
watching = compiler.watch({ aggregateTimeout: 50 }, () => {});

compiler.hooks.done.tap("WatchSuspendTest", () => {
if (onChange) onChange();
});
Expand Down Expand Up @@ -92,5 +97,62 @@ describe("WatchSuspend", () => {
};
watching.resume();
});

it("should not ignore changes during resumed compilation", async () => {
// aggregateTimeout must be long enough for this test
// So set-up new watcher and wait when initial compilation is done
await new Promise(resolve => {
watching.close();
watching = compiler.watch({ aggregateTimeout: 1000 }, resolve);
});
return new Promise(resolve => {
watching.suspend();
fs.writeFileSync(filePath, "'baz'", "utf-8");

// Run resume between "changed" and "aggregated" events
setTimeout(() => {
watching.resume();

setTimeout(() => {
expect(fs.readFileSync(outputPath, "utf-8")).toContain("'baz'");
resolve();
}, 2000);
}, 200);
});
});

it("should not drop changes when suspended", done => {
const aggregateTimeout = 50;
// Trigger initial compilation with file2.js (assuming correct)
fs.writeFileSync(
filePath,
'require("./file2.js"); require("./file3.js")',
"utf-8"
);

onChange = () => {
// Initial compilation is done, start the test
watching.suspend();

// Trigger the first change (works as expected):
fs.writeFileSync(file2Path, "'foo'", "utf-8");

// Trigger the second change _after_ aggregation timeout of the first
setTimeout(() => {
fs.writeFileSync(file3Path, "'bar'", "utf-8");

// Wait when the file3 edit is settled and re-compile
setTimeout(() => {
watching.resume();

onChange = () => {
onChange = null;
expect(fs.readFileSync(outputPath, "utf-8")).toContain("'bar'");
done();
};
}, 200);
}, aggregateTimeout + 50);
};
});
});
});

0 comments on commit 749cfa6

Please sign in to comment.