title: Jest Unable to Parse Image Files
body: <p>I'm trying to set up the configuration and mock files for jest to parse/ignore image files in order for the tests to pass. Just about every online resource leads me to the jest docs located: <a href="https://jestjs.io/docs/webpack#handling-static-assets" rel="nofollow noreferrer">https://jestjs.io/docs/webpack#handling-static-assets</a>
  which tell you exactly how to handle the situation. However, not in my case. I've tried both options of creating mock files and using a transformer.
  My current jest.config.js:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>module.exports = {
    
    projects: [
      {
        displayName: 'Unit',
        testMatch: ["**/?(*.)+(spec|test).[tj]s?(x)"],
        setupFilesAfterEnv: ["&lt;rootDir&gt;/jest.setup.ts"],
        testPathIgnorePatterns: ["&lt;rootDir&gt;/.next/", "&lt;rootDir&gt;/node_modules/", "&lt;rootDir&gt;/cypress/"],
        moduleFileExtensions: ["js", "jsx", "ts", "tsx"],
        moduleDirectories: ["node_modules", "bower_components", "shared"],
        moduleNameMapper: {
          "^.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "&lt;rootDir&gt;/__mocks__/fileMock.js",
          '^.+\\.(css|sass|scss)$': '&lt;rootDir&gt;/__mocks__/styleMock.js'
        },
        // transform: {
        //   "\\.js$": "jest",
        //   "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "&lt;rootDir&gt;/fileTransformer.js"
        //   //'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
        // }
        
      },
      {
        displayName: 'Pacts',
        testMatch: ["**/?(*.)+(pacttest).[tj]s?(x)"],
        testPathIgnorePatterns: ["&lt;rootDir&gt;/.next/", "&lt;rootDir&gt;/node_modules/", "&lt;rootDir&gt;/cypress/"],
        watchPathIgnorePatterns: ["pact/logs/*", "pact/pacts/*"],
      }
    ],
    
  };</code></pre>
  </div>
  </div>
  </p>
  <p>my fileMock.js:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>module.exports = 'test-file-stub';</code></pre>
  </div>
  </div>
  </p>
  <p>My styleMock.js:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>module.exports = {};</code></pre>
  </div>
  </div>
  </p>
  <p>My fileTransformer.js:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>const path = require('path');
  
  module.exports = {
    process(src, filename, config, options) {
      return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
    },
  };
  
  //export default module.exports;</code></pre>
  </div>
  </div>
  
  my directory:
  <a href="https://i.stack.imgur.com/7rcgx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7rcgx.png" alt="enter image description here" /></a></p>
  <p>I've been bouncing back and forth trying different options in the configurations but they pretty much all lead me to the same two errors, one when I try to use the transformer, and another without. With the transformer commented out, I get 2 errors thrown at the fileMock.js file:</p>
  <ol>
  <li>TypeError: Invalid URL: test-file-stub</li>
  <li>Failed to parse src &quot;test-file-stub&quot; on <code>next/image</code></li>
  </ol>
  <p>Both of these are referring to the suggested string for the mock. I initially thought that maybe the string was a placeholder for code to actually handle something. But after some reading, my understanding is that it's actually just supposed to be a string there. Perhaps it's a specific string dependent on my environment? And <code>next/image</code> is where I'm importing the image component from.</p>
  <p>I'm prioritizing the mocking (please correct me if I'm wrong) because my understand is the mock tells jest to ignore the image file and proceed with the rest of the test while the transformer actually attempts to change the file type from js to jpg or png or whatever filetype the image is. However, I'm trying everything I can. When I try to the run the tests with the transformer portion uncommented I receive an error before any tests are even run stating:
  TypeError: Jest: a transformm must export something.
  (which is why there is a commented out export default statement.)</p>
  <p>This is my first time ever attempting anything like this and I think I've reached a point where I cannot think of anything else to try. If anybody has experienced anything like this please lay some knowledge on me. I'm not sure if I have the mockfiles set up incorrectly or if it's something in the configurations.</p>
  <p>Thanks.</p>