Javascript 在函数内的何处装载和卸载?

Javascript 在函数内的何处装载和卸载?,javascript,reactjs,enzyme,Javascript,Reactjs,Enzyme,我正试图掌握在React项目中测试组件的诀窍。到目前为止,我在一个组件上有一个测试文件,我正试图将此文件准备为一个包含多个测试的测试套件 import React from 'react'; import Enzyme, { mount } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; import HamburgerIcon from './HamburgerIcon'; Enzyme.configure({ ad

我正试图掌握在React项目中测试组件的诀窍。到目前为止,我在一个组件上有一个测试文件,我正试图将此文件准备为一个包含多个测试的测试套件

import React from 'react';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import HamburgerIcon from './HamburgerIcon';

Enzyme.configure({ adapter: new Adapter() });

describe('<HamburgerIcon />', () => {

  const hamburgerIcon = mount(<HamburgerIcon showOverlay={showOverlay} />);

  it('displays on mobile', () => {
     ...
     ...
  });

  it('has class .open after click', () => {
    ...
    ...
  });

  hamburgerIcon.unmount();

});
…测试通过了


不过,这似乎有些过分。如果我的测试套件有十个测试功能呢?每次测试我都应该这样安装和卸载吗?

您可以使用每次测试前后的功能来设置和清除测试

afterEach(() => {
    //do the unmounting and other stuff here
    //this will be called after each test case
});

beforeEach(() => {
    //do the mounting and setting up the test case here
    //this will be called before each test case
});
afterEach(() => {
    //do the unmounting and other stuff here
    //this will be called after each test case
});

beforeEach(() => {
    //do the mounting and setting up the test case here
    //this will be called before each test case
});