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

require('./') broken if parent directory has file with the same name #8910

Closed
vkarpov15 opened this issue Sep 4, 2019 · 5 comments · Fixed by #8912
Closed

require('./') broken if parent directory has file with the same name #8910

vkarpov15 opened this issue Sep 4, 2019 · 5 comments · Fixed by #8912

Comments

@vkarpov15
Copy link

🐛 Bug Report

Suppose you have a file dir/fn.js that requires the current directory using const x = require('./'), and the parent directory has a file dir.js. In Node.js 8, x will contain the exported value from dir/index.js. In Jest, x will contain the exported value from dir.js

To Reproduce

Steps to reproduce the behavior:

https://github.com/vkarpov15/jest-bug

Expected behavior

require() in Jest should behave the same as require() in vanilla Node.js, especially if using the Node test environment.

Link to repl or repo (highly encouraged)

https://github.com/vkarpov15/jest-bug

envinfo

$ npx envinfo --preset jest
npx: installed 1 in 1.063s

  System:
    OS: Linux 4.15 Ubuntu 16.04.3 LTS (Xenial Xerus)
    CPU: (4) x64 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
  Binaries:
    Node: 8.9.4 - /usr/bin/node
    npm: 5.6.0 - /usr/bin/npm
  npmPackages:
    jest: 24.9.0 => 24.9.0 

$ 

Re: Automattic/mongoose#8053

@SimenB
Copy link
Member

SimenB commented Sep 4, 2019

require should absolutely work the same way in Node. Odd we haven't run into this one before, it's a pretty glaring error...

Would you be able to provide a PR fixing this? The error is probably in https://github.com/facebook/jest/blob/master/packages/jest-resolve/src/defaultResolver.ts

@vkarpov15
Copy link
Author

I don't think I have the time to put in a PR. I already worked around this in Mongoose so nothing more to do on my end.

Why does Jest monkey patch require() anyway? Seems very dubious

@jeysal
Copy link
Contributor

jeysal commented Sep 4, 2019

It's not really monkey patched, it's a full custom implementation. Used e.g. for the mocking features.

@SimenB
Copy link
Member

SimenB commented Sep 4, 2019

I don't think I have the time to put in a PR.

That's alright, thanks for the great reproduction case!

Why does Jest monkey patch require() anyway? Seems very dubious

There's no monkey patching involved - we inject a function into the module scope just like node does. The reason we do it is for JIT transpilation and mocking/module mapping. It also allows full isolation between tests without leaking state between them. Granted, it's imperfect, but it's way better nothing

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants