rstest icon indicating copy to clipboard operation
rstest copied to clipboard

[Feature]: Mock all files with certain extension

Open akcyp opened this issue 1 month ago • 4 comments

What problem does this feature solve?

In Jest it's common to create following configuration to ignore / mock files with certain file extensions (css, less, scss, png, etc.).

{
  "moduleNameMapper": {
    "\\.(css|less|scss)$": "<rootDir>/src/Mocks/styleMock.js" // module.exports = {}
  }
}

It seems that in the case of rstest this is not so easy to achieve without a custom plugin.

What does the proposed API look like?

Support regexpes in resolve.alias or create native "ignore" plugin that will override files content with empty export.

akcyp avatar Oct 27 '25 10:10 akcyp

This can be achieved using rspack NormalModuleReplacementPlugin.

export default defineConfig({
  tools: {
    rspack: (config, { rspack}) => {
      config.plugins.push(
       new rspack.NormalModuleReplacementPlugin(/\.(css|less|scss)/, require.resolve('./src/Mocks/styleMock.js'))
      );
    }
  }
});

9aoy avatar Nov 06 '25 03:11 9aoy

Thanks for the reply. I didn't know about this plugin, maybe it would be worth considering adding this information to the migration guide from Jest? I'm closing this thread.

akcyp avatar Nov 07 '25 12:11 akcyp

I agree that it would be helpful to include this method in the Jest migration guide or offer a more direct solution. I'll reopen this issue so we can follow up on it.

chenjiahan avatar Nov 07 '25 12:11 chenjiahan

+1. The NormalModuleReplacementPlugin is a bit difficult for users to use. Perhaps it would be simpler for rstest or rsbuild to provide a configuration option similar to resolve.moduleNameMapper.

9aoy avatar Nov 07 '25 13:11 9aoy