[Pkg-javascript-devel] Bug#1009572: Workaround to fix Jest resolver

Yadd yadd at debian.org
Sun Apr 24 06:46:55 BST 2022


Control: severity -1 important

To facilitate ES transition, we built some hybrid modules (node-find-up 
for example):
  * package.json#exports#require exposes old & new API
  * package.json#exports#import exposes new API

The reported error shows Jest resolver behavior:

 > FAIL test/cjs.test.js
 >   ● Test suite failed to run
 >
 >     TypeError: findUp.sync is not a function
 >
 >       11 |
 >       12 | module.exports.sync = cwd => {
 >     > 13 | 	const filePath = findUp.sync('package.json', {cwd});
 >          | 	                        ^
 >       14 | 	return filePath && path.dirname(filePath);
 >       15 | };
 >       16 |

Jest does not use Node.js resolver but jest-resolver instead. Here, 
jest-resolver uses find-up/index.js instead of find-up/index.cjs and 
then does not find old API functions.

Here is a workaround:
  * patch package.json to add: "jest": {"resolver":"debian/resolver.js"},
  * write the resolver:

$ cat > debian/resolver.js << EOF
module.exports = (request, options) => {
   // use defaultResolver
   return options.defaultResolver(request, {
     ...options,
     // use packageFilter to modify `package.json` before resolution
     // (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
     packageFilter: (pkg) => {
       let main = pkg.exports && pkg.exports.require ? 
pkg.exports.require : pkg.main;
       return {
         ...pkg,
         // change `main` value
         main: main
       };
     },
   });
};
EOF



More information about the Pkg-javascript-devel mailing list