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

tests: better display value tests with i18n #5720

Merged
merged 3 commits into from
Jul 26, 2018
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
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
'!**/test/',
'!**/scripts/',
],
setupTestFrameworkScriptFile: './lighthouse-core/test/test-utils.js',
testEnvironment: 'node',
testMatch: [
'**/lighthouse-core/**/*-test.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('Byte efficiency base audit', () => {
],
}, graph, simulator);

assert.ok(result.displayValue);
expect(result.displayValue).toBeDisplayString(/savings of 2/);
});

it('should work on real graphs', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Cache headers audit', () => {
assert.equal(items.length, 1);
assert.equal(items[0].cacheLifetimeMs, 0);
assert.equal(items[0].wastedBytes, 10000);
assert.ok(result.displayValue);
expect(result.displayValue).toBeDisplayString('1 resource found');
});
});

Expand All @@ -68,7 +68,7 @@ describe('Cache headers audit', () => {
assert.equal(Math.round(items[1].wastedBytes), 8000);
assert.equal(items[2].cacheLifetimeMs, 86400 * 1000);
assert.equal(Math.round(items[2].wastedBytes), 4000);
assert.ok(result.displayValue);
expect(result.displayValue).toBeDisplayString('3 resources found');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const mockArtifacts = (mockChain) => {
describe('Performance: critical-request-chains audit', () => {
it('calculates the correct chain result for failing example', () => {
return CriticalRequestChains.audit(mockArtifacts(FAILING_REQUEST_CHAIN)).then(output => {
assert.ok(output.displayValue);
expect(output.displayValue).toBeDisplayString('2 chains found');
assert.equal(output.rawValue, false);
assert.ok(output.details);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Appcache manifest audit', () => {
AppCacheManifest: 'manifest-name',
});
assert.equal(auditResult.rawValue, false);
assert.ok(/manifest-name/.test(auditResult.displayValue));
expect(auditResult.displayValue).toBeDisplayString(/manifest-name/);
});

it('passes when <html> does not contain a manifest attribute', () => {
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/audits/dobetterweb/dom-size-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Num DOM nodes audit', () => {
const auditResult = DOMSize.audit(artifact, {options});
assert.equal(auditResult.score, 0.43);
assert.equal(auditResult.rawValue, numNodes);
assert.ok(auditResult.displayValue);
expect(auditResult.displayValue).toBeDisplayString('1,500 nodes');
assert.equal(auditResult.details.items[0].totalNodes, numNodes.toLocaleString());
assert.equal(auditResult.details.items[0].depth, '1');
assert.equal(auditResult.details.items[0].width, '2');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Performance: estimated-input-latency audit', () => {
return Audit.audit(artifacts, {options, settings}).then(output => {
assert.equal(Math.round(output.rawValue * 10) / 10, 17.1);
assert.equal(output.score, 1);
assert.ok(output.displayValue);
expect(output.displayValue).toBeDisplayString('20\xa0ms');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Performance: first-meaningful-paint audit', () => {

assert.equal(fmpResult.score, 1);
assert.equal(fmpResult.rawValue, 783.328);
assert.ok(fmpResult.displayValue);
expect(fmpResult.displayValue).toBeDisplayString('780\xa0ms');
});

it('computes FMP correctly for simulated', async () => {
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-core/test/audits/metrics/interactive-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Performance: interactive audit', () => {
return Interactive.audit(artifacts, {options, settings}).then(output => {
assert.equal(output.score, 1);
assert.equal(Math.round(output.rawValue), 1582);
assert.ok(output.displayValue);
expect(output.displayValue).toBeDisplayString('1,580\xa0ms');
});
});

Expand All @@ -52,7 +52,7 @@ describe('Performance: interactive audit', () => {
return Interactive.audit(artifacts, {options, settings}).then(output => {
assert.equal(output.score, 0.97);
assert.equal(Math.round(output.rawValue), 2712);
assert.ok(output.displayValue);
expect(output.displayValue).toBeDisplayString('2,710\xa0ms');
});
});
});
2 changes: 1 addition & 1 deletion lighthouse-core/test/audits/seo/robots-txt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ wrong

assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.details.items.length, expectedErrors);
assert.ok(auditResult.displayValue);
expect(auditResult.displayValue).toBeDisplayString(/\d errors? found/);
});
});

Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/audits/user-timing-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Performance: user-timings audit', () => {
assert.equal(blackListedUTs.length, 0, 'Blacklisted usertimings included in results');

assert.equal(auditResult.rawValue, false);
assert.ok(auditResult.displayValue);
expect(auditResult.displayValue).toBeDisplayString('2 user timings');

assert.equal(auditResult.details.items[0].name, 'measure_test');
assert.equal(auditResult.details.items[0].timingType, 'Measure');
Expand Down
32 changes: 32 additions & 0 deletions lighthouse-core/test/test-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @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';

/* eslint-env jest */

const i18n = require('../lib/i18n');

expect.extend({
toBeDisplayString(received, expected) {
const obj = {value: received};
i18n.replaceIcuMessageInstanceIds(obj, 'en-US');
const actual = obj.value;
const pass = expected instanceof RegExp ?
expected.test(actual) :
actual === expected;

const message = () =>
[
`${this.utils.matcherHint('.toBeDisplayString')}\n`,
`Expected object to be a display string matching:`,
` ${this.utils.printExpected(expected)}`,
`Received:`,
` ${this.utils.printReceived(actual)}`,
].join('\n');

return {actual, message, pass};
},
});