运行 在 angular + jest 中进行单元测试时如何正确模拟库 "Twemoji"?

How to correctly mock the library "Twemoji" when running unit tests in angular + jest?

我的 objective 是 运行 在我的 Angular 应用程序中开玩笑地进行单元测试,但没有得到:

error TS2304: Cannot find name 'twemoji'

Twemoji 添加到项目 package.jsonangular.json 文件的脚本部分。它在 运行ning 和构建应用程序时完美运行,但在 运行ning 单元测试时失败并出现上述错误。

我试过的

我已尝试将以下内容添加到我的 src/jestGlobalMocks.ts 文件中:

declare var twemoji: {
    parse(str: string, options?: { folder: string; ext: string }): string;
};
Object.defineProperty(window, 'twemoji', {
    value: {
        parse: str => str,
    },
});

src/typings.d.ts

declare var twemoji: {
    parse(str: string, options?: { folder: string; ext: string }): string;
};

但这似乎并没有解决问题,因为我仍然得到错误 TS2304。我似乎得到了一些不一致的行为,因为它有时似乎有效?期待 solution/explanation :)

想法:

  1. 尝试安装types for twemoji
  2. 基本上 src/typings.d.ts 应该可以。它是否包含在编译过程中?它是否列在您的测试 tsconfig filesinclude 部分?你可以看看运行tsc --project=tsconfig.spec.json --outDir=output --listFiles
  3. 尝试将 typings.d.ts 中的代码更改为:
    declare module 'twemoji' {
      export function parse(str: string, options?: { folder: string; ext: string }): string;
    }