code:
class Foo extends React.PureComponent<undefined,undefined>{
bar:number;
async componentDidMount() {
this.bar = 0;
let echarts = await import('echarts'); // async import
this.bar = 100;
}
}
test:
describe('...', () => {
test('...', async () => {
const wrapper = shallow(<Foo/>);
const instance = await wrapper.instance();
expect(instance.bar).toBe(100);
});
});
Error:
Expected value to be:
100
Received:
0
Solution:
1: use the async/await syntax.
2: Use mount (no shallow).
3: await async componentLifecycle.
For ex:
test(' ',async () => {
const wrapper = mount(
<Foo />
);
await wrapper.instance().componentDidMount();
})