Last active
October 31, 2023 15:06
-
-
Save gcangussu/af52da296aef829eba15ea626453f031 to your computer and use it in GitHub Desktop.
Custom Jest resolver to preserve symlinks. Useful for usage with Bazel. Requires `resolve` npm package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const resolve = require('resolve'); | |
/** | |
* @typedef {{ | |
basedir: string; | |
browser?: boolean; | |
defaultResolver: (request: string, options: ResolverOptions) => string; | |
extensions?: readonly string[]; | |
moduleDirectory?: readonly string[]; | |
paths?: readonly string[]; | |
rootDir?: string; | |
}} ResolverOptions | |
*/ | |
/** | |
* @param {string} request | |
* @param {ResolverOptions} options | |
*/ | |
module.exports = (request, options) => { | |
try { | |
return resolve.sync(request, { | |
basedir: options.basedir, | |
extensions: options.extensions, | |
preserveSymlinks: true, | |
}); | |
} catch (e) { | |
if (e.code === 'MODULE_NOT_FOUND') { | |
return options.defaultResolver(request, options); | |
} | |
throw e; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
resolver: '<rootDir>/jest-resolver.js', | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment