LogoLogo
❤️ SponserMore
  • 🚀Getting Started
    • Installation
    • Angular Compatibility
    • Config Options
  • 💡Core Concepts
    • Translation in the Template
    • Signals
    • Translation API
    • Language API
  • 🧠Advanced Topics
    • 🦥Lazy Load
      • Scope Configuration
      • Inline Loaders
    • The Transpiler
    • SSR Support
    • Unit Testing
    • Hack the Library
  • 🧪Sandbox & Examples
  • ⚙️Additional Functionality
    • Loading Template
    • Key Referencing
    • Utility Functions
    • Comments for Translators
    • Multiple Languages Simultaneously
  • 📦Migration Guides
    • Migrate from ngx-translate
    • Migrate from Angular's i18n
  • 🔧Tools
    • Keys Manager (TKM)
      • Keys Extractor
      • Keys Detective
      • Options
      • Debugging
      • Using with Nx 🐋
    • Validator
    • Optimize
    • Scoped Library Extractor
  • 🔌Plugins
    • Message Format
    • Persist Translations
    • Persist Lang
    • Preload Langs
    • Locale l10n
    • Community Plugins
  • 🧰Schematics
    • :ng-add
    • :scope
    • :join
    • :split
  • 🍳Recipies
    • Prefetch User Language
    • Using Xliff
    • Generate Locale Files using Google Translate
  • 📚Blog Posts
    • Transloco Team Posts
      • Transloco Goes Functional: A Guide to Transloco's Functional Transpiler
    • From the Community
  • ❓FAQs
Powered by GitBook

@ 2025 Transloco

On this page
  • Using the module in your spec files
  • Testing scopes
  • TypeScript Configuration

Was this helpful?

  1. Advanced Topics

Unit Testing

Note

If you’re experiencing issues running unit tests with Jest and Transloco due to the ESM version of the flat library—commonly encountering an error like:

Error while trying to load ‘en’: TypeError: (0 , import_flat.flatten) is not a function

You can resolve this issue by upgrading to Transloco version 7.5.1 or higher, as the flat dependency was removed starting from that version.

When running tests, it's important to have the languages available immediately and synchronously. Transloco provides the TranslocoTestingModule, which allows you to specify the languages and configuration needed for your specs.

To follow the DRY (Don't Repeat Yourself) principle, it's a good idea to create a module factory function that can be reused in each spec. Here's an example:

transloco-testing.module.ts
import { TranslocoTestingModule, TranslocoTestingOptions } from '@jsverse/transloco';
import en from '../assets/i18n/en.json';
import es from '../assets/i18n/es.json';

export function getTranslocoModule(options: TranslocoTestingOptions = {}) {
  return TranslocoTestingModule.forRoot({
    langs: { en, es },
    translocoConfig: {
      availableLangs: ['en', 'es'],
      defaultLang: 'en',
    },
    preloadLangs: true,
    ...options,
  });
}

Using the module in your spec files

app.component.spec.ts
describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [getTranslocoModule()],
      declarations: [AppComponent],
    }).compileComponents();
  }));

  it('should work', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    expect(
      fixture.debugElement.query(By.css('h1')).nativeElement.innerText
    ).toBe('hello');
  });
});

Testing scopes

If you need to test scopes, you should add them as languages. For example:

transloco-testing.module.ts
export function getTranslocoModule(options: TranslocoTestingOptions = {}) {
  return TranslocoTestingModule.forRoot({
    langs: {
      en,
      es,
      'admin-page/en': admin,
      'admin-page/es': adminSpanish,
    },
    translocoConfig: {
      availableLangs: ['en', 'es'],
      defaultLang: 'en',
    },
    preloadLangs: true,
    ...options,
  });
}

TypeScript Configuration

To import JSON files in your TypeScript project, you need to update your tsconfig.json with the following properties:

{
  "resolveJsonModule": true,
  "esModuleInterop": true
}
PreviousSSR SupportNextHack the Library

Last updated 1 month ago

Was this helpful?

You can find a full example .

You can find an example .

🧠
here
here