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),
I'm distributing a reusable piece of Jest config in a package, which provides a
moduleNameMapperobject that maps certain strings to paths in that package. I use absolute paths to make the resolution independent of the actual Jest root dir:Jest can handle this, but
eslint-import-resolver-jestdoes not expect the path to be absolute and always prependsjestConfig.importResolverProjectRoot.eslint-import-resolver-jest/src/index.js
Lines 137 to 146 in c7b3d04
I think, instead of checking whether
jestConfig.rootDiris absolute, this case could be covered by checking ifreplacedRootinstead. Would you accept a PR along these lines?Workaround
Currently, you can set
rootDirto an absolute path in the Jest config which uses the reusable snippet to make it work: