# Unit Testing

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:

{% code title="transloco-testing.module.ts" %}

```typescript
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,
  });
}
```

{% endcode %}

### Using the module in your spec files

<pre class="language-typescript" data-title="app.component.spec.ts"><code class="lang-typescript">describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
<strong>      imports: [getTranslocoModule()],
</strong>      declarations: [AppComponent],
    }).compileComponents();
  }));

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

You can find a full example [here](https://github.com/jsverse/transloco/blob/master/apps/transloco-playground/src/app/home/home.component.spec.ts).

### Testing scopes

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

<pre class="language-typescript" data-title="transloco-testing.module.ts"><code class="lang-typescript">export function getTranslocoModule(options: TranslocoTestingOptions = {}) {
  return TranslocoTestingModule.forRoot({
    langs: {
      en,
      es,
<strong>      'admin-page/en': admin,
</strong><strong>      'admin-page/es': adminSpanish,
</strong>    },
    translocoConfig: {
      availableLangs: ['en', 'es'],
      defaultLang: 'en',
    },
    preloadLangs: true,
    ...options,
  });
}
</code></pre>

You can find an example [here](https://github.com/jsverse/transloco/blob/master/apps/transloco-playground/src/app/lazy/lazy.component.spec.ts).

### TypeScript Configuration

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

```json
{
  "resolveJsonModule": true,
  "esModuleInterop": true
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jsverse.gitbook.io/transloco/advanced-features/unit-testing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
