Skip to content

Commit ed027d8

Browse files
authored
Merge pull request #564 from GoogleChrome/alwaysusemanifestparser
Improve the robustness of the manifest unittests
2 parents ed8e056 + 685bda0 commit ed027d8

File tree

10 files changed

+235
-119
lines changed

10 files changed

+235
-119
lines changed

lighthouse-core/audits/manifest-background-color.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
'use strict';
1919

2020
const Audit = require('./audit');
21+
const Formatter = require('../formatters/formatter');
2122

2223
class ManifestBackgroundColor extends Audit {
2324
/**
@@ -36,22 +37,24 @@ class ManifestBackgroundColor extends Audit {
3637
* @param {!Manifest=} manifest
3738
* @return {boolean}
3839
*/
39-
static hasBackgroundColorValue(manifest) {
40+
static getBackgroundColorValue(manifest) {
4041
return manifest !== undefined &&
41-
manifest.background_color !== undefined &&
42-
manifest.background_color.value !== undefined;
42+
manifest.background_color.value;
4343
}
4444

4545
/**
4646
* @param {!Artifacts} artifacts
4747
* @return {!AuditResult}
4848
*/
4949
static audit(artifacts) {
50-
const hasBackgroundColor = ManifestBackgroundColor
51-
.hasBackgroundColorValue(artifacts.Manifest.value);
50+
const bgColor = ManifestBackgroundColor.getBackgroundColorValue(artifacts.Manifest.value);
5251

5352
return ManifestBackgroundColor.generateAuditResult({
54-
rawValue: hasBackgroundColor
53+
rawValue: !!bgColor,
54+
extendedInfo: {
55+
value: bgColor,
56+
formatter: Formatter.SUPPORTED_FORMATS.NULL
57+
}
5558
});
5659
}
5760
}

lighthouse-core/test/audits/background-color.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,63 @@
1515
*/
1616
const Audit = require('../../audits/manifest-background-color.js');
1717
const assert = require('assert');
18+
const manifestSrc = JSON.stringify(require('../fixtures/manifest.json'));
19+
const manifestParser = require('../../lib/manifest-parser');
20+
const exampleManifest = manifestParser(manifestSrc);
1821

1922
/* global describe, it*/
2023

2124
// Need to disable camelcase check for dealing with background_color.
2225
/* eslint-disable camelcase */
2326
describe('Manifest: background color audit', () => {
24-
it('fails when no manifest present', () => {
27+
it('fails when no manifest artifact present', () => {
2528
return assert.equal(Audit.audit({Manifest: {
2629
value: undefined
2730
}}).rawValue, false);
2831
});
2932

30-
it('fails when no background color present', () => {
31-
return assert.equal(Audit.audit({Manifest: {
32-
value: {
33-
foo: 1
34-
}
35-
}}).rawValue, false);
33+
it('fails when an empty manifest is present', () => {
34+
const artifacts = {
35+
Manifest: manifestParser('{}')
36+
};
37+
return assert.equal(Audit.audit(artifacts).rawValue, false);
3638
});
3739

38-
it('fails when no background color value present', () => {
39-
return assert.equal(Audit.audit({Manifest: {
40-
value: {
40+
it('fails when a minimal manifest contains no background_color', () => {
41+
const artifacts = {
42+
Manifest: manifestParser(JSON.stringify({
43+
start_url: '/'
44+
}))
45+
};
46+
const output = Audit.audit(artifacts);
47+
assert.equal(output.rawValue, false);
48+
assert.equal(output.debugString, undefined);
49+
});
50+
51+
it('fails when a minimal manifest contains an invalid background_color', () => {
52+
const artifacts = {
53+
Manifest: manifestParser(JSON.stringify({
4154
background_color: 'no'
42-
}
43-
}}).rawValue, false);
55+
}))
56+
};
57+
const output = Audit.audit(artifacts);
58+
assert.equal(output.rawValue, false);
59+
assert.equal(output.debugString, undefined);
4460
});
4561

46-
it('passes when color is present', () => {
47-
return assert.equal(Audit.audit({Manifest: {
48-
value: {
49-
background_color: {value: 'black'}
50-
}
51-
}}).rawValue, true);
62+
it('succeeds when a minimal manifest contains a valid background_color', () => {
63+
const artifacts = {
64+
Manifest: manifestParser(JSON.stringify({
65+
background_color: '#FAFAFA'
66+
}))
67+
};
68+
const output = Audit.audit(artifacts);
69+
assert.equal(output.rawValue, true);
70+
assert.equal(output.extendedInfo.value, '#FAFAFA');
71+
});
72+
73+
it('succeeds when a complete manifest contains a background_color', () => {
74+
return assert.equal(Audit.audit({Manifest: exampleManifest}).rawValue, true);
5275
});
5376
});
5477
/* eslint-enable */

lighthouse-core/test/audits/cache-start-url.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const Audit = require('../../audits/cache-start-url.js');
1717
const assert = require('assert');
1818
const manifestSrc = JSON.stringify(require('../fixtures/manifest.json'));
1919
const manifestParser = require('../../lib/manifest-parser');
20-
const Manifest = manifestParser(manifestSrc);
20+
const exampleManifest = manifestParser(manifestSrc);
2121
const CacheContents = ['https://another.example.com/', 'https://example.com/'];
2222
const URL = 'https://example.com';
2323
const AltURL = 'https://example.com/?utm_source=http203';
@@ -30,50 +30,42 @@ describe('Cache: start_url audit', () => {
3030
});
3131

3232
it('fails when no cache contents given', () => {
33-
return assert.equal(Audit.audit({Manifest, URL}).rawValue, false);
33+
const artifacts = {Manifest: exampleManifest, URL};
34+
const output = Audit.audit(artifacts);
35+
assert.equal(output.rawValue, false);
36+
assert.equal(output.debugString, 'No cache or URL detected');
3437
});
3538

3639
it('fails when no URL given', () => {
37-
return assert.equal(Audit.audit({Manifest, CacheContents}).rawValue, false);
40+
const artifacts = {Manifest: exampleManifest, CacheContents};
41+
const output = Audit.audit(artifacts);
42+
assert.equal(output.rawValue, false);
43+
assert.equal(output.debugString, 'No cache or URL detected');
3844
});
3945

4046
// Need to disable camelcase check for dealing with start_url.
4147
/* eslint-disable camelcase */
42-
it('fails when a manifest artifact contains no start_url', () => {
43-
const inputs = {
44-
Manifest: { }
45-
};
46-
return assert.equal(Audit.audit(inputs).rawValue, false);
47-
});
48-
49-
it('fails when a manifest artifact contains a null start_url', () => {
50-
const inputs = {
51-
Manifest: {
52-
start_url: null
53-
}
54-
};
55-
return assert.equal(Audit.audit(inputs).rawValue, false);
56-
});
57-
5848
it('fails when a manifest contains no start_url', () => {
59-
const inputs = {
60-
Manifest: manifestParser({})
49+
const artifacts = {
50+
Manifest: manifestParser('{}')
6151
};
62-
return assert.equal(Audit.audit(inputs).rawValue, false);
52+
const output = Audit.audit(artifacts);
53+
assert.equal(output.rawValue, false);
54+
assert.equal(output.debugString, 'start_url not present in Manifest');
6355
});
6456
/* eslint-enable camelcase */
6557

6658
it('succeeds when given a manifest with a start_url, cache contents, and a URL', () => {
6759
return assert.equal(Audit.audit({
68-
Manifest,
60+
Manifest: exampleManifest,
6961
CacheContents,
7062
URL
7163
}).rawValue, true);
7264
});
7365

7466
it('handles URLs with utm params', () => {
7567
return assert.equal(Audit.audit({
76-
Manifest,
68+
Manifest: exampleManifest,
7769
CacheContents,
7870
URL: AltURL
7971
}).rawValue, true);

lighthouse-core/test/audits/display.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
* limitations under the License.
1515
*/
1616
const Audit = require('../../audits/manifest-display.js');
17+
const manifestParser = require('../../lib/manifest-parser');
1718
const assert = require('assert');
1819

20+
const manifestSrc = JSON.stringify(require('../fixtures/manifest.json'));
21+
const exampleManifest = manifestParser(manifestSrc);
22+
1923
/* global describe, it*/
2024

2125
describe('Mobile-friendly: display audit', () => {
@@ -26,28 +30,36 @@ describe('Mobile-friendly: display audit', () => {
2630
assert.equal(Audit.hasRecommendedValue(undefined), false);
2731
});
2832

29-
it('handles the case where there is no display property', () => {
30-
const output = Audit.audit({Manifest: {}});
33+
it('fails when no manifest artifact present', () => {
34+
return assert.equal(Audit.audit({Manifest: {
35+
value: undefined
36+
}}).rawValue, false);
37+
});
38+
39+
it('handles the case where there is no manifest display property', () => {
40+
const artifacts = {
41+
Manifest: manifestParser('{}')
42+
};
43+
const output = Audit.audit(artifacts);
3144

3245
assert.equal(output.score, false);
3346
assert.equal(output.displayValue, '');
3447
assert.equal(output.rawValue, false);
3548
});
3649

37-
it('audits a manifest\'s display property', () => {
38-
const expected = 'standalone';
39-
const output = Audit.audit({
40-
Manifest: {
41-
value: {
42-
display: {
43-
value: expected
44-
}
45-
}
46-
}
47-
});
48-
50+
it('succeeds when a manifest has a display property', () => {
51+
const artifacts = {
52+
Manifest: manifestParser(JSON.stringify({
53+
display: 'standalone'
54+
}))
55+
};
56+
const output = Audit.audit(artifacts);
4957
assert.equal(output.score, true);
50-
assert.equal(output.displayValue, expected);
58+
assert.equal(output.displayValue, 'standalone');
5159
assert.equal(output.rawValue, true);
5260
});
61+
62+
it('succeeds when a complete manifest contains a display property', () => {
63+
return assert.equal(Audit.audit({Manifest: exampleManifest}).rawValue, true);
64+
});
5365
});

lighthouse-core/test/audits/exists.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,37 @@
1616
const Audit = require('../../audits/manifest-exists.js');
1717
const assert = require('assert');
1818

19+
const manifestSrc = JSON.stringify(require('../fixtures/manifest.json'));
20+
const manifestParser = require('../../lib/manifest-parser');
21+
const exampleManifest = manifestParser(manifestSrc);
22+
1923
/* global describe, it*/
2024

2125
describe('Manifest: exists audit', () => {
22-
it('fails when no manifest present', () => {
26+
it('fails when no manifest artifact present', () => {
2327
return assert.equal(Audit.audit({Manifest: {
2428
value: undefined
2529
}}).rawValue, false);
2630
});
2731

28-
it('succeeds when a manifest is present', () => {
29-
return assert.equal(Audit.audit({Manifest: {
30-
value: {}
31-
}}).rawValue, true);
32+
it('succeeds with a valid minimal manifest', () => {
33+
const artifacts = {
34+
Manifest: manifestParser('{}')
35+
};
36+
const output = Audit.audit(artifacts);
37+
assert.equal(output.rawValue, true);
38+
assert.equal(output.debugString, undefined);
39+
});
40+
41+
it('succeeds with a valid minimal manifest', () => {
42+
const artifacts = {
43+
Manifest: manifestParser(JSON.stringify({
44+
name: 'Lighthouse PWA'
45+
}))
46+
};
47+
const output = Audit.audit(artifacts);
48+
assert.equal(output.rawValue, true);
49+
assert.equal(output.debugString, undefined);
3250
});
3351

3452
it('correctly passes through debug strings', () => {
@@ -41,4 +59,17 @@ describe('Manifest: exists audit', () => {
4159
}
4260
}).debugString, debugString);
4361
});
62+
63+
it('correctly passes through a JSON parsing failure', () => {
64+
const artifacts = {
65+
Manifest: manifestParser('{ \name: Definitely not valid JSON }')
66+
};
67+
const output = Audit.audit(artifacts);
68+
assert.equal(output.rawValue, false);
69+
assert.ok(output.debugString.includes('Unexpected token'), 'No JSON error message');
70+
});
71+
72+
it('succeeds with a complete manifest', () => {
73+
return assert.equal(Audit.audit({Manifest: exampleManifest}).rawValue, true);
74+
});
4475
});

lighthouse-core/test/audits/icons.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Audit144 = require('../../audits/manifest-icons-min-144.js');
1919
const Audit192 = require('../../audits/manifest-icons-min-192.js');
2020
const assert = require('assert');
2121
const manifestParser = require('../../lib/manifest-parser');
22+
const exampleManifest = JSON.stringify(require('../fixtures/manifest.json'));
2223

2324
/* global describe, it*/
2425

@@ -58,8 +59,7 @@ describe('Manifest: icons audits', () => {
5859

5960
it('succeeds when a manifest contains icons that are large enough', () => {
6061
// stub manifest contains a 192 icon
61-
const manifestSrc = JSON.stringify(require('../fixtures/manifest.json'));
62-
const Manifest = manifestParser(manifestSrc);
62+
const Manifest = manifestParser(exampleManifest);
6363
assert.equal(Audit144.audit({Manifest}).rawValue, true);
6464
assert.equal(Audit192.audit({Manifest}).rawValue, true);
6565
});

lighthouse-core/test/audits/name.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,43 @@ const Audit = require('../../audits/manifest-name.js');
1717
const assert = require('assert');
1818
const manifestSrc = JSON.stringify(require('../fixtures/manifest.json'));
1919
const manifestParser = require('../../lib/manifest-parser');
20-
const manifest = manifestParser(manifestSrc);
20+
const exampleManifest = manifestParser(manifestSrc);
2121

2222
/* global describe, it*/
2323

2424
describe('Manifest: name audit', () => {
25-
it('fails when no manifest present', () => {
25+
it('fails when no manifest artifact present', () => {
2626
return assert.equal(Audit.audit({Manifest: {
2727
value: undefined
2828
}}).rawValue, false);
2929
});
3030

3131
it('fails when an empty manifest is present', () => {
32-
return assert.equal(Audit.audit({Manifest: {}}).rawValue, false);
32+
const artifacts = {
33+
Manifest: manifestParser('{}')
34+
};
35+
return assert.equal(Audit.audit(artifacts).rawValue, false);
3336
});
3437

3538
it('fails when a manifest contains no name', () => {
36-
const inputs = {
37-
Manifest: {
38-
name: null
39-
}
39+
const artifacts = {
40+
Manifest: manifestParser(JSON.stringify({
41+
display: '/'
42+
}))
4043
};
44+
return assert.equal(Audit.audit(artifacts).rawValue, false);
45+
});
4146

42-
return assert.equal(Audit.audit(inputs).rawValue, false);
47+
it('succeeds when a minimal manifest contains a name', () => {
48+
const artifacts = {
49+
Manifest: manifestParser(JSON.stringify({
50+
name: 'Lighthouse PWA'
51+
}))
52+
};
53+
return assert.equal(Audit.audit(artifacts).rawValue, true);
4354
});
4455

45-
it('succeeds when a manifest contains a name', () => {
46-
return assert.equal(Audit.audit({Manifest: manifest}).rawValue, true);
56+
it('succeeds when a complete manifest contains a name', () => {
57+
return assert.equal(Audit.audit({Manifest: exampleManifest}).rawValue, true);
4758
});
4859
});

0 commit comments

Comments
 (0)