nx: JEST failure while upgrading angular v13: SyntaxError: Unexpected token 'export'

Most of my JEST unit test cases are failing with the recent upgrade.

Please see below error message and advise,


 ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    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:

    node_modules/d3-selection/src/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export {default as create} from "./create.js";
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import { Selection, select } from 'd3-selection';
        | ^

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 28
  • Comments: 56 (16 by maintainers)

Most upvoted comments

I got a similar error, after upgrading to latest version;

Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    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:

    .../node_modules/@angular/core/fesm2015/testing.mjs:7
    import * as i0 from '@angular/core';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1728:14)

transformIgnorePatterns: ['node_modules/(?!.*.mjs$)'], is not working for me though

it looks my global (jest.preset.js) transformIgnorePatterns got skipped with the following new addition to jest.config.js

transformIgnorePatterns: ['node_modules/(?!.*.mjs$)'],

@GeorgeKnap i had the same issue but i managed to fix it by making the transformIgnorePatterns an array containing a single string, rather an array of multiple strings

Thanks for the tip @joewIST ! There seems to be a bug with transformIgnorePatterns containing multiple entries (or am I misunderstanding the rule ?)

I changed:

transformIgnorePatterns: [
    'node_modules/(?!.*\\.mjs$)',
    'node_modules/(?!@angular|@ngneat/spectator|array-move|lodash-es)',
],

with:

transformIgnorePatterns: [
    `node_modules/(?!@angular|@ngneat/spectator|array-move|lodash-es)`,
],

and the error was gone !

A note on the migration:

It’s unfortunate you’re having issues with the Jest setup. Unfortunately, the Angular 13 release broke compatibility with several tools (Jest among them) and this has caused a lot of trouble.

As Victor explained in https://github.com/nrwl/nx/issues/7613#issue-1044939980, automatic migrations are hard, not every workspace is the same or evolves in the same way and to account for every possible variation is just impossible. We try our best to provide the best migration experience possible, but there can definitely be scenarios we might miss or simply deviate from what we generate or expect to be generated.

For anyone coming to this particular issue, I described what the migration does in this comment https://github.com/nrwl/nx/issues/7844#issuecomment-977806286. I haven’t been able to reproduce the issue you’re having, so a minimal repo where the issue can be reproduced would help us to troubleshoot it.

If you’re facing the issue, you need to make sure that your projects jest.config.js have these two properties with the following values:

module.exports = {
  ...
  transform: { '^.+.(ts|mjs|js|html)$': 'jest-preset-angular' },
  transformIgnorePatterns: ['node_modules/(?!.*.mjs$)'],
  ...
}

The gist of the change is that now Jest needs to process the .mjs files.

@GeorgeKnap i had the same issue but i managed to fix it by making the transformIgnorePatterns an array containing a single string, rather an array of multiple strings

In my case, not only the .mjs files were generating an error. Hence, this in jest.config.ts file fixed the problem:

// Modules throwing error "Jest encountered an unexpected token"
const esModules = ['@angular', 'tslib', 'rxjs'];

export default {
  ...
  transformIgnorePatterns: [
    `<rootDir>/node_modules/(?!.*\\.mjs$|${esModules.join('|')})`,
  ],
};

@sergiocarneiro Another problem here is that you are changing the resolver that is being used:

    resolver: "jest-preset-angular/build/resolvers/ng-jest-resolver.js"

The jest-preset-angular resolver does not work in Nx Workspaces because it does not transform the TSConfig Path Aliases for libraries.

You need to remove that property and use the jest.preset.js that Nx generates for you at the workspace root, which is:

const nxPreset = require('@nrwl/jest/preset');

module.exports = { ...nxPreset };

Then your standard jest.config.js that lives within your app or library directory should look similar to this:

module.exports = {
  displayName: 'dashboard',
  preset: '../../jest.preset.js',
  setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.(html|svg)$',
    },
  },
  coverageDirectory: '../../coverage/apps/dashboard',
  transform: {
    '^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
  },
  transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-comment',
  ],
};

Confirmed! Error is gone when used just one line 🥳

Is there any solution to this?

Hi guys!

I tried to follow every comment with a possible solution here to the letter. I relied on an example project on github as well which works perfectly.

This also started to happen to me after updating everything manually.

My jest.config.js inside apps/my-app:

module.exports = {
    preset: "../../jest.preset.js",
    coverageDirectory: "../../coverage/apps/my-app",
    moduleFileExtensions: ["ts", "html", "js", "json", "mjs"],
    extensionsToTreatAsEsm: [".ts"],
    resolver: "jest-preset-angular/build/resolvers/ng-jest-resolver.js",
    setupFilesAfterEnv: ["<rootDir>/src/test-setup.ts"],
    globals: {
        "ts-jest": {
            tsconfig: "<rootDir>/tsconfig.spec.json",
            stringifyContentPathRegex: "\\.(html|svg)$",
            useESM: true
        }
    },
    transform: {
        "^.+\\.(ts|js|mjs|html|svg)$": "jest-preset-angular",
    },
    displayName: "my-app",
    snapshotSerializers: [
        "jest-preset-angular/build/serializers/no-ng-attributes",
        "jest-preset-angular/build/serializers/ng-snapshot",
        "jest-preset-angular/build/serializers/html-comment",
    ],
    transformIgnorePatterns: [
        "node_modules/(?!.*\\.mjs$)",
        "node_modules/(?!lodash-es)",
        "node_modules/(?!@ngrx|@ionic-native|@ionic)"
    ]
};

jest.config.js in the project root:

const { getJestProjects } = require("@nrwl/jest");

module.exports = { projects: getJestProjects() };

jest.preset.js in the root project:

const nxPreset = require("@nrwl/jest/preset");
module.exports = {
    ...nxPreset,
    testMatch: ["**/+(*.)+(spec|test).+(ts|js)?(x)"],
    transform: {
        "^.+\\.(ts|js|html)$": "ts-jest",
    },
    resolver: "@nrwl/jest/plugins/resolver",
    moduleFileExtensions: ["ts", "js", "html"],
    coverageReporters: ["html"],
};

package.json:

{
    "name": "my-app",
    "version": "1.1.10",
    "license": "MIT",
    "scripts": {
      ...
    },
    "private": true,
    "dependencies": {
        "@angular-devkit/architect": "0.1301.2",
        "@angular/animations": "13.1.0",
        "@angular/common": "13.1.0",
        "@angular/compiler": "13.1.0",
        "@angular/core": "13.1.0",
        "@angular/forms": "13.1.0",
        "@angular/platform-browser": "13.1.0",
        "@angular/platform-browser-dynamic": "13.1.0",
        "@angular/router": "13.1.0",
        "@angular/service-worker": "13.1.0",
        "@awesome-cordova-plugins/device": "5.37.2",
        "@ionic-enterprise/cordova": "9.0.3",
        "@ionic-native/app-version": "5.36.0",
        "@ionic-native/clipboard": "5.36.0",
        "@ionic-native/core": "5.36.0",
        "@ionic-native/fingerprint-aio": "5.36.0",
        "@ionic-native/in-app-browser": "5.36.0",
        "@ionic-native/ionic-webview": "5.36.0",
        "@ionic-native/secure-storage": "5.36.0",
        "@ionic-native/social-sharing": "5.36.0",
        "@ionic-native/splash-screen": "5.36.0",
        "@ionic-native/status-bar": "5.36.0",
        "@ionic-native/unique-device-id": "5.36.0",
        "@ionic/angular": "6.0.0",
        "@ionic/storage-angular": "3.0.6",
        "@ng-idle/core": "11.1.0",
        "@ng-idle/keepalive": "11.0.3",
        "@ngx-translate/core": "14.0.0",
        "@ngx-translate/http-loader": "7.0.0",
        "@sentry/angular": "6.16.1",
        "@sentry/tracing": "6.16.1",
        "cordova-android": "8.1.0",
        "cordova-plugin-device": "2.0.3",
        "cordova-plugin-inappbrowser": "5.0.0",
        "cordova-plugin-ionic-keyboard": "2.2.0",
        "cordova-plugin-ionic-webview": "5.0.0",
        "cordova-plugin-splashscreen": "6.0.0",
        "cordova-plugin-statusbar": "3.0.0",
        "cordova-plugin-whitelist": "1.3.5",
        "core-js": "3.19.3",
        "crypto-es": "1.2.7",
        "document-register-element": "1.14.10",
        "dom-to-image": "2.6.0",
        "lodash-es": "4.17.21",
        "moment": "2.29.1",
        "native-run": "1.5.0",
        "ng-circle-progress": "1.6.0",
        "node-forge": "0.10.0",
        "npm": "8.3.0",
        "reflect-metadata": "0.1.13",
        "rxjs": "7.4.0",
        "sass": "1.45.0",
        "tslib": "2.3.1",
        "uuid": "8.3.2",
        "zone.js": "0.11.4"
    },
    "devDependencies": {
        "@angular-architects/ddd": "1.5.1",
        "@angular-devkit/build-angular": "13.1.0",
        "@angular-eslint/builder": "13.0.1",
        "@angular-eslint/eslint-plugin": "13.0.1",
        "@angular-eslint/eslint-plugin-template": "13.0.1",
        "@angular-eslint/schematics": "13.0.1",
        "@angular-eslint/template-parser": "13.0.1",
        "@angular/cli": "13.1.0",
        "@angular/compiler-cli": "13.1.0",
        "@angular/language-service": "13.1.0",
        "@angularclass/hmr": "3.0.0",
        "@ionic/angular-toolkit": "5.0.3",
        "@ionic/cli": "6.18.1",
        "@nrwl/angular": "13.3.2",
        "@nrwl/cli": "13.3.1",
        "@nrwl/cypress": "13.3.1",
        "@nrwl/eslint-plugin-nx": "13.3.1",
        "@nrwl/jest": "13.3.1",
        "@nrwl/linter": "13.3.1",
        "@nrwl/node": "13.3.1",
        "@nrwl/workspace": "13.3.1",
        "@schematics/angular": "13.1.0",
        "@types/dom-to-image": "2.6.4",
        "@types/jest": "27.0.3",
        "@types/lodash-es": "4.17.5",
        "@types/node": "16.11.12",
        "@types/node-forge": "0.10.10",
        "@types/uuid": "8.3.3",
        "@typescript-eslint/eslint-plugin": "5.3.1",
        "@typescript-eslint/parser": "5.3.1",
        "codelyzer": "6.0.2",
        "compare-func": "2.0.0",
        "cordova": "10.0.0",
        "cordova-browser": "6.0.0",
        "cordova-clipboard": "1.3.0",
        "cordova-ios": "6.2.0",
        "cordova-plugin-add-swift-support": "2.0.2",
        "cordova-plugin-androidx": "3.0.0",
        "cordova-plugin-androidx-adapter": "1.1.3",
        "cordova-plugin-app-version": "0.1.12",
        "cordova-plugin-fingerprint-aio": "5.0.0",
        "cordova-plugin-iroot": " 3.1.0",
        "cordova-plugin-proguard": "2.2.0",
        "cordova-plugin-secure-storage-echo": " 5.1.1",
        "cordova-plugin-x-socialsharing": "6.0.3",
        "cordova-res": "0.15.4",
        "cordova-sqlite-storage": "6.0.0",
        "cordova-unique-device-id": "1.3.2",
        "cypress": "8.7.0",
        "dotenv": "10.0.0",
        "dtslint": "4.2.1",
        "es6-promise-plugin": "4.2.2",
        "eslint": "8.4.1",
        "eslint-config-prettier": "8.3.0",
        "eslint-plugin-cypress": "2.12.1",
        "eslint-plugin-prettier": "4.0.0",
        "fuzzy": "0.1.3",
        "inquirer": "8.2.0",
        "inquirer-autocomplete-prompt": "1.4.0",
        "jest": "27.4.5",
        "jest-preset-angular": "11.0.1",
        "ncu": "0.2.1",
        "ng-packagr": "13.1.0",
        "ngx-unused-css": "3.0.0",
        "npm-check-updates": "12.0.3",
        "npx": "10.2.2",
        "open": "8.4.0",
        "prettier": "2.5.1",
        "stylelint": "14.1.0",
        "stylelint-config-standard": "24.0.0",
        "ts-jest": "27.0.5",
        "ts-node": "9.1.1",
        "typescript": "^4.4.4",
        "webpack": "5.30.0",
        "xml2js": "0.4.23"
    },
    "description": "An ionic project",
    "browser": {
        "crypto": false,
        "stream": false
    },
    "cordova": {
        "plugins": {
            "cordova-plugin-ionic-keyboard": {},
            "cordova-plugin-splashscreen": {},
            "cordova-plugin-statusbar": {},
            "cordova-plugin-whitelist": {},
            "cordova-plugin-ionic-webview": {},
            "cordova-plugin-inappbrowser": {},
            "cordova-plugin-x-socialsharing": {
                "PHOTO_LIBRARY_ADD_USAGE_DESCRIPTION": "La aplicación requiere acceso a la biblioteca de fotos para funcionar apropiadamente.",
                "PHOTO_LIBRARY_USAGE_DESCRIPTION": "La aplicación requiere acceso a la biblioteca de fotos para funcionar apropiadamente."
            },
            "cordova-plugin-androidx": {},
            "cordova-plugin-androidx-adapter": {},
            "cordova-plugin-proguard": {},
            "cordova-sqlite-storage": {},
            "cordova-unique-device-id": {},
            "cordova-plugin-app-version": {},
            "cordova-plugin-fingerprint-aio": {
                "FACEID_USAGE_DESCRIPTION": " "
            },
            "cordova-plugin-secure-storage-echo": {},
            "cordova-plugin-iroot": {},
            "cordova-clipboard": {},
            "cordova-plugin-add-swift-support": {},
            "cordova-plugin-device": {}
        },
        "platforms": [
            "browser",
            "ios",
            "android"
        ]
    }
}

I still keep getting this error: 😕

    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)

Thansk for your replies.

@dlq84 I added that exact configuration you shared to a project jest.config.js in a v12.9.0 workspace, ran the migrations and got the expected result:

diff --git a/apps/client/jest.config.js b/apps/client/jest.config.js
index 4040c4f..6497f93 100644
--- a/apps/client/jest.config.js
+++ b/apps/client/jest.config.js
@@ -18,5 +18,8 @@ module.exports = {
     'jest-preset-angular/build/serializers/ng-snapshot',
     'jest-preset-angular/build/serializers/html-comment',
   ],
-  transform: { '^.+\\.(ts|js|html)$': 'jest-preset-angular' },
+  transform: {
+    '^.+.(ts|mjs|js|html)$': 'jest-preset-angular',
+  },
+  transformIgnorePatterns: ['node_modules/(?!.*.mjs$)'],
 };

It seems that it has less to do with the version of the dependencies. I’ts like @dean-g pointed out. The global jest.config.js has been altered during migration, but the the projects in the workspace have not had their jest.config.js altered with the new transformIgnorePatterns and transform configurations.

A migration step has gone wrong!

To fix this you have to update your jest.config.js files in all your projects yourself. Here’s an example of how it should look in a library project:

module.exports = {
  displayName: "yourlibraryname",
  preset: '../../jest.preset.js',
  setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.(html|svg)$',
    },
  },
  coverageDirectory: '../../coverage/libs/yourlibraryname',
  transform: {
    '^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
  },
  transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-comment',
  ],
};

Been fixing it on Friday evening actually with a Nx Repository, coming from NG12

So I have one app within apps folder and several libs, all the tests coming from the libs were failing … After a lot of digging, I had to add a test-setup.ts at the the root of the lib, next to index.ts within the folder structure.

In this test-setup, this line made the trick :
import 'jest-preset-angular/setup-jest';

Then in the jest.config.js : image

You’ll notice I had to avoid some special packages such as ngx-translate, ngx-webstorage … this is documented on the jest-preset for angular within the docs.

After all of that, I made it working, hope this helps you guys

I’ve created a minimal repo with lodash-es and @datorama/akita at https://github.com/synec/ng13-jest-fail

On commit bffa49da40b05f17efc8df0e70b596c34ba2785b the test fails because of lodash-es. This is fixed in commit c27cb9371dc1e729fc500ee31a5e87221ec8d517 by adding

moduleNameMapper: {
    "^lodash-es$": "lodash"
  },

to the app jest.config.js

Commit 971d782af5e9c43c797ed6860e076c7b20758ec5 adds a minimal Akita store and test fails again.

jest-preset-angular v11 describes a few changes that need to be performed:

Basically test-setup.ts becomes this:

module.exports = {
    displayName: "your-app-name-here",
    preset: "../../jest.preset.js",
    setupFilesAfterEnv: ["<rootDir>/src/test-setup.ts"],
    globals: {
        "ts-jest": {
            tsconfig: "<rootDir>/tsconfig.spec.json",
            stringifyContentPathRegex: "\\.(html|svg)$",
            useESM: true
        }
    },
    coverageDirectory: "../../coverage/apps/your-app-name-here",
    moduleFileExtensions: ["ts", "html", "js", "json", "mjs"],
    extensionsToTreatAsEsm: [".ts"],
    resolver: "jest-preset-angular/build/resolvers/ng-jest-resolver.js",
    transform: {
        "^.+\\.(ts|js|mjs|html|svg)$": "jest-preset-angular",
    },
    transformIgnorePatterns: [
        "node_modules/(?!.*\\.mjs$)"
    ],
    snapshotSerializers: [
        "jest-preset-angular/build/serializers/no-ng-attributes",
        "jest-preset-angular/build/serializers/ng-snapshot",
        "jest-preset-angular/build/serializers/html-comment",
    ],
};

Hopefully this helps someone, but for me, the issue has not been solved.

if someone is still having issues, add <rootDir> at the beginning of transformIgnorePatterns path, especially jest.config.js files inside libs, if the lib generator doesn’t add it. e.g.

transformIgnorePatterns: ['<rootDir>/node_modules/(?!.*\\.mjs$)'],

This StackOverflow answer did it for me. I made a script to update all my jest.config.js files, I’ll leave it here since it can be useful for someone. Note that you also have to update the dependencies, as stated in the answer.

For what it’s worth, I think a more efficient approach is to mock out the service all together such as

jest.mock('nanoid', () => ({
    __esModule: true, // this property makes it work
    default: 'mockedDefaultExport',
    namedExport: jest.fn(),
}));

in the test-setup.ts file. Then make an Angular wrapper service for the esm service. You can then mock out in your unit tests. Jest configs can then remain the same.

I did as suggested above. Have the test-setup.ts with the import and this is the lib jest config

module.exports = {
  displayName: 'reports',
  preset: '../../jest.preset.js',
  setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.(html|svg)$',
    },
  },
  coverageDirectory: '../../coverage/libs/reports',
  transform: {
    '^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
  },
  transformIgnorePatterns: [
    'node_modules/(?!.*\\.mjs$)',
    'node_modules/(?!@angular|@ngrx|@ngrx-translate|rxjs|countup.js|lodash-es|lodash)',
  ],
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-comment',
  ],
};

and yet it’s the same error 🤷‍♂️ :

    Jest encountered an unexpected token

    Details:

    C:\development\SDS\core-ui-v3\node_modules\lodash-es\lodash.js:10
    export { default as add } from './add.js';
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      3 | import { Actions, createEffect, EffectNotification, ofType, OnRunEffects } from '@ngrx/effects';
      4 | import { FFUFUsersApiService, FFuFUserService, UserEntity, UserField } from '@sds/api';
    > 5 | import { find } from 'lodash-es';

@1antares1 migrating from 12 -> 14 will be especially difficult because of 13.

Instead of doing it manually, can you try run nx migrate latest npm install or yarn depending on your package manager nx migrate --run-migrations

is it truly jest.config or the root jest.preset ? To me it looks like something should be in the root.

That’s why I mentioned above every workspace is unique. You can have it in the root jest.preset.js and it will apply to every other jest.config.js that uses the preset (if it’s not overwriting the config).

That’s an example of a deviation of what we generate and what makes migrations to be hard. Nx workspaces can contain multiple technologies, therefore, we don’t add configuration that might be Angular-specific at this point in time to the root configuration, we rather add it to the configuration of the Angular projects.

Also, how do we add to this the ignore patterns for things like lodah-es… ?

The transformIgnorePatterns property expects an array, you can pass multiple patterns to it. The pattern required to support Angular 13 is 'node_modules/(?!.*.mjs$)'. Besides that one you can have others you need.

I think the migration was not looking jest.preset.js file configuration. it was overriding global configuration. I have removed this setup and moved that into jest.preset.js global configuration file and it worked after that!.

Now, I am running into one last issue,

Jest has detected the following 2 open handles potentially keeping Jest from exiting:

● MESSAGEPORT

> 1 | import 'jest-preset-angular/setup-jest';

I am not sure why I am getting this issue.

I’m getting this error,

import _async_to_generator from “@swc/helpers/src/_async_to_generator.mjs”; ^^^^^^ SyntaxError: Cannot use import statement outside a module at Object.compileFunction (node:vm:360:18) at wrapSafe (node:internal/modules/cjs/loader:1094:15) at Module._compile (node:internal/modules/cjs/loader:1129:27) at Module._compile (/Users/symplrr/Desktop/Projects/Backend/zonocloud-monorepo/node_modules/pirates/lib/index.js:136:24) at Module._extensions…js (node:internal/modules/cjs/loader:1219:10) at Object.newLoader [as .ts] (/Users/symplrr/Desktop/Projects/Backend/zonocloud-monorepo/node_modules/pirates/lib/index.js:141:7) at Module.load (node:internal/modules/cjs/loader:1043:32) at Function.Module._load (node:internal/modules/cjs/loader:878:12) at Module.require (node:internal/modules/cjs/loader:1067:19) at require (node:internal/modules/cjs/helpers:103:18)

While building a nx-plugin

@synec I was able to reproduce that issue in a non-Nx Workspace (ng new workspace). It’s not a specific issue to Nx, however, I’ll investigate and consult with the team to see if we can think of a solution

@leosvelperez , the above PR is an example of migration that might help shed the light on some other issues that might be happening. Feel free to check it as a replication

Got exactly the same error with a new nx workspace generated with npx create-nx-workspace

Though it wasn’t with the latest version so I had to run the migrations.

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    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:

    /home/johannes/projects/herewego/node_modules/@angular/core/fesm2015/testing.mjs:7
    import * as i0 from '@angular/core';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1728:14)

Running npx create-nx-workspace@latest seems to have working tests with jest!