Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jamesmillerburgess/4dd6372346677785d60de2a0bdf51983 to your computer and use it in GitHub Desktop.
Save jamesmillerburgess/4dd6372346677785d60de2a0bdf51983 to your computer and use it in GitHub Desktop.
import fs from 'fs';
import { WebAppInternals } from 'meteor/webapp';
import * as fns from './meteor-bundle-size-check';
jest.mock('fs');
const STATIC_FILES_SAMPLE = {
'/hello.stats.json': {
type: 'json',
absolutePath: '/hello.stats.json',
hash: '123',
},
};
const STATIC_FILES_BY_ARCH_SAMPLE = {
'web.browser': {
'/hello.stats.json': {
type: 'json',
absolutePath: '/hello.stats.json',
hash: '123',
},
},
'web.browser.legacy': {
'/helloLegacy.stats.json': {
type: 'json',
absolutePath: '/helloLegacy.stats.json',
hash: '321',
},
},
};
describe('statFileFilter', () => {
it('filters files based on the type and absolute path', () => {
const file = { type: 'json', absolutePath: 'x.stats.json' };
expect(fns.statFileFilter(file)).toBe(true);
file.absolutePath = 'x.stats';
expect(fns.statFileFilter(file)).toBeFalsy();
file.absolutePath = undefined;
expect(fns.statFileFilter(file)).toBeFalsy();
file.type = 'xml';
expect(fns.statFileFilter(file)).toBeFalsy();
});
});
describe('readOrNull', () => {
it('reads the file or returns null in case of an error', () => {
fs.readFileSync = jest
.fn()
.mockReturnValue('{"totalMinifiedGzipBytes":10}');
expect(fns.readOrNull()).toEqual({ totalMinifiedGzipBytes: 10 });
fs.readFileSync.mockImplementation(() => {
throw new Error();
});
expect(fns.readOrNull()).toBeNull();
});
});
describe('getStatBundles', () => {
let temp;
beforeEach(() => {
temp = WebAppInternals;
fs.readFileSync = jest
.fn()
.mockReturnValue('{"totalMinifiedGzipBytes":10}');
});
afterEach(() => {
WebAppInternals.staticFiles = temp.staticFiles;
WebAppInternals.staticFilesByArch = temp.staticFilesByArch;
});
it('returns an empty array if there are no files', () => {
WebAppInternals.staticFiles = undefined;
WebAppInternals.staticFilesByArch = undefined;
expect(fns.getStatBundles()).toEqual([]);
});
it('returns the size stats of the staticFiles without architecture', () => {
WebAppInternals.staticFiles = STATIC_FILES_SAMPLE;
WebAppInternals.staticFilesByArch = undefined;
expect(fns.getStatBundles()).toEqual([
{
arch: 'web.browser.legacy',
name: '123',
stats: { totalMinifiedGzipBytes: 10 },
},
]);
});
it('returns the size stats of the staticFiles with architecture', () => {
WebAppInternals.staticFiles = undefined;
WebAppInternals.staticFilesByArch = STATIC_FILES_BY_ARCH_SAMPLE;
expect(fns.getStatBundles()).toEqual([
{
arch: 'web.browser',
name: '123',
stats: { totalMinifiedGzipBytes: 10 },
},
{
arch: 'web.browser.legacy',
name: '321',
stats: { totalMinifiedGzipBytes: 10 },
},
]);
});
});
describe('checkBundleSize', () => {
let temp;
beforeEach(() => {
temp = WebAppInternals;
temp.IS_BUNDLE_CHECK = process.env.IS_BUNDLE_CHECK;
fs.readFileSync = jest
.fn()
.mockReturnValue('{"totalMinifiedGzipBytes":10}');
});
afterEach(() => {
WebAppInternals.staticFiles = temp.staticFiles;
WebAppInternals.staticFilesByArch = temp.staticFilesByArch;
process.env.IS_BUNDLE_CHECK = temp.IS_BUNDLE_CHECK;
});
it('returns undefined if the environment variable is not set', () => {
delete process.env.IS_BUNDLE_CHECK;
expect(fns.checkBundleSize()).toBeUndefined();
});
it('exits the process with code 0 if the bundle is within the threshold (static files)', () => {
process.env.IS_BUNDLE_CHECK = 1;
WebAppInternals.staticFiles = STATIC_FILES_SAMPLE;
WebAppInternals.staticFilesByArch = undefined;
fns.checkBundleSize();
expect(process.exit).toHaveBeenLastCalledWith(0);
});
it('exits the process with code 1 if the bundle is outside the threshold (static files)', () => {
process.env.IS_BUNDLE_CHECK = 1;
fs.readFileSync = jest
.fn()
.mockReturnValue('{"totalMinifiedGzipBytes":999999999}');
WebAppInternals.staticFiles = STATIC_FILES_SAMPLE;
WebAppInternals.staticFilesByArch = undefined;
fns.checkBundleSize();
expect(process.exit).toHaveBeenLastCalledWith(1);
});
it('exits the process with code 1 if the bundle is outside the threshold (static files by arch)', () => {
process.env.IS_BUNDLE_CHECK = 1;
fs.readFileSync = jest
.fn()
.mockReturnValue('{"totalMinifiedGzipBytes":999999999}');
WebAppInternals.staticFiles = undefined;
WebAppInternals.staticFilesByArch = STATIC_FILES_BY_ARCH_SAMPLE;
fns.checkBundleSize();
expect(process.exit).toHaveBeenLastCalledWith(1);
});
});
describe('formatBytes', () => {
it('formats bytes correctly', () => {
expect(fns.formatBytes(0)).toBe('0 Bytes');
expect(fns.formatBytes(1)).toBe('1 Byte');
expect(fns.formatBytes(1024)).toBe('1 KB');
expect(fns.formatBytes(1034)).toBe('1.01 KB');
expect(fns.formatBytes(1034, 0)).toBe('1 KB');
expect(fns.formatBytes(1034, -1)).toBe('1 KB');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment