码:
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:使用安装座(不浅)。
3:等待异步组件生命周期。
例如:
test(' ',async () => { const wrapper = mount( <Foo /> ); await wrapper.instance().componentDidMount(); })