Hello, I have a very strange problem since I set up automated tests with jest into my nestjs project. When I run a test file for the first time, jest takes more than 6-7 minutes to run. Whereas the same test ported to vite using SWC takes less than 300ms.
Anyone had a similar issue with ts-jest ? How have you managed to fix it ?
--
Other runs with jest takes five seconds, I believe that once jest caches the dependencies, it fixes the problem. Therefore, the issue might be in the TS compilation.
Here is a sample of my integration test file. Since it's a professional project, I unfortunately can't share a working repository.
import { HttpService } from '@nestjs/axios';
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule, getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { configuration } from '../../config/configuration';
import { RequestUserModel } from '../auth/model/request-user.model';
import { CryptoService } from '../shared/services/crypto.service';
import { entities } from '../shared/test-utils/entities';
import { FixtureService } from '../shared/test-utils/fixture.service';
import { TenantEntity } from '../tenant/entity/tenant.entity';
import { TenantModel } from '../tenant/model/tenant.model';
import { TenantService } from '../tenant/service/tenant.service';
import { UserEntity } from '../users/entity/user.entity';
import { SalesforceConnectionsController } from './SalesforceConnections.controller';
import { SalesforceInstanceDetailsResponseDto } from './dto/SalesforceInstanceDetailsResponse.dto';
import { SalesforceConnectionEntity } from './entity/SalesforceConnection.entity';
import { SalesforceInstanceEntity } from './entity/SalesforceInstance.entity';
import { SalesforceConnectionService } from './service/SalesforceConnection.service';
import { SalesforceInstanceService } from './service/SalesforceInstance.service';
import { SalesforceOauthService } from './service/SalesforceOauth.service';
import { ConnectivityStatus } from './dto/StatusResponse.dto';
To troubleshoot the problem, I added console.time on each imports. Here is the result :
cross-env NODE_ENV=test jest -runInBand --maxWorkers=1 SalesforceConnections.controller.spec.ts --no-cache
Imports :
HttpService: 62226 ms (@nestjs/axios)
BadRequestException: 1 ms
ConfigModule: 4736 ms (@nestjs/config)
Test: 22756 ms (@nestjs/testing)
TypeOrmModule: 133894 ms (@nestjs/typeorm)
Repository: 0 ms
configuration: 9 ms
RequestUserModel: 8 ms
CryptoService: 33 ms
entities: 153 ms
FixtureService: 109 ms
TenantEntity: 0 ms
TenantModel: 5 ms
TenantService: 34 ms
UserEntity: 0 ms
SalesforceConnectionsController: 183104 ms (imports the same)
SalesforceInstanceDetailsResponseDto: 0 ms
SalesforceConnectionEntity: 0 ms
SalesforceInstanceEntity: 0 ms
SalesforceConnectionService: 0 ms
SalesforceInstanceService: 0 ms
SalesforceOauthService: 0 ms
ConnectivityStatus: 1 ms
Test module compilation :
Compile: 11315 ms