ilearnio / module-alias

Register aliases of directories and custom module paths in Node
MIT License
1.76k stars 69 forks source link

Pass along options argument #54

Closed mikew closed 5 years ago

mikew commented 5 years ago

Closes https://github.com/ilearnio/module-alias/issues/53

mikew commented 5 years ago

Not sure why the test is failing for latest node. The others make sense, as it seems options.paths for require.resolve only came in nod@8.

Tests all pass here (node@10), and with this change, the madness that is TypeScript Paths + Jest + compiled + non-compiled all work, after days of frustration.

Kehrlann commented 5 years ago

Thanks for contributing ! I've been a bit busy, but I'll try to take a look pretty soon.

mfaheemakhtar commented 5 years ago

@mikew, the latest version of NodeJS (12) has some changes in module.

I think this could be an issue https://github.com/nodejs/node/pull/23683/files

https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V12.md

Kehrlann commented 5 years ago

Good catch @mfaheemakhtar !

It means that calling resolve will start to look for a node_modules. Update the folder structure like so:

Screenshot from 2019-05-05 22-38-27

And change the test to something like:

  it('should not break require.resolve', function () {
    require.resolve('require-resolve-module', {
      paths: [path.join(process.cwd(), 'test', 'src', 'resolve')]
    })
  })

It should work.


For older versions of node, I think you might have to fiddle with process.version to include / exclude the test you added.

Kehrlann commented 5 years ago

@mikew dev has been updated ; the "reset()" spec shouldn't fail on node 11 and 12 anymore.

Kehrlann commented 5 years ago

Hey @mikew ! This PR hasn't seen work in a while. Do you wish to wrap it up, or should I pick up where you left off ?

mikew commented 5 years ago

Sorry for the delay, I've been away from work.

The issue with the test was fixed in Node v12.3.0. The test could also just be changed to use a spy and ensure the arguments that shouldn't be mucked with aren't mucked with.

mikew commented 5 years ago

All green. Feel free to bring in sinon for proper spies.

ilearnio commented 5 years ago

Hi @mikew. Added a couple suggestions, please have a look when you are free. Thank you for dedicating your time! You are really helping a lot!

Kehrlann commented 5 years ago

Nice PR, I like the idea of pulling in semver ! Thanks again for your time and commitment.

Personnally I wouldn't spy on the call. I'd drop spying, and drop the try-catch in your test - instead, use the folder structure I suggested in my previous comment. The goal is to use the public API and the "normal" behavior of require to validate that your change does The Right Thing™.

If you don't pass along options args, the test will blow up. If you do pass them, test will succeed, which demonstrates that your implementation works.

@ilearnio, do you agree with this strategy ?

ilearnio commented 5 years ago

@Kehrlann Good point. Yeah, it seems like spying is not necessary, we can just check if the require.resolve does not throw error. Also would be good to test the returned value of the require.resolve

mikew commented 5 years ago

The intention was to not rely on how require.resolve with the options argument works in tests, as that functionality had a regression in 12.0.0 - 12.2.0.

Changing the folder structure isn't needed, and frankly needing node_modules to exist doesn't make much sense.

Anyways, just wanted to pass an argument. Feel free to continue the work.

Kehrlann commented 5 years ago

Phew sorry, it's been more than a month, I've gone through a space-time breach.

I've had time to look at this PR again ; it looks good to me, specifically because of the regression in node 12.0 to 12.2 included. I'm also okay not bringing in Sinon.

I've added a commit to this PR, and tested using the require.resolve behavior. The result is in #64.

@ilearnio if you have time, could you please review both and decide what you like best ? You can close the other one.

ilearnio commented 5 years ago

@Kehrlann approved your PR, looks great. Does the same as this PR but without spy module 👍

ilearnio commented 5 years ago

@mikew this it's now released in v2.2.1. Thanks!