webpack/test/ChangesAndRemovals.test.js

155 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-09-13 09:27:16 +08:00
"use strict";
const path = require("path");
const MemoryFs = require("memory-fs");
const webpack = require("..");
const fs = require("graceful-fs");
const rimraf = require("rimraf");
const createCompiler = config => {
const compiler = webpack(config);
compiler.outputFileSystem = new MemoryFs();
return compiler;
};
const tempFolderPath = path.join(__dirname, "ChangesAndRemovalsTemp");
2019-09-13 09:27:16 +08:00
const tempFilePath = path.join(tempFolderPath, "temp-file.js");
const tempFile2Path = path.join(tempFolderPath, "temp-file2.js");
const createSingleCompiler = () => {
return createCompiler({
entry: tempFilePath,
output: {
path: tempFolderPath,
filename: "bundle.js"
}
});
};
const onceDone = (compiler, action) => {
let initial = true;
compiler.hooks.done.tap("ChangesAndRemovalsTest", () => {
if (!initial) return;
initial = false;
2019-11-07 04:35:54 +08:00
setTimeout(action, 1000);
});
};
2019-11-15 22:14:03 +08:00
const getChanges = compiler => {
const modifiedFiles = compiler.modifiedFiles;
const removedFiles = compiler.removedFiles;
return {
removed: removedFiles && Array.from(removedFiles),
modified: modifiedFiles && Array.from(modifiedFiles)
};
};
2019-10-22 16:22:15 +08:00
function cleanup(callback) {
rimraf(tempFolderPath, callback);
2019-09-13 09:27:16 +08:00
}
function createFiles() {
fs.mkdirSync(tempFolderPath, { recursive: true });
2019-09-13 09:27:16 +08:00
fs.writeFileSync(
tempFilePath,
"module.exports = function temp() {return 'temp file';};\n require('./temp-file2')",
"utf-8"
);
2019-09-13 09:27:16 +08:00
fs.writeFileSync(
tempFile2Path,
"module.exports = function temp2() {return 'temp file 2';};",
"utf-8"
);
}
2019-09-23 15:24:15 +08:00
describe("ChangesAndRemovals", () => {
2019-09-13 09:27:16 +08:00
if (process.env.NO_WATCH_TESTS) {
it.skip("watch tests excluded", () => {});
return;
}
2019-11-07 04:17:33 +08:00
jest.setTimeout(30000);
2019-09-13 09:27:16 +08:00
2019-10-22 16:22:15 +08:00
beforeEach(done => {
cleanup(err => {
if (err) return done(err);
createFiles();
// Wait 2.5s after creating the files,
// otherwise the newly-created files will trigger the webpack watch mode to re-compile.
setTimeout(done, 2500);
2019-10-22 16:22:15 +08:00
});
2019-09-13 09:27:16 +08:00
});
2019-10-22 16:22:15 +08:00
afterEach(cleanup);
2019-09-13 09:27:16 +08:00
2019-11-07 04:35:54 +08:00
it("should not track modified/removed files during initial watchRun", done => {
2019-09-13 09:27:16 +08:00
const compiler = createSingleCompiler();
let watcher;
2019-10-22 05:59:50 +08:00
const watchRunFinished = new Promise(resolve => {
compiler.hooks.watchRun.tap("ChangesAndRemovalsTest", compiler => {
2019-11-15 22:14:03 +08:00
expect(getChanges(compiler)).toEqual({
removed: undefined,
modified: undefined
});
2019-09-13 09:27:16 +08:00
resolve();
});
});
2019-11-07 04:35:54 +08:00
watcher = compiler.watch({ aggregateTimeout: 200 }, err => {
if (err) done(err);
});
2019-09-13 09:27:16 +08:00
watchRunFinished.then(() => {
2019-10-22 05:59:50 +08:00
watcher.close(done);
2019-09-13 09:27:16 +08:00
});
});
2019-11-07 04:35:54 +08:00
it("should track modified files when they've been modified", done => {
2019-09-13 09:27:16 +08:00
const compiler = createSingleCompiler();
let watcher;
compiler.hooks.watchRun.tap("ChangesAndRemovalsTest", compiler => {
2019-11-07 04:35:54 +08:00
if (!watcher) return;
2019-10-22 05:59:50 +08:00
if (!compiler.modifiedFiles) return;
2019-11-15 22:14:03 +08:00
expect(getChanges(compiler)).toEqual({
modified: [tempFilePath],
removed: []
});
2019-10-22 05:59:50 +08:00
watcher.close(done);
2019-11-07 04:35:54 +08:00
watcher = null;
2019-09-13 09:27:16 +08:00
});
2019-11-07 04:35:54 +08:00
watcher = compiler.watch({ aggregateTimeout: 200 }, err => {
if (err) done(err);
});
2019-09-13 09:27:16 +08:00
onceDone(compiler, () => {
2019-11-07 04:35:54 +08:00
fs.appendFileSync(tempFilePath, "\nlet x = 'file modified';");
});
2019-09-13 09:27:16 +08:00
});
2019-11-07 04:35:54 +08:00
it("should track removed file when removing file", done => {
2019-09-13 09:27:16 +08:00
const compiler = createSingleCompiler();
let watcher;
compiler.hooks.watchRun.tap("ChangesAndRemovalsTest", compiler => {
2019-11-07 04:35:54 +08:00
if (!watcher) return;
if (!compiler.modifiedFiles) return;
2019-11-15 22:14:03 +08:00
expect(getChanges(compiler)).toEqual({
removed: [tempFilePath],
modified: []
});
2019-10-22 05:59:50 +08:00
watcher.close(done);
2019-11-07 04:35:54 +08:00
watcher = null;
2019-09-13 09:27:16 +08:00
});
2019-11-07 04:35:54 +08:00
watcher = compiler.watch({ aggregateTimeout: 200 }, err => {
if (err) done(err);
2019-09-13 09:27:16 +08:00
});
onceDone(compiler, () => {
2019-11-07 04:35:54 +08:00
fs.unlinkSync(tempFilePath);
});
2019-09-13 09:27:16 +08:00
});
});