Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for custom module resolver #2320

Closed
yoavniran opened this issue Dec 14, 2016 · 5 comments
Closed

support for custom module resolver #2320

yoavniran opened this issue Dec 14, 2016 · 5 comments

Comments

@yoavniran
Copy link
Contributor

Hi,
Im very new to Jest (started today) and had planned to use it with a React app im working on that is using JSPM/SystemJS for module/dependency loading and resolving.

The issue is that Im using a feature of SystemJS that allows for module aliasing, meaning if I have: src/common/utils.js i can alias it as "utils" and then use it like this:

import utils from "utils";

Obviously the Jest resolving mechanism isnt aware of this and immediately stops with module not found error when it reaches this import.

I'd hoped there would be a way (API) for me to easily provide the mapping by reading the SystemJS config file at run time and pass the resolved path to the internal resolving/loading mechanism to do its job.

However, after a short dive into the way the Jest run-time works, how the Haste resolving works it seems like there is no way to do this.

Trying the custom moduleLoader feature (which as far as i can see is completely undocumented 😢 ) also looks like a no-go as the resolving happens outside of it and there is no way to interject in the way the resolver is created or used.

Other less sophisticated ways also seem impossible as the Jest config file is read from FS as a string so i cannot make it into a module with some basic logic.

The moduleNameMapper config parameter is read only as an array so again, cannot be made more dynamic.

I've followed the long correspondence at #599 which at some point discussed allowing something like Im looking for but at the current version (17.0.3) this doesnt seem to be enabled.

Am I missing something?

For now I see only two ways to go about this which I dont like very much:

  1. simply copy the aliases from my SystemJS config to the moduleNameMapper config parameter - dont like this at all as it means maintaining them in two places

  2. write a build step to generate the Jest config with the moduleNameMapper parameter based on the SystemJS config - seems like the best solution for now but feels less than ideal...

Thanks.

@cpojer
Copy link
Member

cpojer commented Dec 14, 2016

I would recommend option 2; you can specify a JSON value to the --config argument that you can generate from any place you like. Generally, these kind of things make static analysis harder and it's non trivial to support them – so when you run jest --watch, Jest will be unable to figure out which tests it needs to run based on changed files because Jest cannot walk the dependency graph properly. Between moduleDirectories and moduleNameMapper I believe we expose a bunch of hooks to influence the resolution code but if you have a concrete suggestion that you could turn into a PR to customize module resolution, please let me know.

@yoavniran
Copy link
Contributor Author

yoavniran commented Dec 14, 2016

thanks @cpojer

I was hoping to create something that can go to the community (in the shape of npm package) and option 2 is mostly about solving my own problem rather than providing a stable solution that relies on public APIs.

as both moduleDirectories and moduleNameMapper are very static in nature they dont provide the kind of API for a run-time package.
Coupled with the fact that Jest and Jest-Runtime seem to be using a lot of "static" methods internally, its harder to interject custom logic currently.

As I mentioned, this is my first day with Jest so im woefully under-educated about how it works but from what Ive gleaned so far, making moduleNameMapper a callback (so optionally require-ing it rather than always treating it as an object) that can return multiple results could be a useful and relatively simple way to implement a solution.

what do you think?

@cpojer
Copy link
Member

cpojer commented Dec 14, 2016

I'd prefer not to make a breaking change to moduleNameMapper at this point and I'm with people having to maintain two separate configs. We have discussed building a .jestrc system that will be plain JavaScript and export a (serializable) config. This way you can build up the config the way you want and it should solve your problem as well – is this something you'd like to help out? I'll close this in favor of #2203 which I just retitled around .jestrc.

@yoavniran
Copy link
Contributor Author

In case people get here when looking for a solution to running JSPM and Jest together,
I've written a Gulp plugin accomplish this: gulp-jest-jspm.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants