I can't get the imageCompression() function to work while running jest tests in react
See original GitHub issueWhen 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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:
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?