@codechecks/build-size-watcher
Version:
Keep your build size in check and detect when it gets too big
261 lines • 6.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../index");
const mockFS = require("mock-fs");
const path_1 = require("path");
const client_1 = require("@codechecks/client");
describe("build-size", () => {
const codeChecksMock = require("../__mocks__/@codechecks/client").codechecks;
beforeEach(() => jest.resetAllMocks());
it("should work not in PR context", async () => {
codeChecksMock.isPr.mockReturnValue(false);
mockFS({
[path_1.join(__dirname, "../build")]: {
"main.12315123.js": "APP JS",
"vendor.123124.js": "VENDOR JS",
"vendor.12466345.css": "VENDOR CSS",
},
});
await index_1.buildSizeWatcher({
files: [
{
path: "build/main.*.js",
},
],
});
mockFS.restore();
expect(client_1.codechecks.report).toBeCalledTimes(0);
expect(client_1.codechecks.saveValue).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
"build-size:Build Size",
Object {
"build/main.*.js": Object {
"files": 1,
"overallSize": 26,
"path": "build/main.*.js",
},
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
});
it("should work in PR context", async () => {
codeChecksMock.isPr.mockReturnValue(true);
codeChecksMock.getValue.mockReturnValue({
"build/main.*.js": {
name: "app",
files: 1,
overallSize: 10,
path: "build/main.*.js",
},
});
mockFS({
[path_1.join(__dirname, "../build")]: {
"main.12315123.js": "APP JS",
"vendor.123124.js": "VENDOR JS",
"vendor.12466345.css": "VENDOR CSS",
},
});
await index_1.buildSizeWatcher({
gzip: false,
files: [
{
path: "build/main.*.js",
},
],
});
mockFS.restore();
expect(client_1.codechecks.report).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Object {
"longDescription": "
| Status | Files | Now | Diff | Max |
|:------:|:-----:|:---:|:----:|:---:|
| changed | build/main.*.js | 6B | -4B (-40.00%) | — |
",
"name": "Build Size",
"shortDescription": "Change: -4B (-40.00%) Total: 6B",
"status": "success",
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
expect(client_1.codechecks.saveValue).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
"build-size:Build Size",
Object {
"build/main.*.js": Object {
"files": 1,
"overallSize": 6,
"path": "build/main.*.js",
},
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
});
it("should work in PR context without baseline", async () => {
codeChecksMock.isPr.mockReturnValue(true);
mockFS({
[path_1.join(__dirname, "../build")]: {
"main.12315123.js": "APP JS",
},
});
await index_1.buildSizeWatcher({
files: [
{
path: "build/main.*.js",
},
],
});
mockFS.restore();
expect(client_1.codechecks.report).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Object {
"longDescription": "
| Status | Files | Now | Diff | Max |
|:------:|:-----:|:---:|:----:|:---:|
| new | build/main.*.js | 26B | +26B (+100.00%) | — |
",
"name": "Build Size",
"shortDescription": "Change: +26B (+100.00%) Total: 26B",
"status": "success",
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
expect(client_1.codechecks.saveValue).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
"build-size:Build Size",
Object {
"build/main.*.js": Object {
"files": 1,
"overallSize": 26,
"path": "build/main.*.js",
},
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
});
it("should work with custom name in PR context", async () => {
codeChecksMock.isPr.mockReturnValue(true);
codeChecksMock.getValue.mockReturnValue({
"build/main.*.js": {
name: "app",
files: 1,
overallSize: 10,
path: "build/main.*.js",
},
});
mockFS({
[path_1.join(__dirname, "../build")]: {
"main.12315123.js": "APP JS",
"vendor.123124.js": "VENDOR JS",
"vendor.12466345.css": "VENDOR CSS",
},
});
await index_1.buildSizeWatcher({
name: "Frontend",
gzip: false,
files: [
{
path: "build/main.*.js",
},
],
});
mockFS.restore();
expect(client_1.codechecks.report).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Object {
"longDescription": "
| Status | Files | Now | Diff | Max |
|:------:|:-----:|:---:|:----:|:---:|
| changed | build/main.*.js | 6B | -4B (-40.00%) | — |
",
"name": "Frontend",
"shortDescription": "Change: -4B (-40.00%) Total: 6B",
"status": "success",
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
expect(client_1.codechecks.saveValue).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
"build-size:Frontend",
Object {
"build/main.*.js": Object {
"files": 1,
"overallSize": 6,
"path": "build/main.*.js",
},
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
});
});
//# sourceMappingURL=index.spec.js.map