samedi 17 février 2018

Why use beforeEach() when writing javascript tests?

I'm learning how to write tests for my code and my instructor explained to put the component being rendered in a beforeEach() function like this...

describe('CommentBox', () => {
  let component;

  beforeEach(() => {
    component = renderComponent(CommentBox);
  });

  it('has the correct class', () => {
    expect(component).to.have.class('comment-box');
  });

  it('has a text area', () => {
    expect(component.find('textarea')).to.exist;
  });

  it('has a button', () => {
    expect(component.find('button')).to.exist;
  });
});

Why use beforeEach(). Why not just declare the component variable at the top of the describe function like so...

describe('CommentBox', () => {
  const component = renderComponent(CommentBox);


  it('has the correct class', () => {
    expect(component).to.have.class('comment-box');
  });

  it('has a text area', () => {
    expect(component.find('textarea')).to.exist;
  });

  it('has a button', () => {
    expect(component.find('button')).to.exist;
  });
});

This would seem to save a couple extra lines of code, and one less function you would have to write?

Aucun commentaire:

Enregistrer un commentaire