Skip to content

Commit

Permalink
add node 12 vs node 13+ split in locale unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed May 20, 2021
1 parent 1ada8e8 commit 227ab05
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lighthouse-core/test/config/config-helpers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ describe('.deepCloneConfigJson', () => {
describe('.resolveSettings', () => {
it('resolves the locale', () => {
const settings = resolveSettings({locale: 'zh-CN'});
// COMPAT: Node 12 only has 'en' by default.
if (process.versions.node.startsWith('12')) {
expect(settings.locale).toEqual('en');
return;
}
expect(settings.locale).toEqual('zh');
});

Expand Down
4 changes: 4 additions & 0 deletions lighthouse-core/test/config/config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,13 +828,17 @@ describe('Config', () => {
it('uses config setting for locale if set', () => {
const locale = 'ar-XB';
const config = new Config({settings: {locale}});
// COMPAT: Node 12 only has 'en' by default.
if (process.versions.node.startsWith('12')) return;
assert.strictEqual(config.settings.locale, locale);
});

it('uses flag setting for locale if set', () => {
const settingsLocale = 'en-XA';
const flagsLocale = 'ar-XB';
const config = new Config({settings: {locale: settingsLocale}}, {locale: flagsLocale});
// COMPAT: Node 12 only has 'en' by default.
if (process.versions.node.startsWith('12')) return;
assert.strictEqual(config.settings.locale, flagsLocale);
});
});
Expand Down
20 changes: 19 additions & 1 deletion lighthouse-core/test/lib/i18n/i18n-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,34 @@ describe('i18n', () => {

it('logs a warning if locale is not available and the default is used', () => {
const logListener = jest.fn();
log.events.once('warning', logListener);
log.events.on('warning', logListener);

expect(i18n.lookupLocale(invalidLocale)).toEqual('en');

// COMPAT: Node 12 logs an extra warning that full-icu is not available.
if (process.versions.node.startsWith('12')) {
expect(logListener).toBeCalledTimes(2);
expect(logListener).toHaveBeenNthCalledWith(1, ['i18n',
expect.stringMatching(/Requested locale not available in this version of node/)]);
expect(logListener).toHaveBeenNthCalledWith(2, ['i18n',
`locale(s) '${invalidLocale}' not available. Falling back to default 'en'`]);
return;
}

expect(logListener).toBeCalledTimes(1);
expect(logListener).toBeCalledWith(['i18n',
`locale(s) '${invalidLocale}' not available. Falling back to default 'en'`]);

log.events.off('warning', logListener);
});

it('falls back to root tag prefix if specific locale not available', () => {
// COMPAT: Node 12 only has 'en' by default.
if (process.versions.node.startsWith('12')) {
expect(i18n.lookupLocale('es-JKJK')).toEqual('en');
return;
}

expect(i18n.lookupLocale('es-JKJK')).toEqual('es');
});

Expand Down
19 changes: 19 additions & 0 deletions lighthouse-core/test/lib/i18n/swap-locale-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ const lhr = require('../../results/sample_v2.json');

/* eslint-env jest */
describe('swap-locale', () => {
// COMPAT: Node 12 only has 'en' by default. Skip these tests since they're all about swapping locales.
if (process.versions.node.startsWith('12')) {
// Jest requires at least one test per suite.
it('runs even if other locales are not supported', () => {
/** @type {LH.Result} */
const lhrClone = JSON.parse(JSON.stringify(lhr));

// Even though 'pt' is requested, 'en' is all that's available.
const lhrEn = swapLocale(lhr, 'pt').lhr;
expect(lhrEn.configSettings.locale).toBe('en');

// Set locale back to full 'en-US' do do the comparison.
lhrEn.configSettings.locale = 'en-US';
expect(lhrEn).toStrictEqual(lhrClone);
});

return;
}

it('does not mutate the original lhr', () => {
/** @type {LH.Result} */
const lhrClone = JSON.parse(JSON.stringify(lhr));
Expand Down
6 changes: 6 additions & 0 deletions lighthouse-core/test/report/html/renderer/i18n-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ describe('util helpers', () => {
});

it('formats numbers based on locale', () => {
// COMPAT: Node 12 only has 'en' by default.
if (process.versions.node.startsWith('12')) return;

// Requires full-icu or Intl polyfill.
const number = 12346.858558;

Expand All @@ -117,6 +120,9 @@ describe('util helpers', () => {
});

it('uses decimal comma with en-XA test locale', () => {
// COMPAT: Node 12 only has 'en' by default.
if (process.versions.node.startsWith('12')) return;

// Requires full-icu or Intl polyfill.
const number = 12346.858558;

Expand Down

0 comments on commit 227ab05

Please sign in to comment.