javascriptnode.jsjestjsjsdom

Jest30+JSDOM30+Node24 TextEncoder is not defined


I'm aware of previous related problems with node12- & jsdom & textEncoder

but now, trying to create a jest30+jsdom30 config file doing as little as:

module.exports = {
  testEnvironment: 'jsdom',
    setupFiles: [
      '<rootDir>/.jest/setup.js',
    ]
}
// .jest/setup.js
import { JSDOM } from 'jsdom'



will throw

● Test suite failed to run

    ReferenceError: TextEncoder is not defined

      1 | import { JSDOM } from 'jsdom'
      2 |

      at Object.<anonymous> (node_modules/whatwg-url/lib/encoding.js:2:21)
      at Object.<anonymous> (node_modules/whatwg-url/lib/url-state-machine.js:5:34)
      at Object.<anonymous> (node_modules/whatwg-url/lib/URL-impl.js:2:13)
      at Object.<anonymous> (node_modules/whatwg-url/lib/URL.js:499:14)
      at Object.<anonymous> (node_modules/whatwg-url/webidl2js-wrapper.js:3:13)
      at Object.<anonymous> (node_modules/whatwg-url/index.js:3:34)
      at Object.<anonymous> (node_modules/jsdom/lib/api.js:7:19)
      at Object.<anonymous> (.jest/setup.js:3:14)

On previous jest24+node16+jsdom14 If we need Text(En/De)coder in an specific test, we just imported

import { TextEncoder, TextDecoder } from 'util';

But now looks like is not disponible for the very JSDOM library, is there a way to fix this behaviour?

here is the console log: https://pastebin.com/e1Z8VNx7


Solution

  • Solved here

    Add the package (via npm or yarn) jest-fixed-jsdom Then in jest.config.js

    module.exports = {
      ...
      testEnvironment: 'jest-fixed-jsdom',
      ...
    }
    

    Reason (fullly explained here) jest-environment-jsdom pollyfills many core function that broke node compatibility

    Thanks to @EstusFlask