title: Angular v13 Jest with nx test - SyntaxError: Cannot use import statement outside a module at Runtime.createScriptFromCode
body: <p>I tried to follow every comment with a possible solution <a href="https://github.com/nrwl/nx/issues/7844" rel="nofollow noreferrer">here</a> to the letter. I relied on an example project on github as well which works perfectly.</p>
  <p>This also started to happen to me after updating everything manually and when running the nx test command, occurrs this error.</p>
  <p>My jest.config.js inside <strong>apps/my-app:</strong></p>
  <pre class="lang-js prettyprint-override"><code>module.exports = {
      preset: &quot;../../jest.preset.js&quot;,
      coverageDirectory: &quot;../../coverage/apps/my-app&quot;,
      moduleFileExtensions: [&quot;ts&quot;, &quot;html&quot;, &quot;js&quot;, &quot;json&quot;, &quot;mjs&quot;],
      extensionsToTreatAsEsm: [&quot;.ts&quot;],
      resolver: &quot;jest-preset-angular/build/resolvers/ng-jest-resolver.js&quot;,
      setupFilesAfterEnv: [&quot;&lt;rootDir&gt;/src/test-setup.ts&quot;],
      globals: {
          &quot;ts-jest&quot;: {
              tsconfig: &quot;&lt;rootDir&gt;/tsconfig.spec.json&quot;,
              stringifyContentPathRegex: &quot;\\.(html|svg)$&quot;,
              useESM: true
          }
      },
      transform: {
          &quot;^.+\\.(ts|js|mjs|html|svg)$&quot;: &quot;jest-preset-angular&quot;,
      },
      displayName: &quot;my-app&quot;,
      snapshotSerializers: [
          &quot;jest-preset-angular/build/serializers/no-ng-attributes&quot;,
          &quot;jest-preset-angular/build/serializers/ng-snapshot&quot;,
          &quot;jest-preset-angular/build/serializers/html-comment&quot;,
      ],
      transformIgnorePatterns: [
          &quot;node_modules/(?!.*\\.mjs$)&quot;,
          &quot;node_modules/(?!lodash-es)&quot;,
          &quot;node_modules/(?!@ngrx|@ionic-native|@ionic)&quot;
      ]
  };
  </code></pre>
  <p><strong>jest.config.js</strong> in the project root:</p>
  <pre class="lang-js prettyprint-override"><code>const { getJestProjects } = require(&quot;@nrwl/jest&quot;);
  
  module.exports = { projects: getJestProjects() };
  </code></pre>
  <p><strong>jest.preset.js</strong> in the root project:</p>
  <pre class="lang-js prettyprint-override"><code>const nxPreset = require(&quot;@nrwl/jest/preset&quot;);
  module.exports = {
      ...nxPreset,
      testMatch: [&quot;**/+(*.)+(spec|test).+(ts|js)?(x)&quot;],
      transform: {
          &quot;^.+\\.(ts|js|html)$&quot;: &quot;ts-jest&quot;,
      },
      resolver: &quot;@nrwl/jest/plugins/resolver&quot;,
      moduleFileExtensions: [&quot;ts&quot;, &quot;js&quot;, &quot;html&quot;],
      coverageReporters: [&quot;html&quot;],
  };
  </code></pre>
  <p><strong>package.json:</strong></p>
  <pre class="lang-js prettyprint-override"><code>{
      &quot;name&quot;: &quot;my-app&quot;,
      &quot;version&quot;: &quot;1.1.10&quot;,
      &quot;license&quot;: &quot;MIT&quot;,
      &quot;scripts&quot;: {
        ...
      },
      &quot;private&quot;: true,
      &quot;dependencies&quot;: {
          &quot;@angular-devkit/architect&quot;: &quot;0.1301.2&quot;,
          &quot;@angular/animations&quot;: &quot;13.1.0&quot;,
          &quot;@angular/common&quot;: &quot;13.1.0&quot;,
          &quot;@angular/compiler&quot;: &quot;13.1.0&quot;,
          &quot;@angular/core&quot;: &quot;13.1.0&quot;,
          &quot;@angular/forms&quot;: &quot;13.1.0&quot;,
          &quot;@angular/platform-browser&quot;: &quot;13.1.0&quot;,
          &quot;@angular/platform-browser-dynamic&quot;: &quot;13.1.0&quot;,
          &quot;@angular/router&quot;: &quot;13.1.0&quot;,
          &quot;@angular/service-worker&quot;: &quot;13.1.0&quot;,
          &quot;@awesome-cordova-plugins/device&quot;: &quot;5.37.2&quot;,
          &quot;@ionic-enterprise/cordova&quot;: &quot;9.0.3&quot;,
          &quot;@ionic-native/app-version&quot;: &quot;5.36.0&quot;,
          &quot;@ionic-native/clipboard&quot;: &quot;5.36.0&quot;,
          &quot;@ionic-native/core&quot;: &quot;5.36.0&quot;,
          &quot;@ionic-native/fingerprint-aio&quot;: &quot;5.36.0&quot;,
          &quot;@ionic-native/in-app-browser&quot;: &quot;5.36.0&quot;,
          &quot;@ionic-native/ionic-webview&quot;: &quot;5.36.0&quot;,
          &quot;@ionic-native/secure-storage&quot;: &quot;5.36.0&quot;,
          &quot;@ionic-native/social-sharing&quot;: &quot;5.36.0&quot;,
          &quot;@ionic-native/splash-screen&quot;: &quot;5.36.0&quot;,
          &quot;@ionic-native/status-bar&quot;: &quot;5.36.0&quot;,
          &quot;@ionic-native/unique-device-id&quot;: &quot;5.36.0&quot;,
          &quot;@ionic/angular&quot;: &quot;6.0.0&quot;,
          &quot;@ionic/storage-angular&quot;: &quot;3.0.6&quot;,
          &quot;@ng-idle/core&quot;: &quot;11.1.0&quot;,
          &quot;@ng-idle/keepalive&quot;: &quot;11.0.3&quot;,
          &quot;@ngx-translate/core&quot;: &quot;14.0.0&quot;,
          &quot;@ngx-translate/http-loader&quot;: &quot;7.0.0&quot;,
          &quot;@sentry/angular&quot;: &quot;6.16.1&quot;,
          &quot;@sentry/tracing&quot;: &quot;6.16.1&quot;,
          &quot;cordova-android&quot;: &quot;8.1.0&quot;,
          &quot;cordova-plugin-device&quot;: &quot;2.0.3&quot;,
          &quot;cordova-plugin-inappbrowser&quot;: &quot;5.0.0&quot;,
          &quot;cordova-plugin-ionic-keyboard&quot;: &quot;2.2.0&quot;,
          &quot;cordova-plugin-ionic-webview&quot;: &quot;5.0.0&quot;,
          &quot;cordova-plugin-splashscreen&quot;: &quot;6.0.0&quot;,
          &quot;cordova-plugin-statusbar&quot;: &quot;3.0.0&quot;,
          &quot;cordova-plugin-whitelist&quot;: &quot;1.3.5&quot;,
          &quot;core-js&quot;: &quot;3.19.3&quot;,
          &quot;crypto-es&quot;: &quot;1.2.7&quot;,
          &quot;document-register-element&quot;: &quot;1.14.10&quot;,
          &quot;dom-to-image&quot;: &quot;2.6.0&quot;,
          &quot;lodash-es&quot;: &quot;4.17.21&quot;,
          &quot;moment&quot;: &quot;2.29.1&quot;,
          &quot;native-run&quot;: &quot;1.5.0&quot;,
          &quot;ng-circle-progress&quot;: &quot;1.6.0&quot;,
          &quot;node-forge&quot;: &quot;0.10.0&quot;,
          &quot;npm&quot;: &quot;8.3.0&quot;,
          &quot;reflect-metadata&quot;: &quot;0.1.13&quot;,
          &quot;rxjs&quot;: &quot;7.4.0&quot;,
          &quot;sass&quot;: &quot;1.45.0&quot;,
          &quot;tslib&quot;: &quot;2.3.1&quot;,
          &quot;uuid&quot;: &quot;8.3.2&quot;,
          &quot;zone.js&quot;: &quot;0.11.4&quot;
      },
      &quot;devDependencies&quot;: {
          &quot;@angular-architects/ddd&quot;: &quot;1.5.1&quot;,
          &quot;@angular-devkit/build-angular&quot;: &quot;13.1.0&quot;,
          &quot;@angular-eslint/builder&quot;: &quot;13.0.1&quot;,
          &quot;@angular-eslint/eslint-plugin&quot;: &quot;13.0.1&quot;,
          &quot;@angular-eslint/eslint-plugin-template&quot;: &quot;13.0.1&quot;,
          &quot;@angular-eslint/schematics&quot;: &quot;13.0.1&quot;,
          &quot;@angular-eslint/template-parser&quot;: &quot;13.0.1&quot;,
          &quot;@angular/cli&quot;: &quot;13.1.0&quot;,
          &quot;@angular/compiler-cli&quot;: &quot;13.1.0&quot;,
          &quot;@angular/language-service&quot;: &quot;13.1.0&quot;,
          &quot;@angularclass/hmr&quot;: &quot;3.0.0&quot;,
          &quot;@ionic/angular-toolkit&quot;: &quot;5.0.3&quot;,
          &quot;@ionic/cli&quot;: &quot;6.18.1&quot;,
          &quot;@nrwl/angular&quot;: &quot;13.3.2&quot;,
          &quot;@nrwl/cli&quot;: &quot;13.3.1&quot;,
          &quot;@nrwl/cypress&quot;: &quot;13.3.1&quot;,
          &quot;@nrwl/eslint-plugin-nx&quot;: &quot;13.3.1&quot;,
          &quot;@nrwl/jest&quot;: &quot;13.3.1&quot;,
          &quot;@nrwl/linter&quot;: &quot;13.3.1&quot;,
          &quot;@nrwl/node&quot;: &quot;13.3.1&quot;,
          &quot;@nrwl/workspace&quot;: &quot;13.3.1&quot;,
          &quot;@schematics/angular&quot;: &quot;13.1.0&quot;,
          &quot;@types/dom-to-image&quot;: &quot;2.6.4&quot;,
          &quot;@types/jest&quot;: &quot;27.0.3&quot;,
          &quot;@types/lodash-es&quot;: &quot;4.17.5&quot;,
          &quot;@types/node&quot;: &quot;16.11.12&quot;,
          &quot;@types/node-forge&quot;: &quot;0.10.10&quot;,
          &quot;@types/uuid&quot;: &quot;8.3.3&quot;,
          &quot;@typescript-eslint/eslint-plugin&quot;: &quot;5.3.1&quot;,
          &quot;@typescript-eslint/parser&quot;: &quot;5.3.1&quot;,
          &quot;codelyzer&quot;: &quot;6.0.2&quot;,
          &quot;compare-func&quot;: &quot;2.0.0&quot;,
          &quot;cordova&quot;: &quot;10.0.0&quot;,
          &quot;cordova-browser&quot;: &quot;6.0.0&quot;,
          &quot;cordova-clipboard&quot;: &quot;1.3.0&quot;,
          &quot;cordova-ios&quot;: &quot;6.2.0&quot;,
          &quot;cordova-plugin-add-swift-support&quot;: &quot;2.0.2&quot;,
          &quot;cordova-plugin-androidx&quot;: &quot;3.0.0&quot;,
          &quot;cordova-plugin-androidx-adapter&quot;: &quot;1.1.3&quot;,
          &quot;cordova-plugin-app-version&quot;: &quot;0.1.12&quot;,
          &quot;cordova-plugin-fingerprint-aio&quot;: &quot;5.0.0&quot;,
          &quot;cordova-plugin-iroot&quot;: &quot; 3.1.0&quot;,
          &quot;cordova-plugin-proguard&quot;: &quot;2.2.0&quot;,
          &quot;cordova-plugin-secure-storage-echo&quot;: &quot; 5.1.1&quot;,
          &quot;cordova-plugin-x-socialsharing&quot;: &quot;6.0.3&quot;,
          &quot;cordova-res&quot;: &quot;0.15.4&quot;,
          &quot;cordova-sqlite-storage&quot;: &quot;6.0.0&quot;,
          &quot;cordova-unique-device-id&quot;: &quot;1.3.2&quot;,
          &quot;cypress&quot;: &quot;8.7.0&quot;,
          &quot;dotenv&quot;: &quot;10.0.0&quot;,
          &quot;dtslint&quot;: &quot;4.2.1&quot;,
          &quot;es6-promise-plugin&quot;: &quot;4.2.2&quot;,
          &quot;eslint&quot;: &quot;8.4.1&quot;,
          &quot;eslint-config-prettier&quot;: &quot;8.3.0&quot;,
          &quot;eslint-plugin-cypress&quot;: &quot;2.12.1&quot;,
          &quot;eslint-plugin-prettier&quot;: &quot;4.0.0&quot;,
          &quot;fuzzy&quot;: &quot;0.1.3&quot;,
          &quot;inquirer&quot;: &quot;8.2.0&quot;,
          &quot;inquirer-autocomplete-prompt&quot;: &quot;1.4.0&quot;,
          &quot;jest&quot;: &quot;27.4.5&quot;,
          &quot;jest-preset-angular&quot;: &quot;11.0.1&quot;,
          &quot;ncu&quot;: &quot;0.2.1&quot;,
          &quot;ng-packagr&quot;: &quot;13.1.0&quot;,
          &quot;ngx-unused-css&quot;: &quot;3.0.0&quot;,
          &quot;npm-check-updates&quot;: &quot;12.0.3&quot;,
          &quot;npx&quot;: &quot;10.2.2&quot;,
          &quot;open&quot;: &quot;8.4.0&quot;,
          &quot;prettier&quot;: &quot;2.5.1&quot;,
          &quot;stylelint&quot;: &quot;14.1.0&quot;,
          &quot;stylelint-config-standard&quot;: &quot;24.0.0&quot;,
          &quot;ts-jest&quot;: &quot;27.0.5&quot;,
          &quot;ts-node&quot;: &quot;9.1.1&quot;,
          &quot;typescript&quot;: &quot;^4.4.4&quot;,
          &quot;webpack&quot;: &quot;5.30.0&quot;,
          &quot;xml2js&quot;: &quot;0.4.23&quot;
      },
      &quot;description&quot;: &quot;An ionic project&quot;,
      &quot;browser&quot;: {
          &quot;crypto&quot;: false,
          &quot;stream&quot;: false
      },
      &quot;cordova&quot;: {
          &quot;plugins&quot;: {
              &quot;cordova-plugin-ionic-keyboard&quot;: {},
              &quot;cordova-plugin-splashscreen&quot;: {},
              &quot;cordova-plugin-statusbar&quot;: {},
              &quot;cordova-plugin-whitelist&quot;: {},
              &quot;cordova-plugin-ionic-webview&quot;: {},
              &quot;cordova-plugin-inappbrowser&quot;: {},
              &quot;cordova-plugin-x-socialsharing&quot;: {
                  &quot;PHOTO_LIBRARY_ADD_USAGE_DESCRIPTION&quot;: &quot;La aplicación requiere acceso a la biblioteca de fotos para funcionar apropiadamente.&quot;,
                  &quot;PHOTO_LIBRARY_USAGE_DESCRIPTION&quot;: &quot;La aplicación requiere acceso a la biblioteca de fotos para funcionar apropiadamente.&quot;
              },
              &quot;cordova-plugin-androidx&quot;: {},
              &quot;cordova-plugin-androidx-adapter&quot;: {},
              &quot;cordova-plugin-proguard&quot;: {},
              &quot;cordova-sqlite-storage&quot;: {},
              &quot;cordova-unique-device-id&quot;: {},
              &quot;cordova-plugin-app-version&quot;: {},
              &quot;cordova-plugin-fingerprint-aio&quot;: {
                  &quot;FACEID_USAGE_DESCRIPTION&quot;: &quot; &quot;
              },
              &quot;cordova-plugin-secure-storage-echo&quot;: {},
              &quot;cordova-plugin-iroot&quot;: {},
              &quot;cordova-clipboard&quot;: {},
              &quot;cordova-plugin-add-swift-support&quot;: {},
              &quot;cordova-plugin-device&quot;: {}
          },
          &quot;platforms&quot;: [
              &quot;browser&quot;,
              &quot;ios&quot;,
              &quot;android&quot;
          ]
      }
  }
  </code></pre>
  <p>I still keep getting this error: :/</p>
  <pre class="lang-js prettyprint-override"><code>    You'll find more details and examples of these config options in the docs:
      https://jestjs.io/docs/configuration
      For information about custom transformations, see:
      https://jestjs.io/docs/code-transformation
  
      Details:
  
      C:\Repos\temp-repos\my-app\node_modules\@angular\core\fesm2015\testing.mjs:7
      import { getDebugNode, RendererFactory2, ɵstringify, ɵReflectionCapabilities, Directive, Component, Pipe, NgModule, ɵgetInjectableDef, resolveForwardRef, ɵNG_COMP_DEF, ɵRender3NgModuleRef, ApplicationInitStatus, LOCALE_ID, ɵDEFAULT_LOCALE_ID, ɵsetLocaleId, ɵRender3ComponentFactory, ɵcompileComponent, ɵNG_DIR_DEF, ɵcompileDirective, ɵNG_PIPE_DEF, ɵcompilePipe, ɵNG_MOD_DEF, ɵtransitiveScopesFor, ɵpatchComponentDefWithScope, ɵNG_INJ_DEF, ɵcompileNgModuleDefs, NgZone, Compiler, COMPILER_OPTIONS, ɵNgModuleFactory, ModuleWithComponentFactories, InjectionToken, Injector, InjectFlags, ɵresetCompiledComponents, ɵflushModuleScopingQueueAsMuchAsPossible } from '@angular/core';       
      ^^^^^^
  
      SyntaxError: Cannot use import statement outside a module
  
        at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1728:14)
  </code></pre>
  <p>Thansk for your replies.</p>