Trying to use the ArcGIS JS API in a unit test in Jest results in an import error:
Environment:
OS: Windows 10
Node: v20.1.0
Setup: nx repo > library > jest unit test
This issue can be found by simply creating a new NX monorepo/project and creating a TypeScript library and trying to use any ArcGIS JS API class within a Jest spec file (unit test).
Example:
`my-test.spec.ts`
// This import will fail!
import { Point } from '@arcgis/core/geometry';
describe('my test', ()=>{
it('will fail', ()=>{
new Point({ latitude: 42, longitude: -72 });
});
});
Steps to Reproduce:
Solved! Go to Solution.
Ah okay, I clearly didn't look deep enough. Yep, that ignore pattern isn't working correctly. This isn't a problem with "@arcgis/core". The error "Cannot use import statement outside a module" is a well known JavaScript configuration issue.
I changed this in your app and it worked:
"transformIgnorePatterns": [ "node_modules/(?!(@angular|@arcgis|@esri|@stencil|@popperjs)/)" ],
It's been a while since I used Jest with Angular and no guarantee that it will work, but you may need to do something like this in your Jest config:
"transformIgnorePatterns": [
"node_modules/(?!(@angular|@arcgis|@esri|@stencil/)"
],
And, you might also need: https://www.npmjs.com/package/jest-preset-angular.
The current Jest config in the sample repo should already be handling this dynamically (unless I'm missing something):
Also, just to note; you can take Angular out of this mix and the same issue will occur. I just happened to create the library as an Angular library using the NX cli.
Ah okay, I clearly didn't look deep enough. Yep, that ignore pattern isn't working correctly. This isn't a problem with "@arcgis/core". The error "Cannot use import statement outside a module" is a well known JavaScript configuration issue.
I changed this in your app and it worked:
"transformIgnorePatterns": [ "node_modules/(?!(@angular|@arcgis|@esri|@stencil|@popperjs)/)" ],
Yep, that solved it. Thanks, @AndyGup!