question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I can't get the imageCompression() function to work while running jest tests in react

See original GitHub issue

When I run a function (which I’ve got working correctly in codesandbox), in my React app with some Jest tests, the code seems to stall at:

    console.log(file.type, file.size, options);
    const output = await imageCompression(file, options);

The console prints:

  console.log
    image/jpeg 1826515 {
      maxSizeMB: 1,
      maxWidthOrHeight: 1024,
      useWebWorker: false,
      onProgress: [Function: onProgress]
    }

So I think everything is as it should be. The tests keep timing out and it’s as if everything gets stuck when I run the imageCompression function. Does anyone know why, or how to fix this?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Donaldcwlcommented, Dec 3, 2021

You may way to have a look at how the test set up of this library here: https://github.com/Donaldcwl/browser-image-compression/blob/master/test/setup_jsdom.js

If you still can’t set it up, please share your CRA with me.

0reactions
jimmythecodecommented, Dec 1, 2021

Thanks for this. I’ve spent some hours trying to understand how jest works with the dom, but I think I’m actually a good few weeks of practice away before I really get it. I’ve created a new CRA app with typescript and it’s predictably giving me the same errors with the following /package.json set up:

  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "browser-image-compression": "^1.0.17",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",

If it’s a relatively simple fix, do you know what I’d need to do to make a simple test (eg the original example I gave above) work? Is it a case of needing to mock each method on the window object, or is there a quick fix?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Test a React App with Jest and React Testing Library
Jest is used as the test runner, and React Testing Library provides test helpers for structuring tests around user interactions. To begin, you ......
Read more >
Importing images breaks jest test - Stack Overflow
When you import image files, Jest tries to interpret the binary codes of the images as .js, hence runs into errors.
Read more >
Continuous integration for React applications using Jest and ...
This function will be called in place of the actual dispatch function in your tests. You can run the tests with the npm...
Read more >
A Practical Guide To Testing React Applications With Jest
I'll introduce you to Jest testing techniques, including: running tests, testing React components, snapshot testing, and mocking.
Read more >
Testing Recipes - React
This page assumes you're using Jest as a test runner. If you use a different test runner, ... On this page, we will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found