webpack/test/Stats.unittest.js

135 lines
3.3 KiB
JavaScript

/*globals describe it */
"use strict";
const Stats = require("../lib/Stats");
describe("Stats", () => {
describe("Error Handling", () => {
describe("does have", () => {
it("hasErrors", () => {
const mockStats = new Stats({
children: [],
errors: ["firstError"],
hash: "1234",
compiler: {
context: ""
}
});
expect(mockStats.hasErrors()).toBe(true);
});
it("hasWarnings", () => {
const mockStats = new Stats({
children: [],
warnings: ["firstError"],
hash: "1234",
compiler: {
context: ""
}
});
expect(mockStats.hasWarnings()).toBe(true);
});
});
describe("does not have", () => {
it("hasErrors", () => {
const mockStats = new Stats({
children: [],
errors: [],
hash: "1234",
compiler: {
context: ""
}
});
expect(mockStats.hasErrors()).toBe(false);
});
it("hasWarnings", () => {
const mockStats = new Stats({
children: [],
warnings: [],
hash: "1234",
compiler: {
context: ""
}
});
expect(mockStats.hasWarnings()).toBe(false);
});
});
describe("children have", () => {
it("hasErrors", () => {
const mockStats = new Stats({
children: [{
getStats: () => new Stats({
errors: ["firstError"],
hash: "5678"
}),
}],
errors: [],
hash: "1234"
});
expect(mockStats.hasErrors()).toBe(true);
});
it("hasWarnings", () => {
const mockStats = new Stats({
children: [{
getStats: () => new Stats({
warnings: ["firstError"],
hash: "5678"
}),
}],
warnings: [],
hash: "1234"
});
expect(mockStats.hasWarnings()).toBe(true);
});
});
it("formatError handles string errors", () => {
const mockStats = new Stats({
errors: ["firstError"],
warnings: [],
assets: [],
entrypoints: new Map(),
chunks: [],
modules: [],
children: [],
hash: "1234",
mainTemplate: {
getPublicPath: () => "path"
},
compiler: {
context: ""
}
});
const obj = mockStats.toJson();
expect(obj.errors[0]).toEqual("firstError");
});
});
describe("Presets", () => {
describe("presetToOptions", () => {
it("returns correct object with 'Normal'", () => {
expect(Stats.presetToOptions("Normal")).toEqual({});
});
it("truthy values behave as 'normal'", () => {
const normalOpts = Stats.presetToOptions("normal");
expect(Stats.presetToOptions("pizza")).toEqual(normalOpts);
expect(Stats.presetToOptions(true)).toEqual(normalOpts);
expect(Stats.presetToOptions(1)).toEqual(normalOpts);
expect(Stats.presetToOptions("verbose")).not.toEqual(normalOpts);
expect(Stats.presetToOptions(false)).not.toEqual(normalOpts);
});
it("returns correct object with 'none'", () => {
expect(Stats.presetToOptions("none")).toEqual({
all: false
});
});
it("falsy values behave as 'none'", () => {
const noneOpts = Stats.presetToOptions("none");
expect(Stats.presetToOptions("")).toEqual(noneOpts);
expect(Stats.presetToOptions(null)).toEqual(noneOpts);
expect(Stats.presetToOptions()).toEqual(noneOpts);
expect(Stats.presetToOptions(0)).toEqual(noneOpts);
expect(Stats.presetToOptions(false)).toEqual(noneOpts);
});
});
});
}, 10000);