Repository: jhipster/jhipster-lite (java)

Issue:
An unhandled exception occurred: require is not defined when we apply Angular and typescript modules
An unhandled exception occurred: require is not defined when we apply Angular and typescript modules

Steps to reproduce :

1. run jhipter-lite => `java -jar jhlite-1.31.0.jar`
2. create a new folder: `take jhlite-issue-typemodule`
3. apply these modules using http://localhost:7471/landscape: `init`, `maven-java`, `java-base`, `maven-wrapper`, `spring-boot`, `prettier`, `typescript` and `angular-core`
4. `npm install`
5. `npm run test` or `npm run test:coverage`

➜  jhlite-issue-typemodule git:(main) ✗ npm run test         

> sample-app@0.0.0 test
> npm run watch:test

> sample-app@0.0.0 watch:test
> ng test --watch

An unhandled exception occurred: require is not defined
See "/private/var/folders/s7/140cq9qx2bq5l707qynhzcrw0000gn/T/ng-xQiMe1/angular-errors.log" for further details.


To fix this issue I removed the `"type":"module"` from package.json

<img width="529" alt="Image" src="https://github.com/user-attachments/assets/1080d5de-8798-42a5-bd4c-d388aea0915d" />


Pull request:
Angular: compatibility with type:module
Fix #12524 

Patch:
diff --git a/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java
index cd4313da6..025f383ba 100644
--- a/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java
+++ b/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java
@@ -101,7 +101,7 @@ public class AngularModuleFactory {
         .batch(SOURCE, to("."))
           .addTemplate("eslint.config.mjs")
           .addTemplate("proxy.conf.json")
-          .addTemplate("jest.conf.js")
+          .addTemplate("jest.conf.mjs")
           .addTemplate("tsconfig.spec.json")
           .and()
         .batch(SOURCE.file("src/main/webapp/app"), to("src/main/webapp/app"))
diff --git a/src/main/resources/generator/client/angular/core/angular.json.mustache b/src/main/resources/generator/client/angular/core/angular.json.mustache
index b84a8c82e..69944ece4 100644
--- a/src/main/resources/generator/client/angular/core/angular.json.mustache
+++ b/src/main/resources/generator/client/angular/core/angular.json.mustache
@@ -83,7 +83,7 @@
         "test": {
           "builder": "@angular-builders/jest:run",
           "options": {
-            "configPath": "jest.conf.js"
+            "configPath": "jest.conf.mjs"
           }
         },
         "extract-i18n": {
diff --git a/src/main/resources/generator/client/angular/core/jest.conf.js.mustache b/src/main/resources/generator/client/angular/core/jest.conf.mjs.mustache
similarity index 53%
rename from src/main/resources/generator/client/angular/core/jest.conf.js.mustache
rename to src/main/resources/generator/client/angular/core/jest.conf.mjs.mustache
index 3ea75792b..cc1f8cbc6 100644
--- a/src/main/resources/generator/client/angular/core/jest.conf.js.mustache
+++ b/src/main/resources/generator/client/angular/core/jest.conf.mjs.mustache
@@ -1,10 +1,10 @@
-const { pathsToModuleNameMapper } = require('ts-jest');
+import { pathsToModuleNameMapper } from 'ts-jest';
+import { readFileSync } from 'fs';
+import { resolve } from 'path';
 
-const {
-  compilerOptions: { paths = {}, baseUrl = './' },
-} = require('./tsconfig.json');
+const tsconfig = JSON.parse(readFileSync(resolve('./tsconfig.json'), 'utf-8'))
 
-module.exports = {
+export default {
   transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$|dayjs/esm|keycloak-js)'],
   transform: {
     '^.+\\.(ts|js|mjs|html|svg)$': [
@@ -15,10 +15,10 @@ module.exports = {
     ],
   },
   resolver: 'jest-preset-angular/build/resolvers/ng-jest-resolver.js',
-  roots: ['<rootDir>', `<rootDir>/${baseUrl}`],
-  modulePaths: [`<rootDir>/${baseUrl}`],
-  cacheDirectory: '<rootDir>/{{projectBuildDirectory}}/jest-cache',
-  coverageDirectory: '<rootDir>/{{projectBuildDirectory}}/test-results/',
+  roots: ['<rootDir>', `<rootDir>/${tsconfig.compilerOptions.baseUrl}`],
+  modulePaths: [`<rootDir>/${tsconfig.compilerOptions.baseUrl}`],
+  cacheDirectory: '<rootDir>/target/jest-cache',
+  coverageDirectory: '<rootDir>/target/test-results/',
   coverageThreshold: {
     global: {
       statements: 100,
@@ -27,7 +27,9 @@ module.exports = {
       lines: 100,
     },
   },
-  moduleNameMapper: pathsToModuleNameMapper(paths, { prefix: `<rootDir>/${baseUrl}/` }),
+  moduleNameMapper: pathsToModuleNameMapper(tsconfig.compilerOptions.paths ?? {}, {
+    prefix: `<rootDir>/${tsconfig.compilerOptions.baseUrl}/`,
+  }),
   testEnvironmentOptions: {
     url: 'https://jestjs.io',
   },
diff --git a/src/main/resources/generator/client/common/.eslintignore b/src/main/resources/generator/client/common/.eslintignore
index b10c1db27..72fc56f17 100644
--- a/src/main/resources/generator/client/common/.eslintignore
+++ b/src/main/resources/generator/client/common/.eslintignore
@@ -2,8 +2,8 @@ node_modules/
 src/main/docker/
 src/test/webapp/cypress/
 src/test/webapp/protractor.conf.js
-jest.conf.js
-src/test/webapp/jest.conf.js
+jest.conf.mjs
+src/test/webapp/jest.conf.mjs
 webpack/
 target/
 build/


Test patch:
diff --git a/src/test/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactoryTest.java
index ceb2cf6ed..28a8c4190 100644
--- a/src/test/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactoryTest.java
+++ b/src/test/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactoryTest.java
@@ -93,7 +93,7 @@ class AngularModuleFactoryTest {
       .and()
       .hasPrefixedFiles(
         "",
-        "jest.conf.js",
+        "jest.conf.mjs",
         "angular.json",
         "tsconfig.json",
         "tsconfig.app.json",
diff --git a/src/test/resources/projects/angular/angular.json b/src/test/resources/projects/angular/angular.json
index ca8ae178a..404a3a891 100644
--- a/src/test/resources/projects/angular/angular.json
+++ b/src/test/resources/projects/angular/angular.json
@@ -86,7 +86,7 @@
         "test": {
           "builder": "@angular-builders/jest:run",
           "options": {
-            "configPath": "jest.conf.js"
+            "configPath": "jest.conf.mjs"
           }
         },
         "extract-i18n": {
diff --git a/src/test/resources/projects/angular/empty-angular.json b/src/test/resources/projects/angular/empty-angular.json
index 83ec856c3..646a9a45c 100644
--- a/src/test/resources/projects/angular/empty-angular.json
+++ b/src/test/resources/projects/angular/empty-angular.json
@@ -83,7 +83,7 @@
         "test": {
           "builder": "@angular-builders/jest:run",
           "options": {
-            "configPath": "jest.conf.js"
+            "configPath": "jest.conf.mjs"
           }
         },
         "extract-i18n": {