neutrinojs / neutrino

Create and build modern JavaScript projects with zero initial configuration.
https://neutrinojs.org
Mozilla Public License 2.0
3.95k stars 214 forks source link

Jest: custom moduleNameMapper entries should have priority #1650

Closed davidje13 closed 3 years ago

davidje13 commented 3 years ago

When custom moduleNameMapper config is provided, it ends up at the bottom of the list, below automatic configuration for static file types (stylesheets / images). This causes problems when mocking out more complex interpretations of these file types (e.g. SVGR creates additional exports for SVG files).

For example:

    jest({
      moduleNameMapper: {
        '\\.svg$': '<rootDir>/mocks/svgr.js',
      },
    }),

Where svgr.js contains:

export const ReactComponent = () => null;
export default '';

The given config is ignored, because the built-in SVG rule takes priority (using file-mock).


This could be fixed by putting user-specified moduleNameMapper entries at the top of the resulting config, rather than the bottom. I think it is reasonable for anything the user specifies to override the defaults.