相关疑难解决方法(0)

在异步componentDidMount时使用React的Jest和Enzyme进行测试

  • 反应:16.3.0-alpha.1
  • 开玩笑:"22.3.0"
  • 酶:3.3.0
  • 打字稿: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;
   }
}
Run Code Online (Sandbox Code Playgroud)

测试:

describe('...', () => {
  test('...', async () => {
    const wrapper = shallow(<Foo/>);
    const instance = await wrapper.instance();
    expect(instance.bar).toBe(100);
  });
});
Run Code Online (Sandbox Code Playgroud)

错误:

Expected value to be:
  100
Received:
  0
Run Code Online (Sandbox Code Playgroud)

typescript reactjs jestjs enzyme

11
推荐指数
2
解决办法
8901
查看次数

标签 统计

enzyme ×1

jestjs ×1

reactjs ×1

typescript ×1