- react :16.3.0-alpha.1
- 开 Jest :“22.3.0”
- enzyme :3.3.0
- typescript :2.7.1
代码:
class Foo extends React.PureComponent<undefined,undefined>{
bar:number;
async componentDidMount() {
this.bar = 0;
let echarts = await import('echarts'); // async import
this.bar = 100;
}
}
测试:
describe('...', () => {
test('...', async () => {
const wrapper = shallow(<Foo/>);
const instance = await wrapper.instance();
expect(instance.bar).toBe(100);
});
});
错误:
Expected value to be:
100
Received:
0
最佳答案
解决方法:
1:使用异步/等待语法。
2:使用mount(不浅)
3:WAITING异步组件生命周期。
例如:
test(' ',async () => {
const wrapper = mount(
<Foo />
);
await wrapper.instance().componentDidMount();
})
关于reactjs - 在异步 componentDidMount 时使用 React 的 Jest 和 Enzyme 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49419961/