webpack/test/MultiCompiler.test.js

163 lines
4.0 KiB
JavaScript
Raw Normal View History

"use strict";
const path = require("path");
2020-02-13 00:13:53 +08:00
const { createFsFromVolume, Volume } = require("memfs");
2018-12-09 21:26:35 +08:00
const webpack = require("..");
const createMultiCompiler = () => {
2018-02-25 18:46:17 +08:00
const compiler = webpack([
{
context: path.join(__dirname, "fixtures"),
entry: "./a.js"
},
{
context: path.join(__dirname, "fixtures"),
entry: "./b.js"
}
]);
compiler.outputFileSystem = createFsFromVolume(new Volume());
compiler.watchFileSystem = {
watch(a, b, c, d, e, f, g) {}
};
return compiler;
};
2020-03-29 06:10:15 +08:00
describe("MultiCompiler", function () {
jest.setTimeout(20000);
it("should trigger 'run' for each child compiler", done => {
const compiler = createMultiCompiler();
let called = 0;
compiler.hooks.run.tap("MultiCompiler test", () => called++);
compiler.run(err => {
2018-02-25 18:46:17 +08:00
if (err) {
throw err;
} else {
2018-01-24 23:00:43 +08:00
expect(called).toBe(2);
done();
}
});
});
it("should trigger 'watchRun' for each child compiler", done => {
const compiler = createMultiCompiler();
let called = 0;
compiler.hooks.watchRun.tap("MultiCompiler test", () => called++);
const watcher = compiler.watch(1000, err => {
2018-02-25 18:46:17 +08:00
if (err) {
throw err;
} else {
watcher.close();
2018-01-24 23:00:43 +08:00
expect(called).toBe(2);
done();
}
});
});
2020-05-07 00:53:06 +08:00
it("should not be running twice at a time (run)", function (done) {
const compiler = createMultiCompiler();
compiler.run((err, stats) => {
if (err) return done(err);
});
compiler.run((err, stats) => {
if (err) return done();
});
});
2020-05-07 00:53:06 +08:00
it("should not be running twice at a time (watch)", function (done) {
const compiler = createMultiCompiler();
const watcher = compiler.watch({}, (err, stats) => {
if (err) return done(err);
});
compiler.watch({}, (err, stats) => {
if (err) return watcher.close(done);
});
});
2020-05-07 00:53:06 +08:00
it("should not be running twice at a time (run - watch)", function (done) {
const compiler = createMultiCompiler();
compiler.run((err, stats) => {
if (err) return done(err);
});
compiler.watch({}, (err, stats) => {
if (err) return done();
});
});
2020-05-07 00:53:06 +08:00
it("should not be running twice at a time (watch - run)", function (done) {
const compiler = createMultiCompiler();
let watcher;
watcher = compiler.watch({}, (err, stats) => {
if (err) return done(err);
});
compiler.run((err, stats) => {
if (err) return watcher.close(done);
});
});
2020-05-07 00:53:06 +08:00
it("should not be running twice at a time (instance cb)", function (done) {
const compiler = webpack(
{
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
},
() => {}
);
compiler.outputFileSystem = createFsFromVolume(new Volume());
compiler.run((err, stats) => {
if (err) return done();
});
});
2020-03-29 06:10:15 +08:00
it("should run again correctly after first compilation", function (done) {
const compiler = createMultiCompiler();
compiler.run((err, stats) => {
if (err) return done(err);
compiler.run((err, stats) => {
if (err) return done(err);
done();
});
});
});
2020-03-29 06:10:15 +08:00
it("should watch again correctly after first compilation", function (done) {
const compiler = createMultiCompiler();
compiler.run((err, stats) => {
if (err) return done(err);
let watcher;
watcher = compiler.watch({}, (err, stats) => {
if (err) return done(err);
watcher.close(done);
});
});
});
2020-03-29 06:10:15 +08:00
it("should run again correctly after first closed watch", function (done) {
const compiler = createMultiCompiler();
const watching = compiler.watch({}, (err, stats) => {
if (err) return done(err);
});
watching.close(() => {
compiler.run((err, stats) => {
if (err) return done(err);
done();
});
});
});
2020-03-29 06:10:15 +08:00
it("should watch again correctly after first closed watch", function (done) {
const compiler = createMultiCompiler();
const watching = compiler.watch({}, (err, stats) => {
if (err) return done(err);
});
watching.close(() => {
let watcher;
watcher = compiler.watch({}, (err, stats) => {
if (err) return done(err);
watcher.close(done);
});
});
});
});