Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest unit tests saying "TextDecoder is not defined" when testing any file that imports mapbox #13027

Closed
pdfabbro opened this issue Jan 2, 2024 · 3 comments

Comments

@pdfabbro
Copy link

pdfabbro commented Jan 2, 2024

any unit test for a file that has "import mapboxgl from "mapbox-gl"" at the top fails with "TextDecoder is not defined"

mapbox-gl-js version: ^3.0.1

browser: n/a

@mattdean-digicatapult
Copy link

Possibly caused by jsdom/jsdom#2524?

We hit a simlar issue and jsdom/jsdom#2524 (comment) solved it for us in a jest setup file:

import * as util from 'util'

// ref: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
// ref: https://github.com/jsdom/jsdom/issues/2524
Object.defineProperty(window, 'TextEncoder', {
  writable: true,
  value: util.TextEncoder
})
Object.defineProperty(window, 'TextDecoder', {
  writable: true,
  value: util.TextDecoder
})

@pdfabbro
Copy link
Author

Thanks Matt, I'll give that a go at some point today and reply here to let you know if it resovled my issue

@pdfabbro
Copy link
Author

@mattdean-digicatapult beautiful, that did the trick. Thanks! This can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants