stalniy / bdd-lazy-var

Provides UI for testing frameworks such as mocha, jasmine and jest which allows to define lazy variables and subjects.
MIT License
162 stars 14 forks source link

Subject not correctly defined in child contexts #30

Closed iain-b closed 6 years ago

iain-b commented 6 years ago

I'm trying to upgrade from 1.3.1 to 2.1.2 and I'm having some issues. This is similar to an issue I created earlier but I'll leave it to the maintainer to determine if it's a duplicate or not.

It seems that if I inherit from the subject in one context this function is applied again in a child of this context. I've put comments on an example below but the crux is that if I define a subject in a context subject(() => subject().foo) then in child contexts which don't explicitly define the subject it seems it is defined as subject().foo.foo.

I'm happy to help out with a fix but I'd need some pointers to where I might look in the code.

describe('testing 2', () => {
    subject(() => ({ ...get('props') }));

    def('props', () => ({
      numberOfRows: get('numberOfRows'),
      header: get('header'),
    }));
    def('numberOfRows', 1);
    def('header', 'asdfasdf');

    context('the rows', () => {
      subject(() => subject().numberOfRows);

      // fails: expected undefined to deeply equal 0
      context('where there are now rows', () => {
        def('numberOfRows', 0);
        // behaves as if I have defined subject _again_ as
        // subject(() => subject().numberOfRows);
        // i.e. subject().numberOfRows.numberOfRows ?
        // defining the subject as follows fixes this
        // subject(() => subject());

        it('renders no rows', () => {
          expect(subject()).to.eql(0);
        });
      });

      // passes
      context('where there is one row ', () => {
        subject(() => subject()); // without this it fails
        def('numberOfRows', 1);
        // commenting out the above, the test passes as seemingly the subject is not redefined in
        // that case.

        it('renders the correct rows', () => {
          expect(subject()).to.eql(1);
        });
      });

      // fails: expected undefined to deeply equal 3
      context('where there are multiple rows', () => {
        def('numberOfRows', 3);

        it('renders multiple rows', () => {
          expect(subject()).to.eql(3);
        });
      });
    });
  });
stalniy commented 6 years ago

Ok, I found where the issue is. Will try to fix it today.

Thanks for testing!

stalniy commented 6 years ago

@iain-b could you please retest master branch? I tested your cases now should work as expected

Update: I will make a release if it works for you

iain-b commented 6 years ago

Tested and can confirm the fixes are good 👍 With a couple of small fixes on my side I was able to upgrade. A release would be great, Thanks!

stalniy commented 6 years ago

Cool! I'll publish new version on Sun.

stalniy commented 6 years ago

just published 2.2.0

UPDATE: release notes can be found here https://github.com/stalniy/bdd-lazy-var/releases/tag/v2.2.0

iain-b commented 6 years ago

Upgraded 👍 Thank you!