Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(fr): index test parity #13867

Merged
merged 10 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lighthouse-core/fraggle-rock/config/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ function filterConfigByExplicitFilters(config, filters) {
baseAuditIds = getAuditIdsInCategories(config.categories, onlyCategories);
} else if (onlyAudits) {
baseAuditIds = new Set();
} else if (!config.categories || !Object.keys(config.categories).length) {
baseAuditIds = new Set(config.audits?.map(audit => audit.implementation.meta.id));
}

const auditIdsToKeep = new Set(
Expand Down
3 changes: 2 additions & 1 deletion lighthouse-core/fraggle-rock/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ async function navigationGather(requestor, options) {
const artifacts = await Runner.gather(
async () => {
let {page} = options;
const normalizedRequestor = isCallback ? requestor : URL.normalizeUrl(requestor);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, is this change necessary? it isn't even used until far down.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, at least to match the legacy behavior. If the url is invalid, we should throw before attempting to connect to the page.


// For navigation mode, we shouldn't connect to a browser in audit mode,
// therefore we connect to the browser in the gatherFn callback.
Expand All @@ -340,7 +341,7 @@ async function navigationGather(requestor, options) {
const context = {
driver,
config,
requestor: isCallback ? requestor : URL.normalizeUrl(requestor),
requestor: normalizedRequestor,
options: internalOptions,
};
const {baseArtifacts} = await _setup(context);
Expand Down
28 changes: 28 additions & 0 deletions lighthouse-core/test/fraggle-rock/config/filters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,34 @@ describe('Fraggle Rock Config Filtering', () => {
});
});

it('should keep all audits if there are no categories', () => {
config = {
...config,
audits: [
...audits,
{implementation: NavigationOnlyAudit, options: {}},
],
categories: {},
};

const filtered = filters.filterConfigByExplicitFilters(config, {
onlyAudits: null,
onlyCategories: null,
skipAudits: null,
});
expect(filtered).toMatchObject({
navigations: [{id: 'firstPass'}],
artifacts: [{id: 'Snapshot'}, {id: 'Timespan'}],
audits: [
{implementation: SnapshotAudit},
{implementation: TimespanAudit},
{implementation: NavigationAudit},
{implementation: ManualAudit},
{implementation: NavigationOnlyAudit},
],
});
});

it('should preserve full-page-screenshot', async () => {
config = (await initializeConfig(undefined, {gatherMode: 'navigation'})).config;

Expand Down
107 changes: 107 additions & 0 deletions lighthouse-core/test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,113 @@ describe('Module Tests', function() {
assert.notEqual(lighthouseTraceCategories.length, 0);
});

describe('lighthouse', () => {
it('should throw an error when the first parameter is not defined', async () => {
const resultPromise = lighthouse();
await expect(resultPromise).rejects.toThrow();
});

it('should throw an error when the first parameter is an empty string', async () => {
const resultPromise = lighthouse('');
await expect(resultPromise).rejects.toThrow();
});

it('should throw an error when the first parameter is not a string', async () => {
const resultPromise = lighthouse({});
await expect(resultPromise).rejects.toThrow();
});

it('should throw an error when the second parameter is not an object', async () => {
const resultPromise = lighthouse('chrome://version', 'flags');
await expect(resultPromise).rejects.toThrow();
});

it('should throw an error when the config is invalid', async () => {
const resultPromise = lighthouse('chrome://version', {}, {});
await expect(resultPromise).rejects.toThrow();
});

it('should throw an error when the config contains incorrect audits', async () => {
const resultPromise = lighthouse('chrome://version', {}, {
passes: [{
gatherers: [
'script-elements',
],
}],
audits: [
'fluff',
],
});
await expect(resultPromise).rejects.toThrow();
});

it('should throw an error when the url is invalid', async () => {
const resultPromise = lighthouse('i-am-not-valid', {}, {});
await expect(resultPromise).rejects.toThrow('INVALID_URL');
});

it('should throw an error when the url is invalid protocol (file:///)', async () => {
const resultPromise = lighthouse('file:///a/fake/index.html', {}, {});
await expect(resultPromise).rejects.toThrow('INVALID_URL');
});

it('should return formatted LHR when given no categories', async () => {
const exampleUrl = 'https://www.reddit.com/r/nba';
const result = await lighthouse(exampleUrl, {
output: 'html',
}, {
settings: {
auditMode: TEST_DIR + '/fixtures/artifacts/perflog/',
formFactor: 'mobile',
},
artifacts: [
{id: 'MetaElements', gatherer: 'meta-elements'},
],
audits: [
'viewport',
],
});

assert.ok(/<html/.test(result.report), 'did not create html report');
assert.ok(result.artifacts.ViewportDimensions, 'did not set artifacts');
assert.ok(result.lhr.lighthouseVersion);
assert.ok(result.lhr.fetchTime);
assert.equal(result.lhr.finalUrl, exampleUrl);
assert.equal(result.lhr.requestedUrl, exampleUrl);
assert.equal(Object.values(result.lhr.categories).length, 0);
assert.ok(result.lhr.audits.viewport);
assert.strictEqual(result.lhr.audits.viewport.score, 0);
assert.ok(result.lhr.audits.viewport.explanation);
assert.ok(result.lhr.timing);
assert.ok(result.lhr.timing.entries.length > 3, 'timing entries not populated');
});

it('should specify the channel as node by default', async () => {
const exampleUrl = 'https://www.reddit.com/r/nba';
const result = await lighthouse(exampleUrl, {}, {
settings: {
auditMode: TEST_DIR + '/fixtures/artifacts/perflog/',
formFactor: 'mobile',
},
audits: [],
});
assert.equal(result.lhr.configSettings.channel, 'node');
});

it('lets consumers pass in a custom channel', async () => {
const exampleUrl = 'https://www.reddit.com/r/nba';
const result = await lighthouse(exampleUrl, {}, {
settings: {
auditMode: TEST_DIR + '/fixtures/artifacts/perflog/',
formFactor: 'mobile',
channel: 'custom',
},
audits: [],
});
assert.equal(result.lhr.configSettings.channel, 'custom');
});
});

describe('legacyNavigation', () => {
it('should throw an error when the first parameter is not defined', function() {
return legacyNavigation()
Expand Down