Lately, I've been working on implementing unit tests for some of my modular ES6 code. The project structure I have looks something like this:
project
└───src
| └───js
| cumsum.js
| index.js <--- main file
└───test
tests.js <--- QUnit testing code
Inside the cumsum.js
file, you'll find the following function:
export const cumsum=x=>{
var result = x.reduce((r, a)=> {
if (r.length > 0) {
a += r[r.length - 1];
}
r.push(a);
return r;
}, []);
return result;
}
When I run a basic test using qunit
in the terminal, it works perfectly fine:
const A=[1,2,3,4,5];
const expected=[1,3,6,10,15];
QUnit.test( "cumsum", function( assert ) {
assert.deepEqual([1,3,6,10,15],expected);
});
However, the issue arises when I try to import the actual cumsum
function using ES6 import syntax:
import {cumsum} from '../src/js/cumsum';
const A=[1,2,3,4,5];
const expected=[1,3,6,10,15];
QUnit.test( "cumsum", function( assert ) {
assert.deepEqual(cumsum(A),expected);
});
When attempting this, an error message appears stating
SyntaxError: Unexpected token {
Is there a way to effectively use QUnit with ES6 modules? If not, are there alternative unit testing frameworks that support testing these modules?