首页 文章

Typescript,Jest和i18next:无法在React组件中看到文本

提问于
浏览
0

我正在测试一个使用i18next进行国际化的React组件 .

组件:

import * as React from "react";
import { t } from "i18next";

export function Hello(_props) {
  return <div className="widget-header">
    {t("Hello, world!")}
  </div>;
}

考试:

import * as React from "react";
import { render } from "enzyme";
import { Hello } from "./hello";

describe("<Hello />", () => {
  it("says hi", () => {
    let hi = render(<Hello />);
    // FAILS!
    // Expected "" to contain "Hello"
    expect(hi.text()).toContain("Hello");
  });
})

我的怀疑是,Jest正在追回 i18next.t() 而不是"Hello, world!",但我不确定 .

我试过没有任何运气 unmock("i18next") .

How do I write tests for i18next components in Jest?

UPDATE: 我使用的是Typescript作为我的编译器 . 似乎有hoisting issue与我用于Typescript的加载器(ES6升降机导入, jest.unmock 不是 - Babel用户有一个插件来处理这个) .

1 回答

  • 3

    不知道为什么它不起作用,但你可以像这样模拟放 i18next

    jest.mock('i18next', () => ({
      t: (i) => i
    }))
    

相关问题