【发布时间】:2019-02-27 03:49:13
【问题描述】:
我正在尝试使用 jest 来测试我的 componentDidMount 方法:
componentDidMount() {
agent.Gatherings.getAll().then((result) => {
this.setState({ gatherings: result }) //no code coverage
}).catch((err) => {
this.setState({ gatherings: [] }) //no code coverage
})
}
但我的其他测试之一工作正常:
it('test gathering List is rendered', () => {
wrapper.setState({ gatherings: [TestGathering] })
expect(wrapper.find('MyList').length).toEqual(1);
});
我希望在我的测试中涵盖每一行。如何让我的 componentDidMount() 中的行 all 被开玩笑地测试?
更新,我将一个文件直接导入到测试文件中。我要导入的文件名为 agent.js。被遗漏的函数调用的代码是:
agent.js
export const requests = {
get: url => fetch(url).then(res => res.json()),
post: (url, body) =>
fetch(url, {
method: 'POST',
body: body,
headers: {
'Content-Type': 'application/json'
}
}).then(res => res.json()) //also this line lacks coverage
}
export const Gatherings = {
getAll: () =>
requests.get(API_ROOT + '/gatherings')
}
export default {
Gatherings
}
【问题讨论】:
标签: reactjs unit-testing react-native jestjs