webpack/test/WatchDetection.test.js

192 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-01-18 23:39:17 +08:00
"use strict";
const path = require("path");
const fs = require("graceful-fs");
2025-07-03 17:06:45 +08:00
const { Volume, createFsFromVolume } = require("memfs");
2016-05-06 19:38:21 +08:00
2018-12-09 21:26:35 +08:00
const webpack = require("..");
2016-05-06 19:38:21 +08:00
2017-01-18 23:39:17 +08:00
describe("WatchDetection", () => {
2018-02-25 18:46:17 +08:00
if (process.env.NO_WATCH_TESTS) {
2024-06-11 20:32:02 +08:00
// eslint-disable-next-line jest/no-disabled-tests
2018-04-12 05:54:02 +08:00
it.skip("long running tests excluded", () => {});
2017-12-20 23:53:56 +08:00
return;
}
jest.setTimeout(20000);
2018-01-31 04:03:23 +08:00
2021-05-19 21:48:00 +08:00
createTestCase(100, true);
createTestCase(10, true);
createTestCase(600, true);
2018-02-25 18:46:17 +08:00
for (let changeTimeout = 10; changeTimeout < 100; changeTimeout += 10) {
2016-05-06 19:38:21 +08:00
createTestCase(changeTimeout);
}
2018-02-25 18:46:17 +08:00
for (let changeTimeout = 200; changeTimeout <= 2000; changeTimeout += 200) {
2016-05-06 19:38:21 +08:00
createTestCase(changeTimeout);
}
2025-04-22 20:42:33 +08:00
/**
* @param {number} changeTimeout change timeout
* @param {boolean=} invalidate need invalidate?
*/
2021-05-19 21:48:00 +08:00
function createTestCase(changeTimeout, invalidate) {
describe(`time between changes ${changeTimeout}ms${
invalidate ? " with invalidate call" : ""
}`, () => {
2018-02-25 18:46:17 +08:00
const fixturePath = path.join(
__dirname,
"fixtures",
2024-07-31 10:39:30 +08:00
`temp-${changeTimeout}`
2018-02-25 18:46:17 +08:00
);
2017-01-18 23:39:17 +08:00
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);
2024-07-31 15:37:05 +08:00
} catch (_err) {
// empty
}
fs.writeFileSync(filePath, "require('./file2')", "utf8");
fs.writeFileSync(file2Path, "original", "utf8");
2016-05-06 19:38:21 +08:00
});
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);
2024-07-31 15:37:05 +08:00
} catch (_err) {
// empty
}
2016-05-07 05:15:07 +08:00
try {
fs.unlinkSync(file2Path);
2024-07-31 15:37:05 +08:00
} catch (_err) {
// empty
}
2016-05-07 05:15:07 +08:00
try {
fs.rmdirSync(fixturePath);
2024-07-31 15:37:05 +08:00
} catch (_err) {
// empty
}
2016-05-07 05:15:07 +08:00
done();
}, 100); // cool down a bit
2016-05-06 19:38:21 +08:00
});
2018-01-24 23:00:43 +08:00
it("should build the bundle correctly", (done) => {
2017-01-18 23:39:17 +08:00
const compiler = webpack({
2018-01-24 23:00:43 +08:00
mode: "development",
2024-07-31 10:39:30 +08:00
entry: `${loaderPath}!${filePath}`,
2016-05-06 19:38:21 +08:00
output: {
2020-02-17 16:31:02 +08:00
path: "/directory",
2016-05-06 19:38:21 +08:00
filename: "bundle.js"
}
});
const memfs = (compiler.outputFileSystem = createFsFromVolume(
2020-02-13 00:13:53 +08:00
new Volume()
));
2017-01-18 23:39:17 +08:00
let onChange;
2017-12-20 23:51:24 +08:00
compiler.hooks.done.tap("WatchDetectionTest", () => {
2018-02-25 18:46: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();
2025-04-22 20:42:33 +08:00
/**
* @returns {void}
*/
2016-05-06 19:38:21 +08:00
function step1() {
2017-01-18 23:39:17 +08:00
onChange = () => {
2018-02-25 18:46:17 +08:00
if (
2020-02-17 16:32:05 +08:00
memfs.readFileSync("/directory/bundle.js") &&
2018-02-25 18:46:17 +08:00
memfs
2020-02-17 16:32:05 +08:00
.readFileSync("/directory/bundle.js")
2018-02-25 18:46:17 +08:00
.toString()
2024-07-31 16:02:41 +08:00
.includes("original")
) {
2016-05-06 19:38:21 +08:00
step2();
}
2017-01-18 23:39:17 +08:00
};
2016-05-06 19:38:21 +08:00
2018-02-25 18:46:17 +08:00
watcher = compiler.watch(
{
aggregateTimeout: 50
},
() => {}
);
2016-05-06 19:38:21 +08:00
}
2025-04-22 20:42:33 +08:00
/**
* @returns {void}
*/
2016-05-06 19:38:21 +08:00
function step2() {
2021-05-19 21:48:00 +08:00
onChange = () => {
expect(compiler.modifiedFiles).toBeDefined();
expect(compiler.removedFiles).toBeDefined();
2021-05-19 21:48:00 +08:00
};
2016-05-06 19:38:21 +08:00
2018-02-25 18:46:17 +08:00
fs.writeFile(
filePath,
"require('./file2'); again",
"utf8",
2018-02-25 18:46:17 +08:00
handleError
);
2016-05-06 19:38:21 +08:00
setTimeout(step3, changeTimeout);
}
2025-04-22 20:42:33 +08:00
/**
* @returns {void}
*/
2016-05-06 19:38:21 +08:00
function step3() {
2021-05-19 21:48:00 +08:00
if (invalidate) watcher.invalidate();
fs.writeFile(file2Path, "wrong", "utf8", handleError);
2016-05-06 19:38:21 +08:00
setTimeout(step4, changeTimeout);
}
2025-04-22 20:42:33 +08:00
/**
* @returns {void}
*/
2016-05-06 19:38:21 +08:00
function step4() {
2017-01-18 23:39:17 +08:00
onChange = () => {
expect(compiler.modifiedFiles).toBeDefined();
expect(compiler.removedFiles).toBeDefined();
2018-02-25 18:46:17 +08:00
if (
memfs
2020-02-17 16:32:05 +08:00
.readFileSync("/directory/bundle.js")
2018-02-25 18:46:17 +08:00
.toString()
2024-07-31 16:02:41 +08:00
.includes("correct")
) {
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", "utf8", handleError);
2016-05-06 19:38:21 +08:00
}
2025-04-22 20:42:33 +08:00
/**
* @returns {void}
*/
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
}
2025-04-22 20:42:33 +08:00
/**
* @param {unknown} err err
*/
2016-05-06 19:38:21 +08:00
function handleError(err) {
2018-02-25 18:46:17 +08:00
if (err) done(err);
2016-05-06 19:38:21 +08:00
}
});
2017-01-18 23:39:17 +08:00
});
2016-05-06 19:38:21 +08:00
}
});