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 6 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 @@ -293,6 +293,8 @@ function filterConfigByExplicitFilters(config, filters) {
baseAuditIds = getAuditIdsInCategories(config.categories, onlyCategories);
} else if (onlyAudits) {
baseAuditIds = new Set();
} else if (!Object.keys(config.categories || {}).length) {
adamraine marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -326,6 +326,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 @@ -339,7 +340,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', () => {
config = initializeConfig(undefined, {gatherMode: 'navigation'}).config;

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

describe('lighthouse', () => {
it('should throw an error when the first parameter is not defined', function() {
adamraine marked this conversation as resolved.
Show resolved Hide resolved
return lighthouse()
.then(() => {
throw new Error('Should not have resolved when first arg is not a string');
}, err => {
assert.ok(err);
});
});

it('should throw an error when the first parameter is an empty string', function() {
return lighthouse('')
.then(() => {
throw new Error('Should not have resolved when first arg is an empty string');
}, err => {
assert.ok(err);
});
});

it('should throw an error when the first parameter is not a string', function() {
return lighthouse({})
.then(() => {
throw new Error('Should not have resolved when first arg is not a string');
}, err => {
assert.ok(err);
});
});

it('should throw an error when the second parameter is not an object', function() {
return lighthouse('chrome://version', 'flags')
.then(() => {
throw new Error('Should not have resolved when second arg is not an object');
}, err => {
assert.ok(err);
});
});

it('should throw an error when the config is invalid', function() {
return lighthouse('chrome://version', {}, {})
.then(() => {
throw new Error('Should not have resolved when second arg is not an object');
}, err => {
assert.ok(err);
});
});

it('should throw an error when the config contains incorrect audits', function() {
return lighthouse('chrome://version', {}, {
passes: [{
gatherers: [
'script-elements',
],
}],
audits: [
'fluff',
],
})
.then(() => {
throw new Error('Should not have resolved');
}, err => {
assert.ok(err.message.includes('fluff'));
});
});

it('should throw an error when the url is invalid', async () => {
expect.hasAssertions();
try {
await lighthouse('i-am-not-valid', {}, {});
} catch (err) {
expect(err.friendlyMessage).toBe('The URL you have provided appears to be invalid.');
expect(err.code).toEqual('INVALID_URL');
}
});

it('should throw an error when the url is invalid protocol (file:///)', async () => {
expect.hasAssertions();
try {
await lighthouse('file:///a/fake/index.html', {}, {});
} catch (err) {
expect(err.friendlyMessage).toBe('The URL you have provided appears to be invalid.');
expect(err.code).toEqual('INVALID_URL');
}
});

it('should return formatted LHR when given no categories', function() {
const exampleUrl = 'https://www.reddit.com/r/nba';
return lighthouse(exampleUrl, {
output: 'html',
}, {
settings: {
auditMode: TEST_DIR + '/fixtures/artifacts/perflog/',
formFactor: 'mobile',
},
artifacts: [
{id: 'MetaElements', gatherer: 'meta-elements'},
],
audits: [
'viewport',
],
}).then(results => {
assert.ok(/<html/.test(results.report), 'did not create html report');
assert.ok(results.artifacts.ViewportDimensions, 'did not set artifacts');
assert.ok(results.lhr.lighthouseVersion);
assert.ok(results.lhr.fetchTime);
assert.equal(results.lhr.finalUrl, exampleUrl);
assert.equal(results.lhr.requestedUrl, exampleUrl);
assert.equal(Object.values(results.lhr.categories).length, 0);
assert.ok(results.lhr.audits.viewport);
assert.strictEqual(results.lhr.audits.viewport.score, 0);
assert.ok(results.lhr.audits.viewport.explanation);
assert.ok(results.lhr.timing);
assert.ok(results.lhr.timing.entries.length > 3, 'timing entries not populated');
});
});

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

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

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