Repository: GoogleChrome/lighthouse
Pull request #5720 (2018-07-25 01:28:10)

tests: better display value tests with i18n
@brendankenny we do this post #5716 ?

patrickhulce: > This works for me for #5716, if the rest are copied over?

yep 👍 

Patch:
diff --git a/jest.config.js b/jest.config.js
index 25d519914..660866959 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -14,6 +14,7 @@ module.exports = {
     '!**/test/',
     '!**/scripts/',
   ],
+  setupTestFrameworkScriptFile: './lighthouse-core/test/test-utils.js',
   testEnvironment: 'node',
   testMatch: [
     '**/lighthouse-core/**/*-test.js',


Test patch:
diff --git a/lighthouse-core/test/audits/byte-efficiency/byte-efficiency-audit-test.js b/lighthouse-core/test/audits/byte-efficiency/byte-efficiency-audit-test.js
index b383e2d2e..e4dc83735 100644
--- a/lighthouse-core/test/audits/byte-efficiency/byte-efficiency-audit-test.js
+++ b/lighthouse-core/test/audits/byte-efficiency/byte-efficiency-audit-test.js
@@ -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 () => {
diff --git a/lighthouse-core/test/audits/byte-efficiency/uses-long-cache-ttl-test.js b/lighthouse-core/test/audits/byte-efficiency/uses-long-cache-ttl-test.js
index 845c1a10b..148fa6df2 100644
--- a/lighthouse-core/test/audits/byte-efficiency/uses-long-cache-ttl-test.js
+++ b/lighthouse-core/test/audits/byte-efficiency/uses-long-cache-ttl-test.js
@@ -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');
     });
   });
 
@@ -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');
     });
   });
 
diff --git a/lighthouse-core/test/audits/critical-request-chains-test.js b/lighthouse-core/test/audits/critical-request-chains-test.js
index 0006a803e..a7ce7ce9c 100644
--- a/lighthouse-core/test/audits/critical-request-chains-test.js
+++ b/lighthouse-core/test/audits/critical-request-chains-test.js
@@ -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);
     });
diff --git a/lighthouse-core/test/audits/dobetterweb/appcache-manifest-test.js b/lighthouse-core/test/audits/dobetterweb/appcache-manifest-test.js
index 1970c035a..653285e01 100644
--- a/lighthouse-core/test/audits/dobetterweb/appcache-manifest-test.js
+++ b/lighthouse-core/test/audits/dobetterweb/appcache-manifest-test.js
@@ -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', () => {
diff --git a/lighthouse-core/test/audits/dobetterweb/dom-size-test.js b/lighthouse-core/test/audits/dobetterweb/dom-size-test.js
index ee1ee0aed..14420dd6a 100644
--- a/lighthouse-core/test/audits/dobetterweb/dom-size-test.js
+++ b/lighthouse-core/test/audits/dobetterweb/dom-size-test.js
@@ -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');
diff --git a/lighthouse-core/test/audits/metrics/estimated-input-latency-test.js b/lighthouse-core/test/audits/metrics/estimated-input-latency-test.js
index 5b4fc9c44..f348a89f3 100644
--- a/lighthouse-core/test/audits/metrics/estimated-input-latency-test.js
+++ b/lighthouse-core/test/audits/metrics/estimated-input-latency-test.js
@@ -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');
     });
   });
 });
diff --git a/lighthouse-core/test/audits/metrics/first-meaningful-paint-test.js b/lighthouse-core/test/audits/metrics/first-meaningful-paint-test.js
index 2936b2217..e44b4cdbf 100644
--- a/lighthouse-core/test/audits/metrics/first-meaningful-paint-test.js
+++ b/lighthouse-core/test/audits/metrics/first-meaningful-paint-test.js
@@ -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 () => {
diff --git a/lighthouse-core/test/audits/metrics/interactive-test.js b/lighthouse-core/test/audits/metrics/interactive-test.js
index d1b66c356..544e1defd 100644
--- a/lighthouse-core/test/audits/metrics/interactive-test.js
+++ b/lighthouse-core/test/audits/metrics/interactive-test.js
@@ -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');
     });
   });
 
@@ -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');
     });
   });
 });
diff --git a/lighthouse-core/test/audits/seo/robots-txt-test.js b/lighthouse-core/test/audits/seo/robots-txt-test.js
index 49957c355..2061d8db7 100644
--- a/lighthouse-core/test/audits/seo/robots-txt-test.js
+++ b/lighthouse-core/test/audits/seo/robots-txt-test.js
@@ -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/);
     });
   });
 
diff --git a/lighthouse-core/test/audits/user-timing-test.js b/lighthouse-core/test/audits/user-timing-test.js
index 8cd9d8015..66e3ff66e 100644
--- a/lighthouse-core/test/audits/user-timing-test.js
+++ b/lighthouse-core/test/audits/user-timing-test.js
@@ -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');
diff --git a/lighthouse-core/test/test-utils.js b/lighthouse-core/test/test-utils.js
new file mode 100644
index 000000000..0199ac12d
--- /dev/null
+++ b/lighthouse-core/test/test-utils.js
@@ -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};
+  },
+});