Skip to content

Problems with jest in env=node and env=jsdom #1

@Gozala

Description

@Gozala

As per multiformats/js-multibase#66 (quoting below):

Describe the bug
When running the test in jest (with either env=jsdom or env=node setting) the TextDecoder or TextEncoder is not a constructor error makes it impossible to write unit tests using this module

To Reproduce
This simple test and code fails to run in jest
code:
https://github.com/Uniswap/uniswap-interface/blob/0aabf4856e6e90041caf4233db4b7defb01574a2/src/utils/contenthashToUri.ts
test:
https://github.com/Uniswap/uniswap-interface/blob/0aabf4856e6e90041caf4233db4b7defb01574a2/src/utils/contenthashToUri.test.skip.ts

error:
TypeError: TextDecoder is not a constructor


    TypeError: TextDecoder is not a constructor

    > 1 | import CID from 'cids'
        | ^
      2 | import { getCodec, rmPrefix } from 'multicodec'
      3 | import { decode, toB58String } from 'multihashes'
      4 |

      at Object.<anonymous> (node_modules/multibase/src/util.js:6:21)
      at Object.<anonymous> (node_modules/multibase/src/base.js:4:24)
      at Object.<anonymous> (node_modules/multibase/src/constants.js:5:14)
      at Object.<anonymous> (node_modules/multibase/src/index.js:9:19)
      at Object.<anonymous> (node_modules/multihashes/src/index.js:10:19)
      at Object.<anonymous> (node_modules/cids/src/index.js:3:12)
      at Object.<anonymous> (src/utils/contenthashToUri.ts:1:1)
      at Object.<anonymous> (src/utils/contenthashToUri.test.ts:1:1)

Expected behavior
TextDecoder from the environment (specifically, global for node and window for jsdom jest setting) is successfully loaded

Screenshots
image

Desktop (please complete the following information):

  • OS: MacOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions