Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why will Jest run code that has an incorrect import statement?

This issue I am having is that I had a mistake in a javascript program; the import line did not include the .js extension.

The understandably meant that the program failed to with node as ./import does not exist (./import.js does). The error I reflected this Error [ERR_MODULE_NOT_FOUND]: Cannot find module .../export' imported from ....

However, when testing with jest, it works ‘correctly’ (as if the file extension was included). Does anyone know why this code is ‘correct’ for jest even though the file it is trying to import from does not really exist?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have setup a very small repo here that replicates this issue (with the same configs). Hopefully this provides all full context regarding my issue.

>Solution :

When you run code in JEST, you are using JEST’s import system

This is more advanced than the default Javascript import system. It can do things like "guessing" extensions.

Therefore the code likely is running correctly via JEST.

Look in jest.config.js

Does it list which extensions to try? e.g. .js, .jsx, .ts?

If so, that is your answer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading