Skip to content

Instantly share code, notes, and snippets.

@cacheflow
Created October 12, 2018 00:26
Show Gist options
  • Save cacheflow/b4fc7d5adfc40f3b0015d9ce55e9d4e5 to your computer and use it in GitHub Desktop.
Save cacheflow/b4fc7d5adfc40f3b0015d9ce55e9d4e5 to your computer and use it in GitHub Desktop.
"use strict";
/* globals describe it */
const path = require("path");
const MemoryFs = require("memory-fs");
const webpack = require("../");
const fs = require('fs');
const createCompiler = config => {
const compiler = webpack(config);
compiler.outputFileSystem = new MemoryFs();
return compiler;
};
const tempFolderPath = path.join(
__dirname,
"temp"
);
const tempFilePath = path.join(tempFolderPath, 'temp-file.js')
const tempFile2Path = path.join(tempFolderPath, 'temp-file2.js')
const createSingleCompiler = () => {
return createCompiler({
entry: tempFilePath,
watch: true,
output: {
path: tempFolderPath,
filename: "bundle.js"
},
});
};
describe("RemovedFiles", () => {
let compiler;
jest.setTimeout(20000);
function cleanup() {
rimraf.sync(tempFolderPath);
}
function handleError(err) {
if (err) done(err);
}
beforeAll(() => {
cleanup()
fs.mkdirSync(tempFolderPath);
fs.writeFileSync(tempFilePath, "module.exports = function temp() {return 'temp file';};\n require('./temp-file2')",
"utf-8",
)
fs.writeFileSync(tempFile2Path, "module.exports = function temp2() {return 'temp file 2';};",
"utf-8",
)
compiler = createSingleCompiler(tempFilePath);
});
afterAll(done => {
cleanup()
done()
});
it("should track removed files when they've been deleted in watchRun", (done) => {
const compiler = createSingleCompiler();
let watcher;
let unlinkFile = () => fs.unlinkSync(tempFilePath)
setTimeout(unlinkFile, 3000)
compiler.hooks.watchRun.tap('RemovedFilesTest', (compiler, err) => {
if(err) {
done(err)
}
if(compiler.removedFiles.length > 0) {
setTimeout(() => {
expect(compiler.removedFiles).toContain(tempFilePath)
watcher.close()
done()
}, 500)
}
});
watcher = compiler.watch(
{
aggregateTimeout: 50
},
(err, stats) => {}
);
});
it("should not track removed files when they have not been deleted in watchRun", () => {
const compiler = createSingleCompiler();
let watcher;
compiler.hooks.watchRun.tap('RemovedFilesTest', (compiler, err) => {
if(err) {
done(err)
}
expect(compiler.removedFiles).toHaveLength(0)
watcher.close()
});
watcher = compiler.watch(
{
aggregateTimeout: 50
},
(err, stats) => {}
);
});
it("should not track removed files when files have been modified", () => {
const compiler = createSingleCompiler();
let watcher;
let changedFile = false;
let updateFile = () => {
fs.writeFileSync(
tempFilePath,
"require('./temp2'); hello world",
"utf-8",
handleError
);
changedFile = true;
}
setTimeout(updateFile, 3000)
compiler.hooks.watchRun.tap('RemovedFilesTest', (compiler, err) => {
if(err) {
done(err)
}
if(changedFile) {
setTimeout(() => {
expect(compiler.removedFiles).toHaveLength(0)
watcher.close()
done()
}, 500)
}
watcher.close()
});
watcher = compiler.watch(
{
aggregateTimeout: 50
},
(err, stats) => {}
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment