webpack/test/WatchDetection.test.js

126 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-01-18 23:39:17 +08:00
"use strict";
/*globals describe it before after */
2017-01-18 23:39:17 +08:00
const path = require("path");
const fs = require("fs");
const MemoryFs = require("memory-fs");
2016-05-06 19:38:21 +08:00
2017-01-18 23:39:17 +08:00
const webpack = require("../");
2016-05-06 19:38:21 +08:00
2017-01-18 23:39:17 +08:00
describe("WatchDetection", () => {
2017-12-20 23:53:56 +08:00
if(process.env.NO_WATCH_TESTS) {
it("long running tests excluded");
return;
}
2017-01-18 23:39:17 +08:00
for(let changeTimeout = 0; changeTimeout < 100; changeTimeout += 10) {
2016-05-06 19:38:21 +08:00
createTestCase(changeTimeout);
}
2018-01-24 23:00:43 +08:00
for(let changeTimeout = 200; changeTimeout <= 2000; changeTimeout += 200) {
2016-05-06 19:38:21 +08:00
createTestCase(changeTimeout);
}
function createTestCase(changeTimeout) {
describe("time between changes " + changeTimeout + "ms", function() {
2018-01-24 23:00:43 +08:00
jest.setTimeout(10000);
2017-01-18 23:39:17 +08:00
const fixturePath = path.join(__dirname, "fixtures", "temp-" + changeTimeout);
const filePath = path.join(fixturePath, "file.js");
const file2Path = path.join(fixturePath, "file2.js");
const loaderPath = path.join(__dirname, "fixtures", "delay-loader.js");
2018-01-24 23:00:43 +08:00
beforeAll(() => {
2016-05-06 19:38:21 +08:00
try {
fs.mkdirSync(fixturePath);
} catch(e) {}
fs.writeFileSync(filePath, "require('./file2')", "utf-8");
fs.writeFileSync(file2Path, "original", "utf-8");
});
2018-01-24 23:00:43 +08:00
afterAll((done) => {
2017-01-18 23:39:17 +08:00
setTimeout(() => {
2016-05-07 05:15:07 +08:00
try {
fs.unlinkSync(filePath);
} catch(e) {}
try {
fs.unlinkSync(file2Path);
} catch(e) {}
try {
fs.rmdirSync(fixturePath);
} catch(e) {}
done();
}, 100); // cool down a bit
2016-05-06 19:38:21 +08:00
});
2018-01-24 23:00:43 +08:00
2017-01-18 23:39:17 +08:00
it("should build the bundle correctly", (done) => {
const compiler = webpack({
2018-01-24 23:00:43 +08:00
mode: "development",
2016-05-06 19:38:21 +08:00
entry: loaderPath + "!" + filePath,
output: {
path: "/",
filename: "bundle.js"
}
});
2017-01-18 23:39:17 +08:00
const memfs = compiler.outputFileSystem = new MemoryFs();
let onChange;
2017-12-20 23:51:24 +08:00
compiler.hooks.done.tap("WatchDetectionTest", () => {
2017-01-18 23:39:17 +08:00
if(onChange)
onChange();
2016-05-06 19:38:21 +08:00
});
2017-01-18 23:39:17 +08:00
let watcher;
2016-05-06 19:38:21 +08:00
step1();
function step1() {
2017-01-18 23:39:17 +08:00
onChange = () => {
2016-05-06 19:38:21 +08:00
if(memfs.readFileSync("/bundle.js") && memfs.readFileSync("/bundle.js").toString().indexOf("original") >= 0)
step2();
2017-01-18 23:39:17 +08:00
};
2016-05-06 19:38:21 +08:00
watcher = compiler.watch({
aggregateTimeout: 50
2017-01-18 23:39:17 +08:00
}, () => {});
2016-05-06 19:38:21 +08:00
}
function step2() {
onChange = null;
fs.writeFile(filePath, "require('./file2'); again", "utf-8", handleError);
setTimeout(step3, changeTimeout);
}
function step3() {
onChange = null;
fs.writeFile(file2Path, "wrong", "utf-8", handleError);
setTimeout(step4, changeTimeout);
}
function step4() {
2017-01-18 23:39:17 +08:00
onChange = () => {
2016-05-06 19:38:21 +08:00
if(memfs.readFileSync("/bundle.js").toString().indexOf("correct") >= 0)
2017-11-15 21:08:11 +08:00
step5();
2017-01-18 23:39:17 +08:00
};
2016-05-06 19:38:21 +08:00
fs.writeFile(file2Path, "correct", "utf-8", handleError);
}
2017-11-15 21:08:11 +08:00
function step5() {
2016-05-06 19:38:21 +08:00
onChange = null;
watcher.close(() => {
2018-01-24 23:00:43 +08:00
setTimeout(done, 500);
});
2016-05-06 19:38:21 +08:00
}
function handleError(err) {
if(err) done(err);
}
});
2017-01-18 23:39:17 +08:00
});
2016-05-06 19:38:21 +08:00
}
});