2017-04-20 125 views
0

我正在測試使用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用戶有一個插件來處理這個)。

+0

你可以添加一個''的console.log文件,只是檢查問題是否真的與'i18next'相關。 –

回答

3

不知道爲什麼它不工作,但你可以嘲笑把i18next是這樣的:(!「你好,世界」 T())

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

謝謝,這是有效的。看起來我的問題是使用Typescript編譯器。我已經更新了描述。 – Rick

相關問題