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

Relative requires broken in Jest tests #53

Closed
nickserv opened this issue Sep 30, 2017 · 8 comments
Closed

Relative requires broken in Jest tests #53

nickserv opened this issue Sep 30, 2017 · 8 comments

Comments

@nickserv
Copy link

Please refer to jestjs/jest#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
Copy link
Author

nickserv commented Sep 30, 2017

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
Copy link
Author

nickserv commented Oct 1, 2017

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
Copy link
Author

nickserv commented Oct 7, 2017

Fixed by jestjs/jest#4614.

@nickserv nickserv closed this as completed Oct 7, 2017
@Elliot128
Copy link

Elliot128 commented Feb 2, 2018

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

@Maxobat
Copy link

Maxobat commented Mar 5, 2018

This is still happening for me with Jest 22.4.2.

@SokratisVidros
Copy link

Same for me, Jest 22.4.2

@yocontra
Copy link
Collaborator

yocontra commented Mar 9, 2018

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 jestjs/jest#5235

@SokratisVidros
Copy link

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.

stefanbuck pushed a commit to stefanbuck/api that referenced this issue Apr 2, 2018
require-dir is not compatible with jest, but require-directory seems to work

aseemk/requireDir#53 (comment)
josephfrazier pushed a commit to OctoLinker/api that referenced this issue Apr 3, 2018
* Replace require-dir to get jest test working

require-dir is not compatible with jest, but require-directory seems to work

aseemk/requireDir#53 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants