【问题标题】:Angular test : Missing locale data for the locale “XXX” with jest角度测试:缺少区域设置“XXX”的区域设置数据
【发布时间】:2021-06-23 11:20:34
【问题描述】:

我正在使用 Angular 10 和 Jest 开发一个应用程序来测试我的应用程序。我输入了区域设置 ID fr-FR,它运行良好,但在我运行它们时没有在测试中:

Error: Missing locale data for the locale "fr-FR".

我不知道在哪里或如何在测试中一劳永逸地设置语言环境。我进行了失败的测试: :

import {LOCALE_ID} from '@angular/core';
import localeFr from '@angular/common/locales/fr';
import {registerLocaleData} from '@angular/common';

registerLocaleData(localeFr, 'fr-Fr');

beforeEach(() => {
TestBed.configureTestingModule({
    providers: [
        {
            provide: LOCALE_ID,
            useValue: 'fr-FR'
        }
    ]
})})

我在日期格式化程序中使用区域设置日期:

export default class DateFormatter {
  private static readonly YYYY_MM_DD = 'yyyy-MM-dd';

  public static readonly LOCALE_DATE: string = 'fr-FR';

  static formatDateDayWithDashes(date: Date) {
    return !!date ? formatDate(date, this.YYYY_MM_DD, this.LOCALE_DATE) : undefined;
  }
}

你知道我可以在哪里为所有测试设置一次语言环境吗?

【问题讨论】:

    标签: angular jestjs internationalization


    【解决方案1】:

    您可以将全局配置放在 setup-jest 文件中。

    如果你没有安装文件,你可以将它添加到你的 jest.config.js 中:

    module.exports = {
      ...
      setupFilesAfterEnv: [
        '<rootDir>/setup-jest.ts',
      ],
      ...
    };
    

    在你的 setup-jest.ts 中:

    [...maybe some preset...]
    import localeFr from '@angular/common/locales/fr';
    import { LOCALE_ID } from '@angular/core';
    import { registerLocaleData } from '@angular/common';
    import { TestBed } from '@angular/core/testing';
    
    registerLocaleData(localeFr, 'fr-FR');
    
    beforeEach(() => {
      TestBed.configureTestingModule({
        providers: [
          {
            provide: LOCALE_ID,
            useValue: 'fr-FR',
          },
        ],
      });
    });
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-26
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多