Skip to content

Commit

Permalink
clients(lr): add custom config support (#7613)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored and brendankenny committed Apr 4, 2019
1 parent eb0cc68 commit a76e7f2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
19 changes: 13 additions & 6 deletions clients/lightrider-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ const LR_PRESETS = {

/**
* Run lighthouse for connection and provide similar results as in CLI.
*
* If configOverride is provided, lrDevice and categoryIDs are ignored.
* @param {Connection} connection
* @param {string} url
* @param {LH.Flags} flags Lighthouse flags, including `output`
* @param {{lrDevice?: 'desktop'|'mobile', categoryIDs?: Array<string>, logAssets: boolean, keepRawValues: boolean}} lrOpts Options coming from Lightrider
* @param {{lrDevice?: 'desktop'|'mobile', categoryIDs?: Array<string>, logAssets: boolean, keepRawValues: boolean, configOverride?: LH.Config.Json}} lrOpts Options coming from Lightrider
* @return {Promise<string|Array<string>|void>}
*/
async function runLighthouseInLR(connection, url, flags, lrOpts) {
const {lrDevice, categoryIDs, logAssets, keepRawValues} = lrOpts;
const {lrDevice, categoryIDs, logAssets, keepRawValues, configOverride} = lrOpts;

// Certain fixes need to kick in under LR, see https://github.com/GoogleChrome/lighthouse/issues/5839
global.isLightrider = true;
Expand All @@ -38,10 +40,15 @@ async function runLighthouseInLR(connection, url, flags, lrOpts) {
flags.logLevel = flags.logLevel || 'info';
flags.channel = 'lr';

const config = lrDevice === 'desktop' ? LR_PRESETS.desktop : LR_PRESETS.mobile;
if (categoryIDs) {
config.settings = config.settings || {};
config.settings.onlyCategories = categoryIDs;
let config;
if (configOverride) {
config = configOverride;
} else {
config = lrDevice === 'desktop' ? LR_PRESETS.desktop : LR_PRESETS.mobile;
if (categoryIDs) {
config.settings = config.settings || {};
config.settings.onlyCategories = categoryIDs;
}
}

try {
Expand Down
48 changes: 48 additions & 0 deletions clients/test/lightrider-entry-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,53 @@ describe('lightrider-entry', () => {

runStub.mockRestore();
});

it('uses the desktop config preset when device is desktop', async () => {
const runStub = jest.spyOn(Runner, 'run');

const mockConnection = {};
const url = 'https://example.com';

const lrDevice = 'desktop';
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {lrDevice});
const config = runStub.mock.calls[0][1].config;
assert.equal(config.settings.emulatedFormFactor, 'desktop');

runStub.mockRestore();
});

it('uses the mobile config preset when device is mobile', async () => {
const runStub = jest.spyOn(Runner, 'run');

const mockConnection = {};
const url = 'https://example.com';

const lrDevice = 'mobile';
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {lrDevice});
const config = runStub.mock.calls[0][1].config;
assert.equal(config.settings.emulatedFormFactor, 'mobile');

runStub.mockRestore();
});

it('overrides the default config when one is provided', async () => {
const runStub = jest.spyOn(Runner, 'run');

const mockConnection = {};
const url = 'https://example.com';

const configOverride = {
default: 'lighthouse:default',
settings: {
onlyAudits: ['network-requests'],
},
};
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {configOverride});
const config = runStub.mock.calls[0][1].config;
assert.equal(config.settings.onlyAudits.length, 1);
assert.equal(config.settings.onlyAudits[0], 'network-requests');

runStub.mockRestore();
});
});
});
17 changes: 12 additions & 5 deletions lighthouse-core/test/report/proto-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,28 @@ describe('round trip JSON comparison subsets', () => {
});

it('has the same config values', () => {
expect(roundTripJson.configSettings).toMatchObject(sampleJson.configSettings);
// Config settings from proto round trip should be a subset of the actual settings.
expect(sampleJson.configSettings).toMatchObject(roundTripJson.configSettings);
});
});

// Note: In a failing diff, if you see details.summary going from {} to null, it's OK.
// Jest considers this not a failure, and neither do we, here in the python roundtrip
// Meanwhile, The PSI roundtrip maintains {} to {}.
describe('round trip JSON comparison to everything', () => {
let sampleJson;

beforeEach(() => {
sampleJson = JSON.parse(preprocessor.processForProto(sample));

// Proto conversion turns empty summaries into null. This is OK,
// and is handled in the PSI roundtrip just fine, but messes up the easy
// jest sub-object matcher. So, we put the empty object back in its place.
for (const audit of Object.values(roundTripJson.audits)) {
if (audit.details && audit.details.summary === null) {
audit.details.summary = {};
}
}
});

it('has the same JSON overall', () => {
expect(roundTripJson).toMatchObject(sampleJson);
expect(sampleJson).toMatchObject(roundTripJson);
});
});

0 comments on commit a76e7f2

Please sign in to comment.