bonsai-analyzer
Version:
Trim your dependency tree.
182 lines (170 loc) • 5.08 kB
JavaScript
;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _getRawStats = _interopRequireWildcard(require("../getRawStats"));
// eslint-disable-next-line no-unused-vars
function isRawStats(stats) {// Mock function to validate flowtype
}
function expectSingleConfigToFallThroughToMulti() {
// eslint-disable-next-line no-console
expect(console.warn).toHaveBeenCalledTimes(1); // eslint-disable-next-line no-console
expect(console.warn).toHaveBeenCalledWith(`Could not find 'chunks' field.`);
}
describe('getRawStats', () => {
beforeEach(() => {
console.warn = jest.fn();
});
describe('getStatsJson', () => {
it('should return a typed object when a single json stats is passed', () => {
const json = {
chunks: [],
modules: []
};
const stats = (0, _getRawStats.getStatsJson)(json);
isRawStats(stats);
expect(stats).toEqual(json); // eslint-disable-next-line no-console
expect(console.warn).not.toHaveBeenCalled();
});
it('should remove module.source fields from the typed dataset', () => {
const json = {
chunks: [],
modules: [{
source: 'hello world'
}]
};
const stats = (0, _getRawStats.getStatsJson)(json);
isRawStats(stats);
expect(stats).toEqual({
chunks: [],
modules: [{}]
});
});
const failureModes = [{
message: 'should throw when chunks is missing',
json: {
modules: []
}
}, {
message: 'should throw when modules is missing',
json: {
chunks: []
}
}, {
message: 'should throw when chunks is not an array',
json: {
chunks: true,
modules: []
}
}, {
message: 'should throw when modules is not an array',
json: {
chunks: [],
modules: true
}
}];
failureModes.forEach(fixture => {
it(fixture.message, () => {
expect(() => (0, _getRawStats.getStatsJson)(fixture.json)).toThrow(); // eslint-disable-next-line no-console
expect(console.warn).toHaveBeenCalledTimes(1);
});
});
});
describe('getMultiStatsJson', () => {
it('should return a typed object when multi-config json stats is passed', () => {
const json = {
children: [{
chunks: [],
modules: []
}]
};
const stats = (0, _getRawStats.getMultiStatsJson)(json);
isRawStats(stats.children[0]);
expect(stats).toEqual(json); // eslint-disable-next-line no-console
expect(console.warn).not.toHaveBeenCalled();
});
const failureModes = [{
message: 'should throw when children field is missing',
json: {}
}, {
message: 'should throw when children field is not an array',
json: {
children: true
}
}, {
message: 'should throw when children[0].chunks is missing',
json: {
children: [{
// chunks: [],
modules: []
}]
}
}, {
message: 'should throw when children[0].modules is missing',
json: {
children: [{
chunks: [] // modules: [],
}]
}
}, {
message: 'should throw when children[0].chunks is not an array',
json: {
children: [{
chunks: true,
modules: []
}]
}
}, {
message: 'should throw when children[0].modules is not an array',
json: {
children: [{
chunks: [],
modules: true
}]
}
}];
failureModes.forEach(fixture => {
it(fixture.message, () => {
expect(() => (0, _getRawStats.getMultiStatsJson)(fixture.json)).toThrow(); // eslint-disable-next-line no-console
expect(console.warn).toHaveBeenCalledTimes(1);
});
});
});
describe('getRawStats', () => {
it('should detect and accept single configs', () => {
const json = {
chunks: [],
modules: []
};
const stats = (0, _getRawStats.default)(json);
isRawStats(stats['test-stats.json']);
expect(stats).toEqual([json]); // eslint-disable-next-line no-console
expect(console.warn).not.toHaveBeenCalled();
});
it('should detect and accept multi configs', () => {
const json = {
children: [{
chunks: [],
modules: []
}]
};
const stats = (0, _getRawStats.default)(json);
isRawStats(stats['test-stats.json']);
expect(stats).toEqual([json.children[0]]);
expectSingleConfigToFallThroughToMulti();
});
it('should disambiguate filenames for multi-config stats', () => {
const json = {
children: [{
chunks: [],
modules: []
}, {
chunks: [],
modules: []
}]
};
const stats = (0, _getRawStats.default)(json);
isRawStats(stats['test-stats.json']);
expect(stats).toEqual([json.children[0], json.children[1]]);
expectSingleConfigToFallThroughToMulti();
});
});
});