Repository: GoogleChrome/lighthouse
Pull request #9384 (2019-07-16 19:28:23)

tests(i18n): no ICU value given to preprocess
<!-- Thank you for submitting a pull request! -->

**Summary**
Add test for a missing ICU replacement value when gettingFormatted.  Removed early exit from _preprocessMessageValues  and added a default so that when sending no replacement object the function will still fire.

fixes: #9383


googlebot: We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors.  If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA ([login here](https://cla.developers.google.com/) to double check)?  If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2FGoogleChrome%2Flighthouse%2Fpull%2F9384) for more info**.

<!-- need_author_cla -->
exterkamp: > We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA ([login here](https://cla.developers.google.com/) to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
> In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again. If the bot doesn't comment, it means it doesn't think anything has changed.
> 
>  **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2FGoogleChrome%2Flighthouse%2Fpull%2F9384) for more info**.

Yo, I can't add `cla: yes` whats up?

Patch:
diff --git a/lighthouse-core/lib/i18n/i18n.js b/lighthouse-core/lib/i18n/i18n.js
index d3da92e48..a6c302101 100644
--- a/lighthouse-core/lib/i18n/i18n.js
+++ b/lighthouse-core/lib/i18n/i18n.js
@@ -127,9 +127,7 @@ function lookupLocale(locale) {
  * @param {string} icuMessage
  * @param {Record<string, *>} [values]
  */
-function _preprocessMessageValues(icuMessage, values) {
-  if (!values) return;
-
+function _preprocessMessageValues(icuMessage, values = {}) {
   const clonedValues = JSON.parse(JSON.stringify(values));
   const parsed = MessageParser.parse(icuMessage);
   // Throw an error if a message's value isn't provided
@@ -137,7 +135,7 @@ function _preprocessMessageValues(icuMessage, values) {
     .filter(el => el.type === 'argumentElement')
     .forEach(el => {
       if (el.id && (el.id in values) === false) {
-        throw new Error('ICU Message contains a value reference that wasn\'t provided');
+        throw new Error(`ICU Message contains a value reference ("${el.id}") that wasn't provided`);
       }
     });
 


Test patch:
diff --git a/lighthouse-core/test/lib/i18n/i18n-test.js b/lighthouse-core/test/lib/i18n/i18n-test.js
index 185c1df5e..2782a2bb1 100644
--- a/lighthouse-core/test/lib/i18n/i18n-test.js
+++ b/lighthouse-core/test/lib/i18n/i18n-test.js
@@ -102,6 +102,7 @@ describe('i18n', () => {
       helloSecWorld: 'Hello {in, number, seconds} World',
       helloTimeInMsWorld: 'Hello {timeInMs, number, seconds} World',
       helloPercentWorld: 'Hello {in, number, extendedPercent} World',
+      helloWorldMultiReplace: '{hello} {world}',
     };
     const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
 
@@ -134,5 +135,16 @@ describe('i18n', () => {
       const helloPercentStr = str_(UIStrings.helloPercentWorld, {in: 0.43078});
       expect(helloPercentStr).toBeDisplayString('Hello 43.08% World');
     });
+
+    it('throws an error when values are needed but not provided', () => {
+      expect(_ => i18n.getFormatted(str_(UIStrings.helloBytesWorld), 'en-US'))
+      .toThrow(`ICU Message contains a value reference ("in") that wasn't provided`);
+    });
+
+    it('throws an error when a value is missing', () => {
+      expect(_ => i18n.getFormatted(str_(UIStrings.helloWorldMultiReplace,
+        {hello: 'hello'}), 'en-US'))
+      .toThrow(`ICU Message contains a value reference ("world") that wasn't provided`);
+    });
   });
 });