Skip to content

Typechecking performance regression in @golevelup/ts-jest createMock() #1139

@Simon-Hayden-Dev

Description

@Simon-Hayden-Dev

I was upgrading from version 0.5.6 to 1.2.1 and noticed that the editor felt rather sluggish. When tracing tsc, I found that our test files containing createMock() took quite some time to finish type checking.

For example, here is one of our test files:

import { createMock } from '@golevelup/ts-jest';
import { getQueueToken } from '@nestjs/bullmq';
import { Test } from '@nestjs/testing';
import { BloomreachConfigService } from '@newseditor/bloomreach';
import { DatabaseModule } from '@newseditor/database';
import { JobQueues } from '@newseditor/job-queue';
import { RedisModule } from '@newseditor/redis';
import { SearchNodeModule } from '@newseditor/search-node';
import { AiTranslationConfig } from '@newseditor/translator';
import { BackendModule } from './backend.module';

describe('BackendModule', () => {
  it('should be imported without errors', async () => {
    let testingModule = Test.createTestingModule({
      imports: [BackendModule],
    })
      .overrideModule(DatabaseModule)
      .useModule(DatabaseModule.forPrismaClient(createMock()))
      .overrideModule(RedisModule)
      .useModule(RedisModule.forRedisClient(createMock()))
      .overrideModule(SearchNodeModule)
      .useModule(
        SearchNodeModule.forConfig({
          openSearchClient: createMock(),
          searchConfig: { url: '', packagePath: null, index: 'test', configurationTarget: 'test_v0' },
        }),
      )
    // Mock the BullMQ queues
    for (const queueName of Object.values(JobQueues)) {
      testingModule = testingModule.overrideProvider(getQueueToken(queueName)).useValue(createMock());
    }
    await testingModule.compile();
  });
});

The three important calls here are DatabaseModule.forPrismaClient(createMock()) which expects a PrismaClient instance, RedisModule.forRedisClient(createMock()) expecting a Client from ioredis, and (by far the worst offender) SearchNodeModule.forConfig({ openSearchClient: createMock() }) expecing a Client from @opensearch-project/opensearch.

Type-checking this file using this command:

pnpm tsc --noEmit --project ./apps/backend/tsconfig.spec.json --extendedDiagnostics --generateTrace ./name-of-folder

I noticed that this file alone takes around 450ms to type-check. I then went back to the old version and measured only around 160ms.

By installing versions 1.2.0 -> 0.5.7 of @golevelup/ts-jest, I pinned the jump in type checking duration to 0.7.0 -> 1.0.0.

For 0.7.0, the type checking still takes around 160ms. After installing 1.0.0 the same file takes 490ms. So something that was changed after 0.7.0 causes a massive regression in type-checking performance.

I did not investigate any further though, so I don't know what code changes may have causes the issue in particular. But if time allows it, I hope I can check your code and how it may be optimized.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions