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(i18n): only accept IcuMessages in toBeDisplayString #12487

Merged
merged 1 commit into from
May 14, 2021
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
4 changes: 2 additions & 2 deletions lighthouse-core/test/audits/installable-manifest-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('PWA: webapp install banner audit', () => {
return InstallableManifestAudit.audit(artifacts, context).then(result => {
assert.strictEqual(result.score, 0);
const items = result.details.items;
expect(items[0].reason).toBeDisplayString(/number of arguments/);
expect(items[0].reason).toMatch(/number of arguments/);
});
});

Expand All @@ -167,7 +167,7 @@ describe('PWA: webapp install banner audit', () => {
return InstallableManifestAudit.audit(artifacts, context).then(result => {
assert.strictEqual(result.score, 0);
const items = result.details.items;
expect(items[0].reason).toBeDisplayString(/unexpected arguments/);
expect(items[0].reason).toMatch(/unexpected arguments/);
Copy link
Member Author

@brendankenny brendankenny May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two are kind of weird because we're putting them in the audit table, but really they're messages to us that the protocol has diverged from what we expect, so it seems better to leave them untranslated (maybe they should be just Sentry errors and test assertions that they don't occur in CI and at least leave out the "unexpected arguments" part for users?)

});
});

Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/audits/is-on-https-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('Security: HTTPS audit', () => {
// Unknown blocked resolution string is used as fallback.
expect(result.details.items[2]).toMatchObject({
url: 'http://localhost/image2.jpeg',
resolution: expect.toBeDisplayString('MixedContentBlockedLOL'),
resolution: 'MixedContentBlockedLOL',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is testing that it falls back to just using the protocol-provided string when it's a string that we don't recognize, so of course it's not localized.

});

expect(result.score).toBe(0);
Expand Down
6 changes: 2 additions & 4 deletions lighthouse-core/test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ describe('Module Tests', function() {
try {
await lighthouse('i-am-not-valid', {}, {});
} catch (err) {
expect(err.friendlyMessage)
.toBeDisplayString('The URL you have provided appears to be invalid.');
expect(err.friendlyMessage).toBe('The URL you have provided appears to be invalid.');
expect(err.code).toEqual('INVALID_URL');
}
});
Expand All @@ -103,8 +102,7 @@ describe('Module Tests', function() {
try {
await lighthouse('file:///a/fake/index.html', {}, {});
} catch (err) {
expect(err.friendlyMessage)
.toBeDisplayString('The URL you have provided appears to be invalid.');
expect(err.friendlyMessage).toBe('The URL you have provided appears to be invalid.');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runner localizes these internally (and there's a test for that), so they aren't LH.IcuMessages by the time index.js sees them

expect(err.code).toEqual('INVALID_URL');
}
});
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-core/test/runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ describe('Runner', () => {

// And it bubbled up to the runtimeError.
expect(lhr.runtimeError.code).toEqual(NO_FCP.code);
expect(lhr.runtimeError.message).toBeDisplayString(/did not paint any content.*\(NO_FCP\)/);
expect(lhr.runtimeError.message).toMatch(/did not paint any content.*\(NO_FCP\)/);
});

it('includes a pageLoadError runtimeError over any gatherer runtimeErrors', async () => {
Expand Down Expand Up @@ -821,7 +821,7 @@ describe('Runner', () => {

// But top-level runtimeError is the pageLoadError.
expect(lhr.runtimeError.code).toEqual(LHError.errors.PAGE_HUNG.code);
expect(lhr.runtimeError.message).toBeDisplayString(/because the page stopped responding/);
expect(lhr.runtimeError.message).toMatch(/because the page stopped responding/);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LHRs are already localized when returned from Runner.run, so can just match the strings directly

});
});

Expand Down
14 changes: 13 additions & 1 deletion lighthouse-core/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const {default: {toBeCloseTo}} = require('expect/build/matchers.js');

expect.extend({
toBeDisplayString(received, expected) {
if (!i18n.isIcuMessage(received)) {
const message = () =>
[
`${this.utils.matcherHint('.toBeDisplayString')}\n`,
`Expected object to be an ${this.utils.printExpected('LH.IcuMessage')}`,
`Received ${typeof received}`,
` ${this.utils.printReceived(received)}`,
].join('\n');

return {message, pass: false};
}

const actual = i18n.getFormatted(received, 'en-US');
const pass = expected instanceof RegExp ?
expected.test(actual) :
Expand All @@ -28,7 +40,7 @@ expect.extend({
` ${this.utils.printReceived(actual)}`,
].join('\n');

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

// Expose toBeCloseTo() so it can be used as an asymmetric matcher.
Expand Down