test(watching): add tests

This commit is contained in:
jkzing 2019-06-01 01:05:10 +08:00
parent df27a5e2bd
commit 6c4a979093
2 changed files with 110 additions and 0 deletions

View File

@ -7,6 +7,8 @@ const MultiWatching = require("../lib/MultiWatching");
const createWatching = () => {
return {
invalidate: jest.fn(),
suspend: jest.fn(),
resume: jest.fn(),
close: jest.fn()
};
};
@ -43,6 +45,20 @@ describe("MultiWatching", () => {
});
});
describe("suspend", () => {
it("suspends each watching", () => {
myMultiWatching.suspend();
expect(watchings[0].suspend.mock.calls.length).toBe(1);
expect(watchings[1].suspend.mock.calls.length).toBe(1);
});
it("resume each watching", () => {
myMultiWatching.resume();
expect(watchings[0].resume.mock.calls.length).toBe(1);
expect(watchings[1].resume.mock.calls.length).toBe(1);
});
});
describe("close", () => {
let callback;
const callClosedFinishedCallback = watching => {

94
test/WatchSuspend.test.js Normal file
View File

@ -0,0 +1,94 @@
"use strict";
/*globals describe it */
const path = require("path");
const fs = require("fs");
const webpack = require("../");
describe("WatchSuspend", () => {
if (process.env.NO_WATCH_TESTS) {
it.skip("long running tests excluded", () => {});
return;
}
jest.setTimeout(5000);
describe("suspend ans resume watcher", () => {
const fixturePath = path.join(
__dirname,
"fixtures",
"temp-watch-" + Date.now()
);
const filePath = path.join(fixturePath, "file.js");
let compiler = null;
let watching = null;
beforeAll(() => {
try {
fs.mkdirSync(fixturePath);
} catch (e) {
// skip
}
try {
fs.writeFileSync(filePath, "'foo'", "utf-8");
} 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,
output: {
path: fixturePath,
filename: "bundle.js"
}
});
watching = compiler.watch({ aggregateTimeout: 50 }, err => {
expect(err).toBe(null);
done();
});
});
it("should suspend compilation", done => {
const spy = 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);
done();
}, 100); // 2x aggregateTimeout
});
it("should resume compilation", done => {
compiler.hooks.done.tap("WatchSuspendTest", () => {
const outputPath = path.join(fixturePath, "bundle.js");
expect(fs.readFileSync(outputPath, "utf-8")).toContain("'bar'");
done();
});
watching.resume();
});
});
});