You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular独立组件Jest单元测试遇Transloco解析错误求解决方案

Angular独立组件Jest测试中Transloco模块解析错误求助

我在为Angular的SrRouteStatus独立组件编写Jest单元测试时,引入TranslocoRootModule触发了Jest解析错误(提示node_modules/flat/index.js中存在意外的export语法)。尝试使用TranslocoTestingModule测试辅助工具后,仍出现相同错误,求正确引入TranslocoTestingModule完成单元测试的方法。


组件代码

.component.html

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

.component.ts

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

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();
  });
});

初始错误信息

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)

我认为错误源于组件中引入的TranslocoRootModule:

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

尝试使用TranslocoTestingModule后的测试代码

.component.spec.ts

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();
  });
});

尝试后的错误信息

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)

内容的提问来源于stack exchange,提问作者Maksat Madeniyetov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:07:03