Skip to content
Advertisement

Why am I getting “TextEncoder is not defined” in Jest?

When testing a function that uses either the TextEncoder or the TextDecoder I get:

ReferenceError: TextEncoder is not defined
ReferenceError: TextDecoder is not defined

I am using jsdom, so why is this not working?

Advertisement

Answer

While it should be bundled with jsdom, it isn’t with jsdom 16. Therefore you can polyfill like so:

import { TextEncoder, TextDecoder } from 'util'
global.TextEncoder = TextEncoder
global.TextDecoder = TextDecoder
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement