我正在測試使用i18next進行國際化的React組件。Typescript,Jest和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");
});
})
我懷疑是玩笑被磕碰i18next.t()
返回undefined
,而不是,但我不能確定的說: 「你好,世界!」 。
我試圖unmock("i18next")
沒有任何運氣。
如何在Jest中爲i18next組件編寫測試?
更新:我正在使用Typescript作爲我的編譯器。看起來有一個hoisting issue與用於Typescript的裝載機(ES6提升機進口,jest.unmock
不是 - Babel用戶有一個插件來處理這個)。
你可以添加一個''的console.log文件,只是檢查問題是否真的與'i18next'相關。 –