Node.js and the underlying V8 engine useInternational Components for Unicode (ICU) to implement these featuresin native C/C++ code. The full ICU data set is provided by Node.js by default.However, due to the size of the ICU data file, severaloptions are provided for customizing the ICU data set either whenbuilding or running Node.js.
The "(not locale-aware)" designation denotes that the function carries out itsoperation just like the non-Locale version of the function, if oneexists. For example, under none mode, Date.prototype.toLocaleString()'soperation is identical to that of Date.prototype.toString().
Node.js can link against an ICU build already installed on the system. In fact,most Linux distributions already come with ICU installed, and this option wouldmake it possible to reuse the same set of data used by other components in theOS.
Functionalities that only require the ICU library itself, such asString.prototype.normalize() and the WHATWG URL parser, are fullysupported under system-icu. Features that require ICU locale data inaddition, such as Intl.DateTimeFormat may be fully or partiallysupported, depending on the completeness of the ICU data installed on thesystem.
Functionalities that only require the ICU library itself, such asString.prototype.normalize() and the WHATWG URL parser, are fullysupported under small-icu. Features that require ICU locale data in addition,such as Intl.DateTimeFormat, generally only work with the English locale:
If the small-icu option is used, one can still provide additional locale dataat runtime so that the JS methods would work for all ICU locales. Assuming thedata file is stored at /runtime/directory/with/dat/file, it can be madeavailable to ICU through either:
ICU is able to automatically find and load a variety of data formats, but thedata must be appropriate for the ICU version, and the file correctly named.The most common name for the data file is icudtX[bl].dat, where X denotesthe intended ICU version, and b or l indicates the system's endianness.Node.js would fail to load if the expected data file cannot be read from thespecified directory. The name of the data file corresponding to the currentNode.js version can be computed with:
The full-icu npm module can greatly simplify ICU data installation bydetecting the ICU version of the running node executable and downloading theappropriate data file. After installing the module through npm i full-icu,the data file will be available at ./node_modules/full-icu. This path can bethen passed either to NODE_ICU_DATA or --icu-data-dir as shown above toenable full Intl support.
This option makes the resulting binary link against ICU statically and includea full set of ICU data. A binary created this way has no further externaldependencies and supports all locales, but might be rather large. This isthe default behavior if no --with-intl flag is passed. The official binariesare also built in this mode.
You need to run npm install including the full-icu package. It's full-icu's postinstall step which downloads the appropriate bits for the currently executing node. Note that multiple files may show up in the full-icu directory, that's OK.
When using Node 13 and higher, Intl support is now out of the box. Thedefault ICU (International Components for Unicode) option for Node isfull-icu meaning all ICU's.
All you need to do is embed the set of ICU data you need:
When using Node with prior versions, the ICU default option is small-icumeaning it includes a subset of ICU data (typically only the Englishlocale).
If you do need to load a locale you have two options:
Load the ICU's at runtime:
Install the packagefull-icu and inject it toyour test environment, you can do that by setting NODE_ICU_DATA beforecalling jest: NODE_ICU_DATA=node_modules/full-icu jest. Doing that youwill give you full-icu support as shown in option 1.
When testing a translated component there can be different approaches forachieving the wanted coverage, where the aimed goal should be to allow testingthe component in a way that will simulate the user behavior as much as posible.
Node.js doesn't ship by default a full ICU installation (International Components for Unicode). Thereason behind this is that most Node.js users will make use of only a small portion of ICU functionality. Only a subset of the full ICU data set is provided by Node.js by default.
Then... the solution seems to be easy: just install it locally and Bob's your uncle? Well, it's not that straightforward.You need to set up some plumbing before running your Jest tests to ensure Node.js is using full-icu
To make this work on a project created by using create-react-app you have to follow almost the same steps; the only stepthat is slightly different is tweaking the test command on your package.json file.
Jest is great, but you have to bear in mind that it doesn't run the tests on the browser. This may lead to different behaviour patterns in certain scenarios, differences which could become critical not only for Jest based testing, but also for server side rendering based projects.
Install the package full-icu and tell node to use it by either setting the environment variable NODE_ICU_DATA=node_modules/full-icu or passing --icu-data-dir=node_modules/full-icu.You can find more detailed information in the official documentation.
c01484d022