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.

How to handle `responsive-loader` with jest/CRA?

See original GitHub issue

I am using CRA with responsive-loader, and I am getting the following errors:

 FAIL  src/pages/User/test.tsx
  ● Test suite failed to run

    Cannot find module './assets/user_background.jpg?min=320&max=800&steps=3' from 'index.tsx'

    However, Jest was able to find:
        './index.tsx'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

      20 | 
    > 21 | const userBackground = require('./assets/user_background.jpg?min=320&max=800&steps=3')
         |                        ^
      22 | 

Are there any solutions to fix the test?

The only way around it I can find it edit my import into const userBackground = require('./assets/user_background.jpg') and so not having any benefits of the loader.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kopaxcommented, Dec 28, 2020

I did overcome the same issue by adding in my jest configuration:

{
  "moduleNameMapper": {
    "\\.(jpg|jpeg|png)\\?format=webp$": "<rootDir>/__mocks__/jpgPngMock.js"},
    "transformIgnorePatterns":[
      "<rootDir>/__mocks__/jpgPngMock.js"
    ]
}

and within __mocks__/jpgPngMock.js:

module.exports = {
  src: '',
  srcSet: '',
  width: 1,
  height: 1,
};
1reaction
bittttttencommented, Jun 20, 2019

I ended up applying the babel-plugin-import-remove-resource-query plugin to jest, and then mocked the file inside the test:

jest.mock('../assets/user_background.jpg', () => ({
    src: '',
    srcSet: '',
    width: 1,
    height: 1,
}))
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle responsive-loader with jest/CRA? #86 - GitHub
I am using CRA with responsive-loader, and I am getting the following errors: FAIL src/pages/User/test.tsx ○ Test suite failed to run ...
Read more >
responsive-loader - npm
A webpack loader for responsive images. Latest version: 3.1.2, last published: a month ago. Start using responsive-loader in your project by ...
Read more >
Create a responsive loader content - Stack Overflow
This gives you less control over the SVG via CSS but is supported by more browsers than the first method. Try expanding the...
Read more >
testing support for the experimental syntax 'jsx' isn't currently ...
Answers related to “testing support for the experimental syntax 'jsx' isn't currently enabled” ; 'React' must be in scope when using JSX react/react-in-jsx-scope ......
Read more >
Fallback Loader - sage - Roots Discourse
For anyone using background images in their css and having issues with file sizes, I added url-loader and responsive loader as my fallback...
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