webpack/test/WatchSuspend.test.js

97 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-06-01 01:05:10 +08:00
"use strict";
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);
2019-06-03 23:49:22 +08:00
describe("suspend and resume watcher", () => {
2019-06-01 01:05:10 +08:00
const fixturePath = path.join(
__dirname,
"fixtures",
"temp-watch-" + Date.now()
);
const filePath = path.join(fixturePath, "file.js");
2019-06-03 23:49:22 +08:00
const outputPath = path.join(fixturePath, "bundle.js");
2019-06-01 01:05:10 +08:00
let compiler = null;
let watching = null;
2019-06-03 23:49:22 +08:00
let onChange = null;
2019-06-01 01:05:10 +08:00
beforeAll(() => {
try {
fs.mkdirSync(fixturePath);
} catch (e) {
// skip
}
try {
fs.writeFileSync(filePath, "'foo'", "utf-8");
} catch (e) {
// skip
}
compiler = webpack({
mode: "development",
entry: filePath,
output: {
path: fixturePath,
filename: "bundle.js"
}
});
2019-06-03 23:49:22 +08:00
watching = compiler.watch({ aggregateTimeout: 50 }, () => {});
compiler.hooks.done.tap("WatchSuspendTest", () => {
if (onChange) onChange();
2019-06-01 01:05:10 +08:00
});
});
2019-06-03 23:49:22 +08:00
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();
};
});
2019-06-01 01:05:10 +08:00
it("should suspend compilation", done => {
2019-06-03 23:49:22 +08:00
onChange = jest.fn();
2019-06-01 01:05:10 +08:00
watching.suspend();
fs.writeFileSync(filePath, "'bar'", "utf-8");
setTimeout(() => {
2019-06-03 23:49:22 +08:00
expect(onChange.mock.calls.length).toBe(0);
onChange = null;
2019-06-01 01:05:10 +08:00
done();
2019-06-03 23:49:22 +08:00
}, 1000);
2019-06-01 01:05:10 +08:00
});
it("should resume compilation", done => {
2019-06-03 23:49:22 +08:00
onChange = () => {
2019-06-01 01:05:10 +08:00
expect(fs.readFileSync(outputPath, "utf-8")).toContain("'bar'");
2019-06-03 23:49:22 +08:00
onChange = null;
2019-06-01 01:05:10 +08:00
done();
2019-06-03 23:49:22 +08:00
};
2019-06-01 01:05:10 +08:00
watching.resume();
});
});
});