Gozala / web-encoding

TextEncoder and TextDecoder APIs from Encoding Standard APIs in a universal package
10 stars 6 forks source link

jest --env=jsdom TypeError: TextDecoder is not a constructor #10

Closed lidel closed 3 years ago

lidel commented 4 years ago

jest with --env=jsdom fails with TypeError: TextDecoder is not a constructor

@Gozala did not dig into it, but I assume this is a bug: it is fair to expect people use js-multiaddr (which depends on web-encoding) in apps tested with jest.

That's what we do in ipfs-webui – see below.

How to reproduce

Having below in multibase/src/util.js causes jest-based tests to fail in ipfs-webui:

const { TextEncoder, TextDecoder } = require('web-encoding')
const textDecoder = new TextDecoder()     
 FAIL  src/bundles/analytics.test.js
  ● Test suite failed to run

    TypeError: TextDecoder is not a constructor

    > 1 | import multiaddr from 'multiaddr'
        | ^
      2 | // @ts-ignore
      3 | import HttpClient from 'ipfs-http-client'
      4 | // @ts-ignore

      at Object.<anonymous> (node_modules/uint8arrays/node_modules/multibase/src/util.js:6:21)
      at Object.<anonymous> (node_modules/uint8arrays/node_modules/multibase/src/base.js:4:24)
      at Object.<anonymous> (node_modules/uint8arrays/node_modules/multibase/src/constants.js:5:14)
      at Object.<anonymous> (node_modules/uint8arrays/to-string.js:3:19)
Gozala commented 4 years ago

@lidel this seems like the same issue as https://github.com/Gozala/web-encoding/issues/1#issuecomment-676811667 do you mind checking if the same workaround works in this context as well ?

dannyhchan commented 3 years ago

Getting error when using it in the latest create web app with node v15.4.0. Does anyone know how to resolve this?

textDecoder-error

System: OS: Linux 5.8 Ubuntu 20.10 (Groovy Gorilla) CPU: (4) x64 Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz Binaries: Node: 15.4.0 - ~/.nvm/versions/node/v15.4.0/bin/node Yarn: 1.17.3 - /usr/bin/yarn npm: 7.0.15 - ~/.nvm/versions/node/v15.4.0/bin/npm

ghost commented 3 years ago

Getting error when using it in the latest create web app with node v15.4.0. Does anyone know how to resolve this?

textDecoder-error

System: OS: Linux 5.8 Ubuntu 20.10 (Groovy Gorilla) CPU: (4) x64 Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz Binaries: Node: 15.4.0 - ~/.nvm/versions/node/v15.4.0/bin/node Yarn: 1.17.3 - /usr/bin/yarn npm: 7.0.15 - ~/.nvm/versions/node/v15.4.0/bin/npm

hi, have you solved it?

dannyhchan commented 3 years ago

@mandyJiai no, I was not able to solve it. It seems that the issue was caused by using the ipfs-http-client library. Instead of using that library, I switched to using ipfs-api which still worked for my use case.

Gozala commented 3 years ago

@mandyJiai @dannyhchan as described in in this comment https://github.com/Gozala/web-encoding/issues/1#issuecomment-676811667 issue is caused by jest and jsdom combination and not this library. It can be fixed by updating jest.