keycloak-angular: Jest not work after migrating to Angular 19.x ( Unexpected token "export" )
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',
],
};
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 4
- Comments: 23 (2 by maintainers)
Foe me, this part in my jest.config.ts worked
This is also faster than the transformIgnorePattern. After upgrading angular to latest (current 19), and also nx (current 20), the transformIgnorePatterns stopped working for me.
I have opened a ticket in the official repo of keycloak: https://github.com/keycloak/keycloak/issues/36522
Maybe we get an official answer there
@Chris2011 Thanks four your answer, this works for me.
I just updated the path. Added
<rootDir>/node_modules/beforekeycloak-angular/then it works. @dvag-carsten-zedler perhaps it will help you too.I have a feeling it is not about Angular 19 but rather TypeScript 5.7. The issue here is that TypeScript started to output es modules irrespectively of the setting of “module”. There are many reference about it. Of course you should also do
transformIgnorePatternsto process yournode_moduleses to cjs. But do it with esbuild instead of TypeScript! I will share here my jest.config.ts with comments to solve this issue and some other. The key here isprocessWithEsbuild: