toonvanstrijp / nestjs-i18n

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

Not translating In jest, resolver returns only the passed key instead #495

Open itinance opened 1 year ago

itinance commented 1 year ago

I came across that when running unit-tests via jest, the t method returns the passed Key and is not doing any translation.

I pass the I18nService into my service:

@Injectable()
export class TokensService {
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  constructor(private readonly i18n: I18nService) {}

and can translate using this.18n.t("key") properly. However, in the context of unit-tests, it only returns the passed key and is not translating anything.

I have tested with the following initialization code (with parameters similar to app.module.ts, but adjusted path)

const module: TestingModule = await Test.createTestingModule({
  imports: [
    I18nModule.forRoot({
      fallbackLanguage: 'en-US',
      loaderOptions: {
        path: path.join(__dirname, '/../i18n/'),
        watch: true,
      },
      resolvers: [
        { use: QueryResolver, options: ['lang'] },
        AcceptLanguageResolver,
      ],
    }),
  ],
  providers: [
    TokensService,
  ],
}).compile();

When executing the following line inside my service,

const s = await this.18n.t("exampleKey")

In the context of a unit test, calling it will simply return 'exampleKey.' However, when the application is run, it will work correctly and return the actual string from the JSON file.

The path in my example is correctly resolved; otherwise, an error message would have appeared. In my case, no errors were thrown.

Happening with versions from 9.0.9, 9.0.10 and 10.2.6