2017-09-26 122 views
1

我有問題,如果我運行我的測試沒有--coverage選項一切都通過,但只要我運行jest--coverage選項我的一些測試失敗。Jest測試失敗只覆蓋

這裏是日誌:

Button › Primary button › should render a primary button 

    undefined:3:457: property missing ':' 

    at Object.it (src/components/Buttons/Button/Button.test.js:13:24) 
    at Promise.resolve.then.el (node_modules/p-map/index.js:46:16) 

而且測試:

it('should render a primary button',() => { 
    const props = { primary: true }; 

    const rendered = renderer.create(<Button { ...props }>Some Button</Button>).toJSON(); 
    expect(rendered).toMatchSnapshot(); 
}); 

出現故障的線路是expect(rendered).toMatchSnapshot();

任何人任何想法?

回答

0

我修復了這個問題。但我認爲這是一個非常具體的問題。

我們使用風格的組成部分,我做了這樣的事情:

const BaseButton = styled.button` 
    ${ noTouchDevice && ` 
    :hover { 
     cursor: ${ ({ disabled }) => (disabled ? 'not-allowed' : 'initial') }; 
    } 
    ` } 
`; 

這是自嵌套模板文字不一樣外一個風格的組分處理不工作。這意味着如果樣式化組件在外部字符串文字中找到一個函數,它將調用它並傳遞組件的道具作爲參數。但是嵌套模板文字不會像這樣處理。

這就是爲什麼在快照中我得到了這樣的事情:的

.c0:hover { 
    cursor: ({ disabled }) => disabled ? 'not-allowed' :'initial'; 
} 

代替

.c0:hover { 
    cursor: not-allowed; 
} 

只需運行測試工作得很好,但覆蓋集合失敗有比較快照。

所以我乾脆改爲只使用三元運算符一樣

const BaseButton = styled.button` 
    ${ noTouchDevice && ` 
    :hover { 
     cursor: ${ disabled ? 'not-allowed' : 'initial' }; 
    } 
    ` } 
`; 

我的代碼,它工作得很好。

我仍然不知道的是,爲什麼錯誤只發生在收集報道時。說實話,我並不想投入更多的時間,但我認爲這與當jess/istanbul收集覆蓋範圍時如何調用測試有關。