Jest - import multiple tests in a describe block, reusing variables defined in beforeEach()

You can simply move the shared tests into a function that does the it() calls.

class Octocat {
  get length() {
    return 3;
  }

  get canSmile() {
    return true;
  }
}

class GrumpyCat {
  get length() {
    return 1;
  }

  get canSmile() {
    return false;
  }
}

const behavesLikeAPet = (pet) => {
  it('is small', () => expect(pet.length).toBeLessThan(5));
  it('can smile', () => expect(pet.canSmile).toEqual(true));
};

describe('Famous animals', () => {
  describe('Octocat', () => {
    behavesLikeAPet(new Octocat());
  });

  describe('GrumpyCat', () => {
    behavesLikeAPet(new GrumpyCat());
  });
});

You will get detailed output for every it test:

Famous animals
  Octocat
    ✓ is small (2ms)
    ✓ can smile (1ms)
  GrumpyCat
    ✓ is small
    ✕ can smile (2ms)

If you still want beforeEach,

for reasons ... it works if you declare your variable in the global scope

let petSetInBefore; // here it works
describe('Famous animals', () => {
  //let petSetInBefore; // here it's undefined

  describe('Octocat', ()  => {
    //let petSetInBefore; // undefined too

    beforeAll(() => {
      petSetInBefore = new Octocat();
    })

    cutenessTests() // .bind(this) results the same
  });

  describe('Doge', () => {
    beforeEach(() => {
      petSetInBefore = new Doge();
    })

    cutenessTests.bind(this)()
  });
})

https://repl.it/@gui3/jestSharedTests

seems like the tests inside the shared function cannot share variables from beforeEach otherwise ...


Jest has describe.each(table) which I haven't seen being used a lot, but it's really helpful for reusing tests which have common/same results.

In case for identical expectations for both of the test subjects you can do it like this:

const aCutePet = pet => {
  it("should be small", () => {
    expect(pet.size).toBeLessThan(10);
  });

  it(`should be able to smile`, () => {
    expect(pet).toHaveProperty('can_smile', true)
  });
}

describe.each([
  [new Doge],
  [new Octocat]
])("The %O", aCutePet);

The output:

  The Doge { size: 3, can_smile: true }
    ✓ should be small (1ms)
    ✓ should be able to smile (1ms)
  The Octocat { size: 5, can_smile: true }
    ✓ should be small
    ✓ should be able to smile (1ms)