Running jest in nest js project is taking a lot of heap memory

I am trying to run unit test on my project which is written with nest js framework. Irrespective of unit tests it is taking almost 4 gb of heap memory which is unexpected.

This is my jest.config.ts:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testMatch: ['**/*-test.ts', '**/**spec.ts', '.*\.spec\.ts$'],
  globalSetup: './spec/jest.setup.ts',
  globalTeardown: './spec/teardown.js',
  reporters: ['default', 'jest-junit'],
  moduleNameMapper: {
    '^apps/(.*)$': '<rootDir>/apps/$1',
    '^common/(.*)$': '<rootDir>/common/$1'
  },
  setupFilesAfterEnv: ['<rootDir>/spec/jest.setup.redis-mock.ts', '<rootDir>/apps/events/jest.setup.ts'],
  moduleFileExtensions: ['js', 'json', 'ts'],
  modulePaths: ['<rootDir>'],
  rootDir: '.',
  transform: {
    '^.+\.(t|j)s$': [
      'ts-jest',
      {
        diagnostics: false
      }
    ]
  },
  collectCoverageFrom: ['**/*.(t|j)s'],
  coverageDirectory: './coverage',
  roots: ['<rootDir>', '<rootDir>/apps/'],
  testPathIgnorePatterns: [
    '/node_modules/',
    '/dist/'
  ]
}

While running with node --no-compilation-cache --inspect-brk -r node_modules/.bin/jest --runInBand apps/events/src/events.controller.spec.ts command, I saw it has already occupied 4gb memory even before my globalSetup is initialized. Not sure why!

This is my tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2020",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "paths": {
      "apps/*": [
        "./apps/*"
      ],
      "common/*": [
        "./common/*"
      ]
    },
    "resolveJsonModule": true
  }
}

This is my apps/events/events.controller.spec.ts:

// import { Test, TestingModule } from '@nestjs/testing'
// import { EventsController } from './events.controller'

describe('EventsController', () => {

  describe('root', () => {
    it('should return "Hello World!"', () => {
      expect(true).toBe(true)
    })
  })
})

which is a very basic test case and should not require to load this much memory.

I am not sure if during initialization of test cases the libraries (my node_modules size is 2.2 gb) are loaded or not, if so what can I do. This is my package.json dependencies:

"dependencies": {
        "@analytics/google-analytics": "^0.5.3",
        "@azure/msal-node": "^2.16.2",
        "@babel/parser": "^7.24.6",
        "@clickhouse/client": "^1.6.0",
        "@dqbd/tiktoken": "^1.0.7",
        "@google-cloud/bigquery": "^5.12.0",
        "@google-cloud/dialogflow": "^5.8.0",
        "@google-cloud/logging-winston": "^4.0.4",
        "@google-cloud/monitoring": "^3.0.3",
        "@google-cloud/pubsub": "^2.15.1",
        "@google-cloud/pubsub_v3": "npm:@google-cloud/[email protected]",
        "@google-cloud/pubsub_v4": "npm:@google-cloud/[email protected]",
        "@google-cloud/secret-manager": "^5.0.1",
        "@google-cloud/storage": "^5.13.1",
        "@google-cloud/storage_v2": "npm:@google-cloud/[email protected]",
        "@google-cloud/tasks": "0.4.0",
        "@google-cloud/tasks_v3": "npm:@google-cloud/[email protected]",
        "@mhoc/axios-digest-auth": "^0.8.0",
        "@microsoft/microsoft-graph-client": "^3.0.5",
        "@mozilla/readability": "^0.4.4",
        "@nestjs/axios": "^3.0.2",
        "@nestjs/common": "^10.3.7",
        "@nestjs/config": "^3.2.2",
        "@nestjs/core": "^10.3.7",
        "@nestjs/mongoose": "^7.2.4",
        "@nestjs/platform-express": "^10.3.7",
        "@nestjs/swagger": "^7.3.1",
        "@types/bcrypt": "^3.0.0",
        "@types/bunyan": "^1.8.6",
        "@types/facebook-js-sdk": "^3.3.5",
        "@types/fluent-ffmpeg": "^2.1.20",
        "@types/google-libphonenumber": "^7.4.20",
        "@types/handlebars": "^4.0.37",
        "@types/json2csv": "^5.0.3",
        "@types/jsonwebtoken": "^8.5.1",
        "@types/lodash": "^4.14.168",
        "@types/mailgun-js": "^0.22.12",
        "@types/moment-timezone": "^0.5.30",
        "@types/multer": "^1.4.7",
        "@types/passport-local-mongoose": "^6.1.0",
        "@types/shortid": "^0.0.29",
        "@types/simple-oauth2": "^4.1.1",
        "@types/valid-url": "^1.0.3",
        "analytics": "^0.7.5",
        "atob": "^2.1.2",
        "authorizenet": "^1.0.8",
        "await-to-js": "^3.0.0",
        "aws-sdk": "^2.906.0",
        "axios": "^0.21.1",
        "basic-auth": "2.0.1",
        "bcrypt": "^5.0.1",
        "class-transformer": "0.4.0",
        "class-validator": "^0.13.1",
        "compression": "^1.7.4",
        "convert-array-to-csv": "^2.0.0",
        "cookie-parser": "^1.4.5",
        "crypto-js": "^3.1.9-1",
        "csvtojson": "^2.0.10",
        "currency.js": "^2.0.4",
        "date-fns": "2",
        "date-fns-tz": "2",
        "dialogflow": "^0.8.0",
        "dotenv-safe": "^8.2.0",
        "elasticsearch": "^16.7.2",
        "emoji-regex": "^10.3.0",
        "eslint-plugin-import": "^2.29.1",
        "fast-xml-parser": "^4.1.2",
        "fb": "^2.0.0",
        "firebase-admin": "12.1.0",
        "fluent-ffmpeg": "^2.1.2",
        "form-data": "^4.0.0",
        "fs": "^0.0.1-security",
        "geoip-country": "^4.1.9",
        "google-auth-library": "^7.10.1",
        "google-libphonenumber": "3.2.34",
        "google-spreadsheet": "^4.1.2",
        "googleapis": "^92.0.0",
        "handlebars": "^4.7.8",
        "handlebars-intl": "^1.1.2",
        "heapdump": "^0.3.15",
        "html-to-text": "^8.0.0",
        "ics": "^2.35.0",
        "id-shorter": "^0.1.2",
        "intuit-oauth": "^4.0.0",
        "ioredis": "^4.16.0",
        "isbot": "^5.1.6",
        "isomorphic-fetch": "^3.0.0",
        "javascript-obfuscator": "^4.1.0",
        "jest-junit": "^13.0.0",
        "jimp": "^0.22.10",
        "joi": "^17.4.0",
        "jsdom": "^19.0.0",
        "json-bigint": "^1.0.0",
        "json2csv": "^6.0.0-alpha.2",
        "jsonwebtoken": "^8.5.1",
        "jstoxml": "^3.2.6",
        "langfuse": "^3.24.0",
        "lodash": "4.17.19",
        "luxon": "^3.4.4",
        "mailgun-js": "^0.22.0",
        "mailgun.js": "^7.0.1",
        "marked": "^13.0.2",
        "md5": "^2.3.0",
        "metadata-scraper": "^0.2.61",
        "migrate-mongo": "^8.2.2",
        "mime-types": "^2.1.35",
        "mjml": "^4.10.4",
        "moment": "^2.29.4",
        "moment-timezone": "^0.5.43",
        "mongo-escape": "^2.0.6",
        "mongoose": "5.12.13",
        "mongoose-extend-schema": "^1.0.0",
        "mongoose-long": "^0.5.1",
        "mongoose6": "npm:mongoose@^6.8.2",
        "mysql2": "^2.3.3",
        "node-forge": "^1.3.1",
        "node-ical": "^0.16.1",
        "parse-reply": "^0.0.4",
        "passport-local-mongoose": "^6.1.0",
        "path": "^0.12.7",
        "path-to-regexp": "^7.1.0",
        "pdf-lib": "^1.17.1",
        "prom-client": "^14.0.1",
        "promisify-any": "2.0.1",
        "psl": "^1.8.0",
        "puppeteer": "^22.7.1",
        "qs": "^6.10.1",
        "sanitize-html": "^2.13.0",
        "sequelize": "^6.37.3",
        "sharp": "^0.33.5",
        "simple-oauth2": "1.5.0",
        "ts-loader": "^9.2.3",
        "ts-node": "^9.1.1",
        "tsconfig-paths": "^3.9.0",
        "type-is": "1.6.18",
        "typescript": "^5.4.4",
        "ulid": "^2.3.0",
        "url": "^0.11.0",
        "uuid": "^8.3.2",
        "uuid-mongodb": "^2.5.1",
        "uuidv4": "^6.2.7",
        "vm2": "^3.9.17",
        "webpack": "^5.93.0",
        "winston": "^3.3.3",
    },
    "devDependencies": {
        "@nestjs/cli": "^10.3.2",
        "@nestjs/schematics": "^10.1.1",
        "@nestjs/testing": "^10.3.7",
        "@types/authorizenet": "^1.0.0",
        "@types/bcrypt": "^3.0.0",
        "@types/bunyan": "^1.8.6",
        "@types/cookie-parser": "^1.4.2",
        "@types/cron": "^2.0.0",
        "@types/express": "^4.17.11",
        "@types/facebook-nodejs-business-sdk": "^15.0.5",
        "@types/google-libphonenumber": "^7.4.20",
        "@types/ioredis": "^4.28.10",
        "@types/ioredis-mock": "^8.2.2",
        "@types/isomorphic-fetch": "^0.0.36",
        "@types/jest": "^26.0.20",
        "@types/jsonwebtoken": "^8.5.1",
        "@types/lodash": "^4.14.168",
        "@types/mailgun-js": "^0.22.12",
        "@types/mjml-core": "^4.7.1",
        "@types/moment-timezone": "^0.5.30",
        "@types/multer": "^1.4.7",
        "@types/node": "^18.11.18",
        "@types/psl": "^1.1.0",
        "@types/request-ip": "^0.0.37",
        "@types/ssh2-sftp-client": "^9.0.1",
        "@types/supertest": "^2.0.10",
        "@typescript-eslint/eslint-plugin": "^7.6.0",
        "@typescript-eslint/parser": "^7.5.0",
        "eslint": "^8.0.0",
        "eslint-config-prettier": "^8.1.0",
        "eslint-import-resolver-typescript": "^3.6.1",
        "eslint-plugin-prettier": "^3.3.1",
        "firestore-jest-mock": "^0.17.0",
        "husky": "^7.0.4",
        "ioredis-mock": "^6.13.0",
        "jest": "^29.7.0",
        "lint-staged": "^11.0.0",
        "mongodb-memory-server": "^8.8.0",
        "prettier": "^2.2.1",
        "pretty-quick": "^3.1.1",
        "rimraf": "^3.0.2",
        "supertest": "^6.1.3",
        "ts-jest": "^29.1.3",
        "ts-loader": "^8.0.17",
        "tsconfig-paths": "^3.9.0",
        "tslint": "^6.1.3"
    }
    ```

Will appreciate if someone can help!