Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Ipfs-http-client throws an error when running jest/jsdom unit tests #3238

Closed
lukaw3d opened this issue Aug 21, 2020 · 3 comments
Closed

Ipfs-http-client throws an error when running jest/jsdom unit tests #3238

lukaw3d opened this issue Aug 21, 2020 · 3 comments
Labels
need/triage Needs initial labeling and prioritization

Comments

@lukaw3d
Copy link

lukaw3d commented Aug 21, 2020

  • Version:

ipfs-http-client@46.0.0

  • Platform:
node@12.18.3
──┬ jest@26.4.1
  └─┬ @jest/core@26.4.1
    └─┬ jest-config@26.4.1
      └─┬ jest-environment-jsdom@26.3.0
        └── jsdom@16.4.0 
  • Subsystem:

Severity:

Medium

Description:

  • What you did:
    Updated a dependency and tried to run unit tests with default jest configuration. Subdependency ipfs-http-client@40.1.0 updated to ipfs-http-client@46.0.0 and was imported require('ipfs-http-client') but not instantiated in tests.

    jest --env=node works as a workaround, if you don't need DOM

  • What happened:
    Tests crash on initialization. Global XMLHttpRequest is defined (so it uses ipfs-utils/src/http/fetch-browser.js), but global Request isn't (throws ReferenceError).

  • What you expected to happen
    Expected ipfs-http-client to continue working with jest/jsdom

Steps to reproduce the error:

newfolder
npm init -y
npm i ipfs-http-client@46.0.0
npm i jest
echo "require('ipfs-http-client'); test('a', () => {});" > a.test.js

npx jest   # [--env=jsdom] 
#> ReferenceError: Request is not defined
#  ipfs-utils/src/http/fetch-browser.js:95

npx jest --env=node
#> Success
@lukaw3d lukaw3d added the need/triage Needs initial labeling and prioritization label Aug 21, 2020
@welcome
Copy link

welcome bot commented Aug 21, 2020

Thank you for submitting your first issue to this repository! A maintainer will be here shortly to triage and review.
In the meantime, please double-check that you have provided all the necessary information to make this process easy! Any information that can help save additional round trips is useful! We currently aim to give initial feedback within two business days. If this does not happen, feel free to leave a comment.
Please keep an eye on how this issue will be labeled, as labels give an overview of priorities, assignments and additional actions requested by the maintainers:

  • "Priority" labels will show how urgent this is for the team.
  • "Status" labels will show if this is ready to be worked on, blocked, or in progress.
  • "Need" labels will indicate if additional input or analysis is required.

Finally, remember to use https://discuss.ipfs.io if you just need general support.

@mitjat
Copy link

mitjat commented Aug 24, 2020

I'm also affected by this.
Stack trace when running the repro:

    ReferenceError: Request is not defined
      at Object.<anonymous> (node_modules/ipfs-utils/src/http/fetch.browser.js:95:19)
      at Object.<anonymous> (node_modules/ipfs-utils/src/http/fetch.js:6:20)
      at Object.<anonymous> (node_modules/ipfs-utils/src/http.js:4:37)
      at Object.<anonymous> (node_modules/ipfs-utils/src/files/url-source.js:3:14)
      at Object.<anonymous> (node_modules/ipfs-http-client/src/index.js:10:19)

The third line (node_modules/ipfs-utils/src/http.js:4:37) points to https://github.com/ipfs/js-ipfs-utils/blame/master/src/http.js#L4, which has recently (6 days ago) been modified to no longer use fetch.js (= the second line from the stack trace above).

So it looks likely that the problem was introduced by ipfs/js-ipfs-utils#54 and has since been reverted/fixed in ipfs-utils. So hopefully, ipfs-http-client just needs to use v3.0 of ipfs-utils: https://github.com/ipfs/js-ipfs-utils/releases

@lukaw3d
Copy link
Author

lukaw3d commented Aug 24, 2020

Ah, it works in ipfs-http-client@next (46.0.0-rc.2)

@lukaw3d lukaw3d closed this as completed Aug 24, 2020
Gozala added a commit to ipfs/js-ipfs-utils that referenced this issue Sep 30, 2020
- address jsdom use case as per
   ipfs/js-ipfs#3238
- remove onDownloadProgress since we don't need it
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
need/triage Needs initial labeling and prioritization
Projects
None yet
Development

No branches or pull requests

2 participants