jeudi 25 février 2021

is it a good idea to use before for multiple tests?

I have a class, which has multiple methods. I am looking at the test cases written by someone.

The tests look like this..

describe('this tests example class', () => {
   let classInstance;
   before(() => {
     classInstance = new ExampleClass();
   })

   describe("tests 1", () => {
      it("first test", () => {
    
      })

      it("second test", () => {
    
      })

   })

   describe("tests 2", () => {
      it("third test", () => {
    
      })

      it("fourth test", () => {
    
      })

   })

}

let's say ExampleClass has a function func1 which has a parameter number and it pushes this number in the global array of its own.

Now first test calls func1 and it passes 5 as a number. Now, first test does something else too, but nothing important. We can look at the second test now, which checks if the ExampleClass's array has the number 5.

So, the way this works now is first test puts something on the global classInstance and second test checks if it's there..

The question: Does these kind of tests ever make sense at all ? I think good approach would be to have beforeEach and each test would work on the new instance of classInstance, but i think that this would cause more code for the test cases. Would appreciate your comment on this.

Aucun commentaire:

Enregistrer un commentaire