-
-
Notifications
You must be signed in to change notification settings - Fork 531
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
Feature: Expose esm hooks factory to public API #1439
Conversation
Exposing this API surface makes sense, but we should do it in a way that's clean and feels like it belongs in our public API surface. The changes should still be minimal. Our existing APIs are We can export this function from |
Thanks for the response!
Makes sense. I wasn't sure if you'd want to support as official API, so I thought it best to open with the path of least resistance.
Sounds good.
I assume by |
One final note — although the code is covered by existing tests, because it's now public-facing, you may want to add some level of test to validate the exported function Also, nice work with the esm support! |
This reverts commit f53ac63.
Good catch, agreed on this approach.
Yes, this PR will need tests before it can be merged. |
I've been thinking this over a bit in terms of approach. Here's where I'm at. You have a battery of tests which covers the loader. But, we need to guarantee that the coverage translates to a custom loader. I generally use jest, which allows for using I believe that the best route for this is to:
This would provide complete coverage without any redundancy in tests. With that said, I'm not seeing any precedent for mocking in the tests. I don't see So my questions are:
There are really a number of ways that it could be done. Realistically, you could even just use the compiler API to parse the Finally, there is another route which may be more in line with precedent, but it's less ideal, in my opinion. You could create a separate loader in assets, which supplies something like At any rate, I'm new to the codebase, and not very familiar with |
We package ts-node and then `npm install` it into ./tests/node_modules` so
I think you can write a pretty simple integration / e2e-style test for this.
…On Mon, Aug 23, 2021 at 1:54 PM Ron S. ***@***.***> wrote:
I've been thinking this over a bit in terms of approach. Here's where I'm
at.
You have a battery of tests which covers the loader. But, we need to
guarantee that the coverage translates to a custom loader. I generally use
jest, which allows for using each to repeat a suite of tests on a second
target, mocking, etc. In this case, it seems we're pretty limited.
I believe that the best route for this is to:
1. Create a custom loader in assets (essentially replicating ./esm.mjs)
2. Mock createEsmHooks and register for the custom and the assets
loader
3. For both – ensure that createEsmHooks is called with the created
instance and that the module exports the proper returned values
This would provide complete coverage without any redundancy in tests.
With that said, I'm not seeing any precedent for mocking in the tests. I
don't see sinon. I do see that proxyquire is installed but is not used in
that capacity.
So my questions are:
1. Would you prefer not mocking?
2. If mocking is okay, is there a particular means you'd prefer?
There are really a number of ways that it could be done.
Realistically, you could even just use the compiler API to parse the
esm.mjs source and verify that it is exporting the proper variables from
the correct createEsmHooks function, while also passing a created
instance in a single parameter.
Finally, there is another route which may be more in line with precedent,
but it's less ideal, in my opinion.
You could create a separate loader in assets, which supplies something
like { "compilerOptions": { "resolveJsonModule": true } } to the
instance, where an index file would console.log an import from a json
file. You then verify the error and regular outputs. This would verify that
it took the instance and created the hooks, but for obvious reasons, it's
not quite as solid.
At any rate, I'm new to the codebase, and not very familiar with ava, so
maybe I'm missing a better route. Will wait to hear your thoughts.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1439 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAC35OB3JEO5YFEEJYNMOELT6KDNBANCNFSM5CSKLR6A>
.
|
src/test/index.spec.ts
Outdated
@@ -1867,6 +1867,25 @@ test.suite('ts-node', (test) => { | |||
}); | |||
}); | |||
|
|||
test.suite('createEsmHooks', (test) => { | |||
if (semver.gte(process.version, '12.0.0')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the if be above the suite? Also, is the version logic sound?
Requested changes implemented and test added. I may also have come across a bug (unrelated to this PR). Given: {
"compilerOptions": {
"resolveJsonModule": true
}
} With esm enabled, I encounter this while trying to
I'm not extremely well versed in esm, so perhaps I should be using require for that? If you think it's aberrant, I'll file an issue. |
You're probably hitting this: https://nodejs.org/dist/latest-v16.x/docs/api/esm.html#esm_no_json_module_loading |
Note to self: refactor This avoids consumers needing to pass an This is a nitpicky thing, so I'm happy to do the work myself and push the changes to this branch. |
Sounds good. Maintainer edits are enabled! |
Just checking in. I have a couple of esm-related features that I'm adding in today. If you think this PR will be merged somewhat soon, I may hold off on the release to roll all in. No rush, of course, but if you have any idea on a timeline, I'd appreciate the heads up! |
I'm pretty bad at time estimation, and my paycheck-job needs some attention this weekend, but how about I plan to merge and release this tomorrow? That way if I don't respond tomorrow you'll know I ran out of time and it'll likely be delayed a while longer. |
@cspotcode No worries. I'm very much the same on my estimations! Working on that, myself. 😄 That sounds good. Thanks! |
…e: enableExperimentalEsmLoaderInterop() This avoids consumers needing to pass an @internal flag at service creation time, since we can call the method automatically within createEsmHooks.
Codecov Report
|
@nonara Thanks again for this contribution and for your patience, this is published in v10.3.0. Please let me know if you hit any bugs. https://github.com/TypeStrong/ts-node/releases/tag/v10.3.0 |
Thank you! Looking forward to implementing it. |
Hope you're doing well!
PR Scope
createEsmHooks
methodBackground
The transformer library typescript-transform-paths now supports
ts-node
without aProgram
instance viatypescript-transform-paths/register
script. This addition is relatively recent.We've just received an issue from a user trying to make it work via esm. (see: LeDDGroup/typescript-transform-paths#134)
Because a NodeJS loader hook is used, our recreated instance isn't sufficient, as the loader remains connected to the initial configuration.
Solution + Problem
We've added a loader to make it work, which calls
registerAndCreateEsmHooks
, with the proper options. (see: LeDDGroup/typescript-transform-paths#135)But, because the loader is an es module, we can't import from
ts-node/dist/esm
, because the file isn't in thets-node
exports
section ofpackage.json
.