You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Create a constant in an index.js file in a directory of your choice.
Create a function that imports this constant by inquiring from the directory, and that uses the constant you created.
Write a Jest test that is capable of verifying the value of the constant that your function uses (via console.log(), expect().toBe(), etc.). *
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.
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.
The text was updated successfully, but these errors were encountered:
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.
馃悰 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
Create a constant in an
index.js
file in a directory of your choice.Create a function that imports this constant by inquiring from the directory, and that uses the constant you created.
Write a Jest test that is capable of verifying the value of the constant that your function uses (via
console.log()
,expect().toBe()
, etc.). *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 runnpm 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
npm start
and see that the regular app works fine.import CONSTANT from 'constants/index'
) But this would not truly be inquiring from the directory.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.The text was updated successfully, but these errors were encountered: