aseemk / requireDir

Node.js helper to require() directories.
MIT License
484 stars 59 forks source link

Relative requires broken in Jest tests #53

Closed nickserv closed 6 years ago

nickserv commented 7 years ago

Please refer to https://github.com/facebook/jest/issues/4567.

It appears that a Jest specific issue causes require-dir's module resolution to happen relatively to the test package instead of the parent package. This causes dependents of require-dir, including npm-check-updates, to break when used in Jest tests.

nickserv commented 7 years ago

It seems like prefixing require-dir's relative path arguments with __dirname fixes this, since it's forced to use the absolute path. Is there a way we could do this within require-dir in a way that still works in Jest?

nickserv commented 7 years ago

Nevermind, that doesn't fix it, Jest's environment incorrectly returns empty directory contents (which breaks this library) because Jest doesn't support module.parent, which this library relies on.

Unfortunately this means that require-dir can't be used in Jest (even by an indirect dependency) unless the issue is fixed upstream. For now I'd recommend that projects that use Jest or have dependents using Jest migrate away from this library using a static index.js file of imports and exports instead (which has performance and ES6 migration advantage anyway).

nickserv commented 6 years ago

Fixed by https://github.com/facebook/jest/pull/4614.

Elliot128 commented 6 years ago

Still seeing this in jest 22.1.4 and the latest require-dir

Maxobat commented 6 years ago

This is still happening for me with Jest 22.4.2.

SokratisVidros commented 6 years ago

Same for me, Jest 22.4.2

yocontra commented 6 years ago

Yeah, this is a jest bug. Nothing we can really do from this library - sorry! Open a ticket on the jest repo for it - this also might be related https://github.com/facebook/jest/issues/5235

SokratisVidros commented 6 years ago

Currently the problem with Jest lies in https://github.com/aseemk/requireDir/blob/master/index.js#L98. In Jest land require.extensions is an empty object {}. That way, map variable is not populated correctly. Please note that require.extensions is also deprecated in latest node.

I've tried various, hacky solutions via Jest preprocessors as stated in this related issue but they didn't work.

I am sorry but I have to switch to require-directory package that doesn't have the require.extensions dependency. I really hope the above information help into addressing the problem faster.