NestJS Application Dependencies Not Found When Reusing Singleton in Jest E2E Tests

I’m working on reusing a NestJS application instance across multiple test suites in my Jest E2E tests.

Here is my current setup:

jest.config.json

{
  other configs...

  "setupFilesAfterEnv": ["./test/jest.setup.ts"],
  "globalSetup": "./test/mysql-global-setup.ts",
  "globalTeardown": "./test/mysql-global-teardown.ts",
  "maxWorkers": 1
}
// test/mysql-global-setup.ts

import { MySqlContainer, StartedMySqlContainer } from '@testcontainers/mysql';
import { initializeApp } from './test_nest_application';
const globalAny: any = global;

export default async () => {
  // some mysql testcontainers settings...

  console.log('Starting NestJS application...');
  const testContext = await initializeApp();
  console.log('NestJS application started.');
};
// test/test-nest-application.ts

import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';
import { WinstonLoggerService } from '../src/common/logger/logger.service';
import * as cookieParser from 'cookie-parser';
import { InternalExceptionsFilter } from '../src/common/filters/internal-exceptions.filter';
import { HttpExceptionsFilter } from '../src/common/filters/http-exception.filter';
import { INestApplication, ValidationPipe } from '@nestjs/common';

let app: INestApplication;

// declare global nest application
declare global {
  var testContext;
}

export const initializeApp = async (): Promise<INestApplication> => {
  // use global.testContext as singleton instance
  if (!global.testContext) {
    const moduleFixture = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();
    app = moduleFixture.createNestApplication();

    // setting some application's config values...
    await app.init();

    global.testContext = app;
  }

  return global.testContext;
};

I want to reuse the TestNestApplication in separate test suites.
However, according to jest document says that “Any global variables that are defined through globalSetup can only be read in globalTeardown.”

To work around this, I defined a singleton module that returns the Nest application named initializeApp() to reuse the Nest application.

The initialized application isn’t defined in globalSetup instead, it is defined in a separate module and added to a global variable.

In my test suites, I use the application like this:

// test/rss/rss-e2e-spec.ts

describe('Rss Accept E2E Test', () => {
  let app: INestApplication;
  let rssRepository: Repository<Rss>;
  let rssAcceptRepository: Repository<RssAccept>;
  let redisService: RedisService;

  beforeAll(async () => {
    app = await initializeApp();
    rssRepository = app.get(RssRepository);
    rssAcceptRepository = app.get(RssAcceptRepository);
    redisService = app.get(RedisService);
  });

describe('POST /api/rss/accept/{rssId}', () => {
    // test codes...

When I run this test, I encounter the following error:

    Nest could not find RssRepository element (this provider does not exist in the current context)

      19 |   beforeAll(async () => {
      20 |     app = await initializeApp();
    > 21 |     rssRepository = app.get(RssRepository);
         |                         ^
      22 |     rssAcceptRepository = app.get(RssAcceptRepository);
      23 |     redisService = app.get(RedisService);
      24 |   });

Nest can’t find the RssRepository instance as well as other dependencies. However, initializing the Nest application every time with beforeAll() works fine without any errors.

Additionally, if no value is passed to the .get() method, I receive an error stating that the .get() method itself cannot be found. However, it keep says can’t find providers.

But, initializing nest application every time with beforeAll() works fine with no errors. (Which means, same initializing codes worked fine when I ran it in beforeAll()

Any insights or suggestions on how to properly share the Nest application instance across test suites would be greatly appreciated.