Skip to content

Commit

Permalink
test: 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Apr 15, 2020
1 parent b1b6779 commit d04ecff
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 13 deletions.
29 changes: 19 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFile as origReadFile } from "fs";
import { promisify } from "util";
import { dirname } from "path";
import { dirname, resolve as pathResolve } from "path";

import fileSize from "filesize";
import gzip from "gzip-size";
Expand Down Expand Up @@ -39,7 +39,9 @@ export default function filesize(options = {}, env) {

info.bundleSize = fileSize(Buffer.byteLength(code), format);

info.brotliSize = showBrotliSize ? fileSize(brotli.sync(code), format) : "";
info.brotliSize = showBrotliSize
? fileSize(await brotli(code), format)
: "";

if (showMinifiedSize || showGzippedSize) {
const minifiedCode = terser.minify(code).code;
Expand All @@ -54,7 +56,7 @@ export default function filesize(options = {}, env) {
if (codeBefore) {
info.bundleSizeBefore = fileSize(Buffer.byteLength(codeBefore), format);
info.brotliSizeBefore = showBrotliSize
? fileSize(brotli.sync(codeBefore), format)
? fileSize(await brotli(codeBefore), format)
: "";
if (showMinifiedSize || showGzippedSize) {
const minifiedCode = terser.minify(codeBefore).code;
Expand All @@ -67,21 +69,28 @@ export default function filesize(options = {}, env) {
}
}

const reporters = options.reporter || ["boxen"];
const reporters = options.reporter
? Array.isArray(options.reporter)
? options.reporter
: [options.reporter]
: ["boxen"];

return (
await Promise.all(
reporters.map(async (reporter) => {
if (typeof reporter === "string") {
let p;
if (reporter === "boxen") {
reporter = "/reporters/boxen.js";
p = import(
dirname(new URL(import.meta.url).pathname) +
"/reporters/boxen.js"
);
} else {
p = import(pathResolve(process.cwd(), reporter));
}
reporter = (
await import(
dirname(new URL(import.meta.url).pathname) + reporter
)
).default;
reporter = (await p).default;
}

return reporter(
{
format,
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/customReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async function customReporter(opt, outputOptions, info) {
return JSON.stringify([opt, outputOptions, info]);
}
1 change: 1 addition & 0 deletions test/fixtures/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("test");
3 changes: 3 additions & 0 deletions test/fixtures/silentReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async function silentReporter(/* opt, outputOptions, info */) {
return null;
}
264 changes: 261 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,274 @@ import filesize from "../src/index.js";
const x = filesize({}, "test");

const bundle = {
fileName: "bundled-file.js",
fileName: "test/fixtures/bundled-file.js",
code: "function add(first, second) { return first + second; }",
};

async function getStdout(cb) {
const originalStdoutWrite = process.stdout.write.bind(process.stdout);

let output = "";
process.stdout.write = (chunk, encoding, callback) => {
if (typeof chunk === "string") {
output += chunk;
}

return originalStdoutWrite(chunk, encoding, callback);
};

await cb();

process.stdout.write = originalStdoutWrite;
return output;
}

test("fileSize should return a string", async (t) => {
t.is(typeof (await x({ file: "abc.js" }, bundle)), "string");
});

test("fileSize should return a string (with deprecated `dest`)", async (t) => {
t.is(typeof (await x({ dest: "abc.js" }, bundle)), "string");
test("fileSize should allow custom reporter", async (t) => {
const getLoggingData = filesize(
{
reporter: "./test/fixtures/customReporter.js",
},
"test"
);
const val = JSON.parse(await getLoggingData({ file: "abc.js" }, bundle));
t.deepEqual(val, [
{
format: {},
theme: "dark",
// reporter: [AsyncFunction: customReporter],
showBeforeSizes: false,
showGzippedSize: true,
showBrotliSize: false,
showMinifiedSize: true,
},
{ file: "abc.js" },
{
fileName: "test/fixtures/bundled-file.js",
bundleSize: "54 B",
brotliSize: "",
minSize: "29 B",
gzipSize: "49 B",
},
]);
});

test("fileSize should apply light theme", async (t) => {
const getLoggingData = filesize({ theme: "light" }, "test");
const val = await getLoggingData({ file: "abc.js" }, bundle);
// 30 is for black
// eslint-disable-next-line no-control-regex
t.regex(val, /\u001b\[30m\u001b\[1mDestination:/);

// 34 is for blue
// eslint-disable-next-line no-control-regex
t.regex(val, /\u001b\[34mabc\.js/);
});

test("fileSize should apply dark theme", async (t) => {
const getLoggingData = filesize({ theme: "dark" }, "test");
const val = await getLoggingData({ file: "abc.js" }, bundle);
// 32 is for green
// eslint-disable-next-line no-control-regex
t.regex(val, /\u001b\[32m\u001b\[1mDestination:/);

// 33 is for yellow
// eslint-disable-next-line no-control-regex
t.regex(val, /\u001b\[33mabc\.js/);
});

test("fileSize should generate a bundle", async (t) => {
let { name, generateBundle } = filesize();
t.is(name, "filesize");
let val = await getStdout(async () => {
await generateBundle(
{
file: "test/fixtures/bundled-file.js",
},
{
"test/fixtures/sample.js": {
fileName: "test/fixtures/sample.js",
code: 'console.log("test");\n',
},
"some/asset.html": {
fileName: "some/asset.html",
code: "<html></html>",
type: "asset",
},
}
);
});
t.regex(val, /Destination:/);
t.regex(val, /21 B/);

({ name, generateBundle } = filesize({
reporter: "./test/fixtures/silentReporter.js",
}));
t.is(name, "filesize");
val = await getStdout(async () => {
await generateBundle(
{
file: "test/fixtures/bundled-file.js",
},
{
"test/fixtures/sample.js": {
fileName: "test/fixtures/sample.js",
code: 'console.log("test");\n',
},
"some/asset.html": {
fileName: "some/asset.html",
code: "<html></html>",
type: "asset",
},
}
);
});
t.is(val, "");
});

test("fileSize should apply `showBeforeSizes` option", async (t) => {
const getLoggingData = filesize({ showBeforeSizes: true }, "test");
const val = await getLoggingData(
{ file: "./test/fixtures/sample.js" },
bundle
);
// eslint-disable-next-line no-control-regex
t.regex(val, /\(was \u001b\[33m21 B/);
});

test("fileSize should apply `showBeforeSizes` option (with deprecated `dest`)", async (t) => {
const getLoggingData = filesize({ showBeforeSizes: true }, "test");
const val = await getLoggingData(
{ dest: "./test/fixtures/sample.js" },
bundle
);
// eslint-disable-next-line no-control-regex
t.regex(val, /\(was \u001b\[33m21 B/);
});

test("fileSize should fallback to printing `fileName`", async (t) => {
const val = await x({}, bundle);
// eslint-disable-next-line no-control-regex
t.regex(val, /\u001b\[1mBundle Name:/);
});

test("fileSize should avoid showing minified and gzipped sizes when disabled", async (t) => {
let getLoggingData = filesize(
{
showMinifiedSize: false,
showGzippedSize: false,
},
"test"
);
let val = await getLoggingData({}, bundle);
t.notRegex(val, /Minified Size/);
t.notRegex(val, /Gzipped Size/);

getLoggingData = filesize(
{
showMinifiedSize: true,
showGzippedSize: false,
},
"test"
);
val = await getLoggingData({}, bundle);
t.regex(val, /Minified Size/);
t.notRegex(val, /Gzipped Size/);

getLoggingData = filesize(
{
showMinifiedSize: false,
showGzippedSize: true,
},
"test"
);
val = await getLoggingData({}, bundle);
t.notRegex(val, /Minified Size/);
t.regex(val, /Gzipped Size/);
});

test("fileSize should show Brotli size when configured", async (t) => {
const getLoggingData = filesize(
{
showBrotliSize: true,
},
"test"
);
const val = await getLoggingData({}, bundle);
t.regex(val, /Brotli size/);
});

test("fileSize should show before Brotli size when configured", async (t) => {
let getLoggingData = filesize(
{
showBeforeSizes: true,
showBrotliSize: true,
},
"test"
);
let val = await getLoggingData({ file: "./test/fixtures/sample.js" }, bundle);
t.regex(val, /Brotli size/);
t.regex(val, /Minified Size/);
t.regex(val, /Gzipped Size/);
// eslint-disable-next-line no-control-regex
t.regex(val, /\(was \u001b\[33m25 B/);

getLoggingData = filesize(
{
showBeforeSizes: true,
showBrotliSize: true,
showMinifiedSize: false,
showGzippedSize: false,
},
"test"
);
val = await getLoggingData({ file: "./test/fixtures/sample.js" }, bundle);
t.regex(val, /Brotli size/);
t.notRegex(val, /Minified Size/);
t.notRegex(val, /Gzipped Size/);
// eslint-disable-next-line no-control-regex
t.regex(val, /\(was \u001b\[33m25 B/);

getLoggingData = filesize(
{
showBeforeSizes: true,
showBrotliSize: true,
showMinifiedSize: true,
showGzippedSize: false,
},
"test"
);
val = await getLoggingData({ file: "./test/fixtures/sample.js" }, bundle);
t.regex(val, /Brotli size/);
t.regex(val, /Minified Size/);
t.notRegex(val, /Gzipped Size/);
// eslint-disable-next-line no-control-regex
t.regex(val, /\(was \u001b\[33m25 B/);

getLoggingData = filesize(
{
showBeforeSizes: true,
showBrotliSize: true,
showMinifiedSize: false,
showGzippedSize: true,
},
"test"
);
val = await getLoggingData({ file: "./test/fixtures/sample.js" }, bundle);
t.regex(val, /Brotli size/);
t.notRegex(val, /Minified Size/);
t.regex(val, /Gzipped Size/);
// eslint-disable-next-line no-control-regex
t.regex(val, /\(was \u001b\[33m25 B/);
});

test("fileSize should fallback to printing nothing with no file info", async (t) => {
const val = await x({}, { ...bundle, fileName: undefined });
// eslint-disable-next-line no-control-regex
t.notRegex(val, /\u001b\[1mBundle Name:/);
});

test("fileSize should return correct string", async (t) => {
Expand Down

0 comments on commit d04ecff

Please sign in to comment.