1

I have a web-app that depends on the @arcgis/core-package;

"dependencies": {
    "@arcgis/core": "^4.23.7",
    "@types/arcgis-js-api": "^4.23.1",
    "@vertigis/arcgis-extensions": "^35.0.1",
    "@vertigis/viewer-spec": "47.6.0"
}

The package itself - from my understanding - also emits ES-modules, as it contains "type": "module":

{
  "name": "@arcgis/core",
  "version": "4.23.7",
  "homepage": "https://js.arcgis.com",
  ...
  "dependencies": {
    "@esri/arcgis-html-sanitizer": "~2.9.0",
    "@esri/calcite-colors": "~6.0.1",
    "@esri/calcite-components": "1.0.0-beta.77",
    "@popperjs/core": "~2.11.4",
    "focus-trap": "~6.7.3",
    "luxon": "~2.3.1",
    "sortablejs": "~1.14.0"
  },
  "type": "module"
}

When I run npm build everything is fine and I get a transpiled main.js-file within my build-directory.

However when I try to exexute the code using jest, I get

SyntaxError: Cannot use import statement outside a module

> 1 | import FeatureLayer from "@arcgis/core/layers/FeatureLayer";
    | ^

which I don't understand, as the package's type is set to module as shown above.

My jest.config.js is this:

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'jsdom',
  transform: {   
    "^.+\\.tsx?$": "ts-jest"    
  },
  transformIgnorePatterns: [
    "node_modules/.*"
  ]
};

I also tried with preset: 'ts-jest/presets/js-with-ts', as suggested in https://stackoverflow.com/a/71545273/2528063

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • [this seems to be a common problem](https://www.google.com/search?q=jest+cannot+use+import+statement+outside+a+module+site%3Astackoverflow.com&client=firefox-b-d&sxsrf=ALiCzsaR8RtMYCS7WGuN0CpscemWt__xqw%3A1656415184389&ei=0OO6YsioF4brgAaj4YaIBg&ved=0ahUKEwjI0Km1g9D4AhWGNcAKHaOwAWEQ4dUDCA0&uact=5&oq=jest+cannot+use+import+statement+outside+a+module+site%3Astackoverflow.com&gs_lcp=Cgdnd3Mtd2l6EAM6BwgAEEcQsAM6BwgAELADEEM6BQgAEIAEOgoIABCABBCHAhAUOgYIABAeEBY6CAgAEB4QFhAKSgQIQRgASgQIRhgAUFtY2Rdg3BloAXABeACAAZwBiAHaDJIBBDE5LjSYAQCgAQHIAQrAAQE&sclient=gws-wiz) – Quentin Jun 28 '22 at 11:20
  • @Quentin Indeed a common problem, however none of the suggested solutions worked for me. I edited my question by appending my jest.config. – MakePeaceGreatAgain Jun 28 '22 at 11:35

0 Answers0