Hi, I’m trying to write some Jest tests on a Create React App project, but when I try to import any @kitware/vtk.js module and run my Jest tests I get the following error:
Cannot use import statement outside a module
I tried to add the node module to transformIgnorePatterns:
I am getting this error but adding type: ‘module’ to package.json does not seem to help. Should I be adding something else to the jest.config.d?
Error:
....
Details:
/host/node_modules/vtk.js/Sources/IO/XML/XMLImageDataReader/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import vtkXMLReader from 'vtk.js/Sources/IO/XML/XMLReader';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 |
> 2 | import vtkXMLImageDataReader from 'vtk.js/Sources/IO/XML/XMLImageDataReader';
| ^
Test:
import vtkXMLImageDataReader from 'vtk.js/Sources/IO/XML/XMLImageDataReader';
test('mytest', done => {
console.log('My test has been called')
done();
});
Update I got it working with the following changes
1- Using the jsdom test environment, and importing using the full path
/**
* @jest-environment jsdom
*/
import vtkXMLImageDataReader from '@kitware/vtk.js/IO/XML/XMLImageDataReader.js';
test('mytest', done => {
console.log('My test has been called')
done();
});
I could not get it to import from just @kitware/vtk.js
2- Changing .babelrc to babel.config.json.
I expect there is a better or more ellegant solution though.