jest-webpack-alias

raw JSON →
3.3.4 verified Sat Apr 25 auth: no javascript deprecated

Jest preprocessor that resolves require() statements using webpack aliases. Version 3.3.4 (latest, no stable release cadence). Allows Jest to understand webpack alias resolution, but is deprecated in favor of babel-plugin-module-resolver. Handles JavaScript and non-JavaScript files through mocks, but AST limitations prevent dynamic require() resolution. Works with webpack multi-compiler profiles. Limited to webpack 1/2/3 style configurations.

error Cannot find module 'some-module'
cause Webpack alias not resolved by Jest without this preprocessor.
fix
Set up jest-webpack-alias preprocessor as described in the README.
error TypeError: webpackAlias.process is not a function
cause Incorrect import: using ESM import syntax on a CommonJS module.
fix
Use require('jest-webpack-alias') instead of import.
deprecated Package is deprecated; consider using babel-plugin-module-resolver instead.
fix Replace with babel-plugin-module-resolver and configure Jest moduleNameMapper for aliases.
gotcha Dynamic require() with variables is not supported by the AST parser.
fix Use resolve() function: require(resolve(moduleName, __filename))
gotcha Non-JavaScript file requires (e.g., CSS) are not resolved; require('./style.css') fails.
fix Manual mock the file in __mocks__ directory, or use ignore-styles.
gotcha resolve.modulesDirectories only searches the package.json directory, not ancestors.
fix Ensure modulesDirectories is relative to package.json location.
npm install jest-webpack-alias
yarn add jest-webpack-alias
pnpm add jest-webpack-alias

Shows the typical setup for a Jest preprocessor that applies babel-jest and webpack alias resolution.

// __tests__/preprocessor.js
var babelJest = require('babel-jest');
require('babel-register');
var webpackAlias = require('jest-webpack-alias');

module.exports = {
  process: function(src, filename) {
    if (filename.indexOf('node_modules') === -1) {
      src = babelJest.process(src, filename);
      src = webpackAlias.process(src, filename);
    }
    return src;
  }
};