Skip to content

Jest not work after migrating to Angular 19.x ( Unexpected token "export" ) #613

@ValentinDEJ

Description

@ValentinDEJ

Bug Report or Feature Request (mark with an x)

  • bug report -> please search for issues before submitting
  • feature request

Versions.

  • keycloak-angular: 19.0.2
  • keycloak-js: 26.0.7
  • angular: 19.0.6
  • jest: 29.7.0

Problems.

The described issue is occurring within an nx-workspace, using the mentioned versions above of angular, jest, keycloak-angular and keycloak-js.

I tried to adapt my jest configuration with suggestion mentionned in this issue but is not work, i have same problem.

The log given by the failure.

 FAIL   authentication  projects/shared/authentication/web/src/ensure-authenticated.guard.spec.ts
  ● 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:

    /home/valentin/Workspace/poc-csuite-v3/node_modules/keycloak-js/lib/keycloak.js:2
    import { __awaiter } from "tslib";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { ActivatedRouteSnapshot, CanActivateFn, RouterStateSnapshot } from '@angular/router';
    > 2 | import { AuthGuardData, createAuthGuard } from 'keycloak-angular';
        | ^
      3 |
      4 | // This is exported only to facilitate testing. It should not be used directly.
      5 | export const internalEnsureAuthenticated = async (_route: ActivatedRouteSnapshot, state: RouterStateSnapshot, authData: AuthGuardData) => {

      at Runtime.createScriptFromCode (../../../node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (../../../node_modules/keycloak-angular/fesm2022/keycloak-angular.mjs:6:22)
      at Object.<anonymous> (web/src/ensure-authenticated.guard.ts:2:1)
      at Object.<anonymous> (web/src/ensure-authenticated.guard.spec.ts:4:1)

jest.config.ts:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions