Currently, I am conducting testing on a TypeScript file using Mocha. Within the file, there is a dependency that I access via the import
statement, and the function I need to test resides within the same file as shown below:
import { Foo } from 'foo-library';
// The function I am attempting to test
export const myHelperFunction = (a, b) => {
return a + b;
// Foo not utilized
};
export class BigClass {
public doStuff() {
// utilizes the Foo dependency
}
}
In addition to the main file, I have created a test file which looks like this:
import { myHelperFunction } from './my-file';
it('executes correctly', () => {
expect(myHelperFunction(2, 3)).to.equal(5);
});
During the execution of tests, Mocha encounters an issue where it tries to interpret the contents of foo-library
, resulting in an error message stating "unexpected token import," even though the imported module is not directly used within myHelperFunction
. This problem arises because the file is in ES6 format, causing Mocha/Node difficulties in parsing it accurately.
One potential solution would involve transpiling the dependency files to ES5. Alternatively, is there a way to bypass importing altogether during the testing process? Attempts to mock the imports using various libraries (such as Sinon) have proven unsuccessful so far.
If anyone has any innovative ideas or suggestions, I would greatly appreciate your input.