This week, I continued exploring how to test JavaScript code. I learned that it’s not as straightforward as python, as one cannot simply import code. JavaScript uses export to allow code to be imported. I’m not sure if this is a good or bad thing, but its notably different from Python. To make matters more confusing there are currently two methods for importing code: the older method CommonJS and the newer method ES Modules (ESM).
I learnt the export/import for ESM, which was working fine via node. However, enabling ESM for Jest the testing module took a long time and trial and error. The docs weren’t that friendly, couldn’t find a good solution from AI and eventually landed on a solution in stack overflow.
Then when I started to test, by creating an export for the function it broke the code to be used for the website. So, I’m in a sort of weird situation where testing the code, makes me change the way I write code. I’m not really sure how to fix this issue and will investigate next week. Could maybe do a conditional run if main, similar to Python’s if __name__ == ‘__main__’. Or, could separate module into its own file.
Also, I’m uncertain how the testing will work for JavaScript run in the browser, as there is also JavaScript run in the backend with node.
Again, I had a few more crashes of the EC2 instance during testing, really need to start developing and testing on my home computer. It sounds insane to be testing in PROD.
Learnings