Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 tasks done
the-sabra opened this issue Dec 11, 2023 · 0 comments
Open
5 tasks done

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

the-sabra opened this issue Dec 11, 2023 · 0 comments

Comments

@the-sabra
Copy link

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

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant