webpack/test/Stats.test.js

162 lines
3.0 KiB
JavaScript
Raw Normal View History

/*globals describe it */
"use strict";
2018-12-09 21:26:35 +08:00
const webpack = require("..");
const MemoryFs = require("memory-fs");
2018-12-28 16:56:11 +08:00
const compile = options => {
return new Promise((resolve, reject) => {
const compiler = webpack(options);
compiler.outputFileSystem = new MemoryFs();
compiler.run((err, stats) => {
2018-12-28 16:56:11 +08:00
if (err) {
reject(err);
} else {
resolve(stats);
}
});
});
2018-12-28 16:56:11 +08:00
};
describe("Stats", () => {
it("should print env string in stats", async () => {
const stats = await compile({
context: __dirname,
entry: "./fixtures/a"
});
2018-12-28 16:56:11 +08:00
expect(
stats.toString({
all: false,
env: true,
_env: "production"
})
).toBe('Environment (--env): "production"');
expect(
stats.toString({
all: false,
env: true,
_env: {
prod: ["foo", "bar"],
baz: true
}
})
).toBe(
"Environment (--env): {\n" +
' "prod": [\n' +
' "foo",\n' +
' "bar"\n' +
" ],\n" +
' "baz": true\n' +
"}"
);
});
it("should omit all properties with all false", async () => {
const stats = await compile({
context: __dirname,
entry: "./fixtures/a"
});
expect(
stats.toJson({
all: false
})
).toEqual({});
});
describe("chunkGroups", () => {
it("should be empty when there is no additional chunks", async () => {
const stats = await compile({
context: __dirname,
entry: {
entryA: "./fixtures/a",
entryB: "./fixtures/b"
}
});
expect(
stats.toJson({
all: false,
chunkGroups: true
})
).toMatchInlineSnapshot(`
Object {
"namedChunkGroups": Object {
"entryA": Object {
"assets": Array [
"entryA.js",
],
"childAssets": Object {},
"children": Object {},
"chunks": Array [
827,
],
"name": "entryA",
},
"entryB": Object {
"assets": Array [
"entryB.js",
],
"childAssets": Object {},
"children": Object {},
"chunks": Array [
886,
],
"name": "entryB",
},
},
2018-12-28 16:56:11 +08:00
}
`);
});
it("should contain additional chunks", async () => {
const stats = await compile({
context: __dirname,
entry: {
entryA: "./fixtures/a",
entryB: "./fixtures/chunk-b"
}
});
expect(
stats.toJson({
all: false,
chunkGroups: true
})
).toMatchInlineSnapshot(`
Object {
"namedChunkGroups": Object {
"chunkB": Object {
"assets": Array [
"chunkB.js",
],
"childAssets": Object {},
"children": Object {},
"chunks": Array [
787,
],
"name": "chunkB",
},
"entryA": Object {
"assets": Array [
"entryA.js",
],
"childAssets": Object {},
"children": Object {},
"chunks": Array [
827,
],
"name": "entryA",
},
"entryB": Object {
"assets": Array [
"entryB.js",
],
"childAssets": Object {},
"children": Object {},
"chunks": Array [
886,
],
"name": "entryB",
},
2018-12-28 16:56:11 +08:00
},
}
`);
});
});
});