-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new_audit: add js-libraries audit, just listing detected js libs (#6081)
- Loading branch information
Showing
6 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @license Copyright 2017 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @fileoverview Diagnostic audit that lists all JavaScript libraries detected on the page | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Audit = require('../audit'); | ||
|
||
class JsLibrariesAudit extends Audit { | ||
/** | ||
* @return {LH.Audit.Meta} | ||
*/ | ||
static get meta() { | ||
return { | ||
id: 'js-libraries', | ||
title: 'Detected JavaScript libraries', | ||
description: 'All front-end JavaScript libraries detected on the page.', | ||
requiredArtifacts: ['JSLibraries'], | ||
}; | ||
} | ||
|
||
/** | ||
* @param {LH.Artifacts} artifacts | ||
* @return {LH.Audit.Product} | ||
*/ | ||
static audit(artifacts) { | ||
const libDetails = artifacts.JSLibraries.map(lib => ({ | ||
name: lib.name, | ||
version: lib.version, // null if not detected | ||
npm: lib.npmPkgName || null, // ~70% of libs come with this field | ||
})); | ||
|
||
const headings = [ | ||
{key: 'name', itemType: 'text', text: 'Name'}, | ||
{key: 'version', itemType: 'text', text: 'Version'}, | ||
]; | ||
const details = Audit.makeTableDetails(headings, libDetails, {}); | ||
|
||
return { | ||
rawValue: true, // Always pass for now. | ||
details, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = JsLibrariesAudit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
lighthouse-core/test/audits/dobetterweb/js-libraries-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @license Copyright 2018 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const JsLibrariesAudit = require('../../../audits/dobetterweb/js-libraries.js'); | ||
const assert = require('assert'); | ||
|
||
/* eslint-env jest */ | ||
describe('Returns detected front-end JavaScript libraries', () => { | ||
it('always passes', () => { | ||
// no libraries | ||
const auditResult1 = JsLibrariesAudit.audit({ | ||
JSLibraries: [], | ||
}); | ||
assert.equal(auditResult1.rawValue, true); | ||
|
||
// duplicates. TODO: consider failing in this case | ||
const auditResult2 = JsLibrariesAudit.audit({ | ||
JSLibraries: [ | ||
{name: 'lib1', version: '3.10.1', npmPkgName: 'lib1'}, | ||
{name: 'lib2', version: null, npmPkgName: 'lib2'}, | ||
], | ||
}); | ||
assert.equal(auditResult2.rawValue, true); | ||
|
||
// LOTS of frontend libs | ||
const auditResult3 = JsLibrariesAudit.audit({ | ||
JSLibraries: [ | ||
{name: 'React', version: null, npmPkgName: 'react'}, | ||
{name: 'Polymer', version: null, npmPkgName: 'polymer-core'}, | ||
{name: 'Preact', version: null, npmPkgName: 'preact'}, | ||
{name: 'Angular', version: null, npmPkgName: 'angular'}, | ||
{name: 'jQuery', version: null, npmPkgName: 'jquery'}, | ||
], | ||
}); | ||
assert.equal(auditResult3.rawValue, true); | ||
}); | ||
|
||
it('generates expected details', () => { | ||
const auditResult = JsLibrariesAudit.audit({ | ||
JSLibraries: [ | ||
{name: 'lib1', version: '3.10.1', npmPkgName: 'lib1'}, | ||
{name: 'lib2', version: null, npmPkgName: 'lib2'}, | ||
], | ||
}); | ||
const expected = [ | ||
{ | ||
name: 'lib1', | ||
npm: 'lib1', | ||
version: '3.10.1', | ||
}, | ||
{ | ||
name: 'lib2', | ||
npm: 'lib2', | ||
version: null, | ||
}, | ||
]; | ||
assert.equal(auditResult.rawValue, true); | ||
assert.deepStrictEqual(auditResult.details.items, expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters