angularjestjsts-jesttransloco

Jest encountered an unexpected token, Angular, Transloco, Jest Unit testing, can not create standalone component in unit test


.component.html looks like this:

<div>Route Status: {{ routeStatus }}</div>
<div>{{ 'performsTrip' | transloco }}</div>

.component.ts looks like this:

import { CommonModule } from '@angular/common';
import { Component, Input } from '@angular/core';
import { RouteStatus } from '../../../../shared/constants/route-status';
import { TranslocoRootModule } from '../../../../transloco-root.module';

@Component({
  selector: 'sr-route-status',
  templateUrl: './sr-route-status.component.html',
  styleUrls: ['./sr-route-status.component.scss'],
  standalone: true,
  imports: [CommonModule, TranslocoRootModule],
})
export class SrRouteStatusComponent {
  @Input() routeStatus = RouteStatus.PERFORMING_ROUTE;
}

.component.spec.ts looks like this:

import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { SrRouteStatusComponent } from './sr-route-status.component';

function setup() {
  @Component({
    standalone: true,
    imports: [SrRouteStatusComponent],
    template: '<sr-route-status/>',
  })
  class SrRouteStatusTestHostComponent {}

  const fixture = TestBed.createComponent(SrRouteStatusTestHostComponent);
  const component = fixture.componentInstance;
  fixture.detectChanges();
  return { fixture, component };
}

describe('SrRouteStatusComponent', () => {
  it('should create', () => {
    const { component } = setup();
    expect(component).toBeTruthy();
  });
});

There is nothing special, just create standalone component test. However, it shows an error.

The error:

Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/a1234/Desktop/Work/shared-route-info/node_modules/flat/index.js:12
    export function flatten (target, opts) {
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      1 | import { isDevMode, NgModule } from '@angular/core';
    > 2 | import {
        | ^
      3 |   provideTransloco,
      4 |   TranslocoModule,
      5 | } from '@ngneat/transloco';

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (node_modules/@ngneat/transloco/fesm2022/ngneat-transloco.mjs:4:64)
      at Object.<anonymous> (src/app/transloco-root.module.ts:2:1)
      at Object.<anonymous> (src/app/main-layout/components/ui/sr-route-status/sr-route-status.component.spec.ts:4:1)

I think it is due to TranslocoRootModule. Because in SrRouteStatusComponent .spec.ts I imported TranslocoRootModule.

standalone: true,
imports: [CommonModule, TranslocoRootModule],

So, to prevent this error, how can I import TranslocoTestingModule?

I also, wrote .spec.ts in this way (it also does not work):

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { getTranslocoModule } from '../../../../shared/utils/transloco-helpers/transloco-module-testing-helper';
import { SrRouteStatusComponent } from './sr-route-status.component';

describe('SrRouteStatusComponent', () => {
  let component: SrRouteStatusComponent;
  let fixture: ComponentFixture<SrRouteStatusComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [SrRouteStatusComponent, getTranslocoModule()],
    });
    fixture = TestBed.createComponent(SrRouteStatusComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

It shows this error:

Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/a1234/Desktop/Work/shared-route-info/node_modules/flat/index.js:12
    export function flatten (target, opts) {
    ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import { TranslocoTestingModule, TranslocoTestingOptions } from '@ngneat/transloco';
        | ^
      2 |
      3 | import en from '../../../../assets/i18n/en.json';
      4 | import ru from '../../../../assets/i18n/ru.json';

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (node_modules/@ngneat/transloco/fesm2022/ngneat-transloco.mjs:4:64)
      at Object.<anonymous> (src/app/shared/utils/transloco-helpers/transloco-module-testing-helper.ts:1:1)
      at Object.<anonymous> (src/app/main-layout/components/ui/sr-route-status/sr-route-status.component.spec.ts:3:1)

Solution

  • I just downgraded Transloco version, then it worked