This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encode non-ASCII characters in all snapshot tests
Summary: We're not supposed to commit non-ASCII text to www for some reason. However, some snapshots contained fancy characters. This diff updates Jest to use the `NonASCIIStringSnapshotSerializer` I added in D16570959 to be used for **all** snapshot tests. Reviewed By: inlineblock Differential Revision: D16633746 fbshipit-source-id: f3055ec873571f5bbe32e8c7d958f663bf7439c9
- Loading branch information
1 parent
1ff8c8c
commit 734bd82
Showing
5 changed files
with
78 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright 2004-present Facebook. All Rights Reserved. | ||
* | ||
* @emails oncall+ads_integration_management | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const MAX_ASCII_CHARACTER = 127; | ||
|
||
/** | ||
* Serializes strings with non-ASCII characters to their Unicode escape | ||
* sequences (eg. \u2022), to avoid hitting this lint rule: | ||
* "Source code should only include printable US-ASCII bytes" | ||
*/ | ||
const NonASCIIStringSnapshotSerializer = { | ||
test(val: mixed): boolean { | ||
if (typeof val !== 'string') { | ||
return false; | ||
} | ||
for (let i = 0; i < val.length; i++) { | ||
if (val.charCodeAt(i) > MAX_ASCII_CHARACTER) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
|
||
print: (val: string): string => { | ||
return ( | ||
'"' + | ||
val | ||
.split('') | ||
.map(char => { | ||
const code = char.charCodeAt(0); | ||
return code > MAX_ASCII_CHARACTER | ||
? '\\u' + code.toString(16).padStart(4, '0') | ||
: char; | ||
}) | ||
.join('') | ||
// Keep the same behaviour as Jest's regular string snapshot | ||
// serialization, which escapes double quotes. | ||
.replace(/"/g, '\\"') + | ||
'"' | ||
); | ||
}, | ||
}; | ||
|
||
module.exports = NonASCIIStringSnapshotSerializer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters