Defer importing ky-universal until actually needed #35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This accomplishes two things:
It prevents a dangling promise with an import in it. If the module is loaded (queuing up the import promise) and then no clients are ever used, and thus never awaited, the import can happen too late. In particular, Jest will break if the import happens after the test is complete.
It avoids a dynamic import altogether when the client isn't needed. In particular, Jest uses Node's VM API which only experimentally supports dynamic imports (and ESM modules altogether). This change means that merely loading the http-client module doesn't require enabling that experimental support.
Fixes #34
@dlongley This should be a non-breaking change. I've used it locally against my repro case and it seems to work great. Let me know if there's anything you'd like me to change (or have at it yourself if that's easier).
There's only one concern I have about it, which is that the error, when it does happen, is now harder to spot. If I leave
--experimental-vm-modules
off and try to expand something with a URL context, as in:…now it fails much more cryptically:
That's because the error now happens during
httpClient.get(url, options)
, andjsonld
interprets that as an HTTP problem. There's a chain ofcause
s on the error that will lead back to a meaningful error from Jest again, but it doesn't print when you run it, because it's not in the error'smessage
. I'm not sure what the best move to deal with that is. In any case, I'm curious what you think!