Skip to content

Commit

Permalink
Add test coverage going from webpack stats JSON to end result
Browse files Browse the repository at this point in the history
  • Loading branch information
pas-trop-de-zele committed Aug 18, 2022
1 parent 7a5e10f commit 5b080fa
Show file tree
Hide file tree
Showing 5 changed files with 5,457 additions and 51 deletions.
1 change: 0 additions & 1 deletion src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const FILENAME_QUERY_REGEXP = /\?.*$/u;
const FILENAME_EXTENSIONS = /\.(js|mjs)$/iu;

module.exports = {
getChunkToInitialByEntrypoint,
getViewerData,
readStatsFromFile
};
Expand Down
64 changes: 14 additions & 50 deletions test/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const path = require('path');
const del = require('del');
const childProcess = require('child_process');
const puppeteer = require('puppeteer');
const {getChunkToInitialByEntrypoint} = require('../src/analyzer');

let browser;

Expand Down Expand Up @@ -196,6 +195,20 @@ describe('Analyzer', function () {
await expectValidReport({bundleLabel: 'bundle.js'});
});

it('should map chunks correctly to entrypoints', async function () {
generateReportFrom('with-multiple-entrypoints/stats.json');
const chartData = await getChartData();
expect(chartData).to.containSubset(
require('./stats/with-multiple-entrypoints/expected-chart-data')
);
});

it('should return empty chartData if there are no entrypoints', async function () {
generateReportFrom('with-no-entrypoints/stats.json');
const chartData = await getChartData();
expect(chartData).to.be.empty;
});

describe('options', function () {
describe('title', function () {
it('should take the --title option', async function () {
Expand Down Expand Up @@ -226,55 +239,6 @@ describe('Analyzer', function () {
});
});


describe('getChunkToInitialByEntrypoint', function () {
it('should map chunks correctly to entrypoints', function () {
const bundleStats = {
entrypoints: {
'A': {
name: 'A',
assets: [
{
name: 'chunkA.js'
}
]
},
'B': {
name: 'B',
assets: [
{
name: 'chunkA.js'
},
{
name: 'chunkB.js'
}
]
}
}
};
expect(JSON.stringify(getChunkToInitialByEntrypoint(bundleStats))).to.equal(JSON.stringify({
'chunkA.js': {'A': true, 'B': true},
'chunkB.js': {'B': true}
}));
});

it('should handle when bundlestats is null or undefined ', function () {
expect(JSON.stringify(getChunkToInitialByEntrypoint(null))).to.equal(JSON.stringify({}));
expect(JSON.stringify(getChunkToInitialByEntrypoint(undefined))).to.equal(JSON.stringify({}));
});

it('should handle when bundlestats is emoty', function () {
const bundleStatsWithoutEntryPoints = {};
expect(JSON.stringify(getChunkToInitialByEntrypoint(bundleStatsWithoutEntryPoints))).to.equal(JSON.stringify({}));
});

it('should handle when entrypoints is empty', function () {
const bundleStatsEmptyEntryPoint = {entrypoints: {}};
expect(JSON.stringify(getChunkToInitialByEntrypoint(bundleStatsEmptyEntryPoint))).to.equal(JSON.stringify({}));
});

});

function generateJSONReportFrom(statsFilename) {
childProcess.execSync(`../lib/bin/analyzer.js -m json -r output/report.json stats/${statsFilename}`, {
cwd: __dirname
Expand Down
Loading

0 comments on commit 5b080fa

Please sign in to comment.