Recently, I was given a new project at work that involves using raw native imports/exports in code (compatible only with the latest browsers). The project is being worked on by consultants, and although I cannot make significant changes to their work, I am tasked with running tests frequently in node.js for speed and efficiency. I prefer to use jsdom when necessary to run my tests and avoid constantly switching between tabs to view results.
Due to decisions made by the consultants without our input, I am required to use mocha and chai for testing instead of my preferred Jasmine and Jest. Additionally, there is no established testing workflow set up by the consultants, leaving me to figure it out on my own.
In my quest for solutions, I came across an old question on Stack Overflow discussing unexpected token import errors with babel and mocha. While some suggestions I found were outdated or not applicable to newer versions of babel.
After much trial and error, I finally managed to install @babel/core, @babel/register, and @/babel/preset-env to proceed with setting up the testing environment.
Following this setup, I ran into issues while executing:
mocha --require @babel/register path/to/test.js
Despite having the right preset in .babelrc, the process threw an error stating "regeneratorRuntime is not defined," as it attempted to polyfill async/await unnecessarily since node already supports it.
My main requirement is simple - only convert import/export statements to module.exports and require, without the need for additional transforms or polyfills which might affect performance. Although I reluctantly added @babel/polyfill to workaround the issue, I am curious if there's a way to target only import/export transformations?