Skip to content

Cannot handle absolute paths in moduleNameMapper #55

Description

@tf

I'm distributing a reusable piece of Jest config in a package, which provides a moduleNameMapper object that maps certain strings to paths in that package. I use absolute paths to make the resolution independent of the actual Jest root dir:

module.export = {
 moduleNameMapper: {
    '^something$': path.resolve(__dirname, './path/in/shared/package'),
   ...
  }
}

Jest can handle this, but eslint-import-resolver-jest does not expect the path to be absolute and always prepends jestConfig.importResolverProjectRoot.

function getAbsolutePath(jestConfig: JestConfig, filepath: Path): Path {
const replacedRoot = filepath.replace(
JEST_ROOT_DIR_PREFIX,
jestConfig.rootDir
);
if (path.isAbsolute(jestConfig.rootDir)) {
return replacedRoot;
}
return path.join(jestConfig.importResolverProjectRoot, replacedRoot);
}

I think, instead of checking whether jestConfig.rootDir is absolute, this case could be covered by checking if replacedRoot instead. Would you accept a PR along these lines?

Workaround

Currently, you can set rootDir to an absolute path in the Jest config which uses the reusable snippet to make it work:

 rootDir: require('path').resolve(__dirname),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions