
A file uploader reserves a byte buffer for captions. Its current reservation uses JavaScript string length and is too small for some emoji. Inspecting the byte array makes the growth concrete. The encoder returns UTF-8 bytes irrespective of how many UTF-16 code units the source contains.
Isolate the operation in one function
The example keeps the input to TextEncoder outside the function, so it is possible to run the same transformation in a test. The comparison uses concrete data rather than the rendered component. If the application accepts a value from JSON, run the fixture after parsing that JSON to preserve the relevant types.
function evaluate(value) {
return Array.from(new TextEncoder().encode(value));
}
const samples = ["A", "₹", "🚌"];
for (const sample of samples) console.log(evaluate(sample));
Trace the result
| Input | Return value |
|---|---|
| 'A' | [65] |
| '₹' | [226, 130, 185] |
| '🚌' | [240, 159, 154, 140] |
For 'A', the observed output is [65]. With '₹', it becomes [226, 130, 185]. These are useful regression fixtures because an integration test can send both through the same entry point. A test of only the first case can stay green after a change that affects the other.
For '🚌', the operation returns 4 top-level items. An assertion on that count would miss a change in their contents. Capture the expected sequence or compare membership explicitly if order has no meaning in the product.
What the calling feature must decide
A character counter should follow the product definition of a character. Reusing a byte count can penalize readers who write in another script.
Run one case twice through the real feature and look at both the displayed value and its stored representation. A component may coerce a result to text even when the implementation returns an ordered collection of values. Keep a failing sample small enough that it can travel with a bug report, including the browser or Node version that produced it.
A second check against a changed input
Feed the encoded bytes into the actual upload limit check. Inspecting character count or byte length before adding the JSON envelope can produce a success indicator for a request the server will later reject.
The same JavaScript function was also run with '' / 'A ' / 'A'. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| '' | [] |
| 'A ' | [65, 32] |
| 'A' | [65] |
MDN Web API reference · MDN JavaScript language guide
Sources and further reading
Cover photograph: Interior of The SCO Group offices Florham Park NJ November 2008 by Jonathan Schilling. CC BY-SA 4.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
