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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest Cannot Properly Handle Imports of Constants from Absolute Paths (Directory Inquiries Only) #9330

Closed
ITenthusiasm opened this issue Dec 19, 2019 · 2 comments

Comments

@ITenthusiasm
Copy link

ITenthusiasm commented Dec 19, 2019

馃悰 Bug Report

When Jest calls a function that imports a constant by inquiring from a directory, the constant appears as undefined throughout the test.

Steps To Reproduce

  1. Create a constant in an index.js file in a directory of your choice.

  2. Create a function that imports this constant by inquiring from the directory, and that uses the constant you created.

  3. Write a Jest test that is capable of verifying the value of the constant that your function uses (via console.log(), expect().toBe(), etc.). *

  4. You will find that the constant appears in your test as undefined rather than the expected value.

* Note: Remember to configure Jest to handle the necessary absolute imports.

For simplicity, an example repo can be found here. Simply perform an npm install and then run npm run test.

Expected Behavior

When Jest calls a function that imports a constant by inquiring from a directory, the constant should retain its true value throughout the test rather than appear as undefined.

Environment Info

npm: 6.13.4
node: 10.16.3
jest: 24.9.0
OS: Windows 10 (Running WSL: Ubuntu 18.04.1 LTS)

Additional Info

  • The test is the only important portion of the repo. If you want proof that the absolute import works outside jest, you can run npm start and see that the regular app works fine.
  • This bug is very specific, as there are scenarios where imports by inquiring from a directory are not a problem:
    • Functions that are imported by inquiring from a directory are not distorted by jest (as seen in the example repo).
    • Constants that are imported by using absolute paths (aliases) will work if the index file is explicitly mentioned. (eg. import CONSTANT from 'constants/index') But this would not truly be inquiring from the directory.
  • This issue (and the ways to circumvent the issue) occurs irrespective of named vs. default exports.

As seen above, there are roundabout solutions to this problem. But users should not be forced to append /index to their aliased imports nor to wrap their exports in functions merely to appease jest. The import functionality in node should be consistent, and this will help mitigate confusion for devs.

@jeysal
Copy link
Contributor

jeysal commented Dec 24, 2019

Thanks for the report. This duplicates #8610. For now you can use a different mapped import path to work around it.

@jeysal jeysal closed this as completed Dec 24, 2019
@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

No branches or pull requests

2 participants