For those utilizing Jest, a helpful solution is provided by Arthur Medeiros: Simply grant jest access to the necessary global variables it requires. It's worth noting that there may not be a need to import util
since TextDecoder
and TextEncoder
should already be available in the global scope within your jest configuration file; all you require is
globals: { TextDecoder, TextEncoder },
within your config file's export.
However, there might be instances where you prefer not to include these globals for every test in your suite (and don't wish to create another config file), or are working with something other than jest. In such scenarios, you can easily add this to your test file:
import { TextDecoder, TextEncoder } from 'node:util'; // (ESM style imports)
global.TextDecoder = TextDecoder;
global.TextEncoder = TextEncoder;
// Proceed with your desired actions... (for me, it involved using node-fetch)