Jest equivalent to RSpec lazy evaluated variables (let)?

The best solutions I’ve found have been libraries like

https://github.com/stalniy/bdd-lazy-var

and

https://github.com/tatyshev/given2

If you don’t want to introduce a dependency, you can get similar behavior (albeit without lazy evaluation) by doing something like this:

beforeEach(() => {
  let input="foo";
  beforeEach(() => {
    setupSomeThing(input);
  });

  describe('when input is bar', () => {
    beforeAll(() => {
     input="bar";
    });

    it('does something different', () => {

    });
  });

  describe('when input is baz', () => {
    beforeAll(() => {
     input="baz";
    });        

    it('does something different', () => {

    });
  });
});

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)