toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
656 stars 113 forks source link

Unit Test Failure: TypeError with NestJS-i18n Injection #602

Open the-sabra opened 11 months ago

the-sabra commented 11 months ago

Describe the bug

I wrote a unit test, but it failed because I injected the Nest-i18n service incorrectly or I didn't know. The error message says...

FAIL  src/auth/auth.service.spec.ts
  ● Test suite failed to run

    TypeError: (0 , index_1.I18nResolverOptions) is not a function

      1 | import { Injectable } from '@nestjs/common';
    > 2 | import { I18nContext, I18nService } from 'nestjs-i18n';
        | ^
      3 |
      4 | @Injectable()
      5 | export class CustomI18Service {

I searched many times but this error is not famous I use this to mock service but it gives me the same message

service I write

import { Injectable } from '@nestjs/common';
import { I18nContext, I18nService } from 'nestjs-i18n';

@Injectable()
export class CustomI18Service {
  constructor(private i18n: I18nService) {}
  translate(key: string, options?: any) {
    const lang: string = I18nContext.current().lang;
    return this.i18n.t(key, { lang, ...options });
  }
}

and this is unit test example I run it

describe('AuthService', () => {
  let authService: AuthService;
  const mockPrismaService = {
    course: {
      findMany: jest.fn(),
      findUnique: jest.fn(),
      update: jest.fn(),
      create: jest.fn(),
      delete: jest.fn(),
    },
    instructor: {
      findMany: jest.fn(),
      findUnique: jest.fn(),
      update: jest.fn(),
      create: jest.fn(),
      delete: jest.fn(),
    },
  };
  let jwtService: JwtService;
  let i18nService: CustomI18Service;
  beforeEach(async () => {
    const i18nServiceMock = {
      t: () => 'test',
    };
    const module: TestingModule = await Test.createTestingModule({
      providers: [AuthService, PrismaService, JwtService, CustomI18Service],
    })
      .overrideProvider(CustomI18Service)
      .useValue(i18nServiceMock)
      .overrideProvider(PrismaService)
      .useValue(mockPrismaService)
      .compile();

    authService = module.get<AuthService>(AuthService);
    jwtService = module.get<JwtService>(JwtService);
  });

  describe('StudentSignUp', () => {
    it('should throw NOT_FOUND if student ID does not exist', async () => {
      // Implement your test here
    });

  });

  afterEach(() => {
    jest.clearAllMocks();
  });
});

Reproduction

I encountered a unit test failure with the following error message

System Info

"jest": "29.7.0"

Used Package Manager

npm

Validations