test(watching): add integration test

This commit is contained in:
jkzing 2019-06-03 23:49:22 +08:00
parent 6c4a979093
commit 460b099a63
2 changed files with 38 additions and 33 deletions

View File

@ -161,6 +161,7 @@ class Watching {
this.watcher.pause();
this.watcher = null;
}
if (this.running) {
this.invalid = true;
return false;
@ -171,6 +172,7 @@ class Watching {
suspend() {
this.suspended = true;
this.invalid = false;
}
resume() {

View File

@ -14,15 +14,17 @@ describe("WatchSuspend", () => {
jest.setTimeout(5000);
describe("suspend ans resume watcher", () => {
describe("suspend and resume watcher", () => {
const fixturePath = path.join(
__dirname,
"fixtures",
"temp-watch-" + Date.now()
);
const filePath = path.join(fixturePath, "file.js");
const outputPath = path.join(fixturePath, "bundle.js");
let compiler = null;
let watching = null;
let onChange = null;
beforeAll(() => {
try {
@ -35,27 +37,6 @@ describe("WatchSuspend", () => {
} catch (e) {
// skip
}
});
afterAll(done => {
watching.close();
compiler = null;
setTimeout(() => {
try {
fs.unlinkSync(filePath);
} catch (e) {
// skip
}
try {
fs.rmdirSync(fixturePath);
} catch (e) {
// skip
}
done();
}, 100); // cool down a bit
});
it("should compile successfully", done => {
compiler = webpack({
mode: "development",
entry: filePath,
@ -64,30 +45,52 @@ describe("WatchSuspend", () => {
filename: "bundle.js"
}
});
watching = compiler.watch({ aggregateTimeout: 50 }, err => {
expect(err).toBe(null);
done();
watching = compiler.watch({ aggregateTimeout: 50 }, () => {});
compiler.hooks.done.tap("WatchSuspendTest", () => {
if (onChange) onChange();
});
});
afterAll(() => {
watching.close();
compiler = null;
try {
fs.unlinkSync(filePath);
} catch (e) {
// skip
}
try {
fs.rmdirSync(fixturePath);
} catch (e) {
// skip
}
});
it("should compile successfully", done => {
onChange = () => {
expect(fs.readFileSync(outputPath, "utf-8")).toContain("'foo'");
onChange = null;
done();
};
});
it("should suspend compilation", done => {
const spy = jest.fn();
onChange = jest.fn();
watching.suspend();
fs.writeFileSync(filePath, "'bar'", "utf-8");
compiler.hooks.compilation.tap("WatchSuspendTest", spy);
// compiler.hooks.done.tap("WatchSuspendTest", spy);
setTimeout(() => {
expect(spy.mock.calls.length).toBe(0);
expect(onChange.mock.calls.length).toBe(0);
onChange = null;
done();
}, 100); // 2x aggregateTimeout
}, 1000);
});
it("should resume compilation", done => {
compiler.hooks.done.tap("WatchSuspendTest", () => {
const outputPath = path.join(fixturePath, "bundle.js");
onChange = () => {
expect(fs.readFileSync(outputPath, "utf-8")).toContain("'bar'");
onChange = null;
done();
});
};
watching.resume();
});
});