stalniy/bdd-lazy-var

Does not work with Jest `describe.each`

UberMouse opened this issue · 4 comments

I have a spec that makes use of the describe.each functionality introduced in Jest v23. As soon as I add a call to def to introduce a variable with bdd-lazy-var it causes an error describe.each is not a function in that test file. Seems like bdd-lazy-var is doing something to interfere with the function but I wouldn't know what.

I'm using the latest version of bdd-lazy-var and Typescript 3.1.6

Thanks for the issue. Indeed, bdd-lazy-var wraps describe statements in order to track suites.
I’ll try to add a bit smarter to logic for features like this one.

The issue appears to be here: https://github.com/stalniy/bdd-lazy-var/blob/master/index.js#L578

I've been able to workaround this issue by adding the original keys to the new context, for example:

['', 'x', 'f'].forEach(function (prefix) {
      var describeKey = prefix + 'describe';
      var itKey = prefix + 'it';
      const originalIt = context[itKey];
      const originalDescribe = context[describeKey];

      context[itKey + 's'] = wrapIts(context[itKey]);
      context[itKey] = wrapIt(context[itKey], isJest);
      Object.keys(originalIt).forEach(key => { context[itKey][key] = originalIt[key]; });
      context[describeKey] = tracker.wrapSuite(context[describeKey]);
      Object.keys(originalDescribe).forEach(key => { context[describeKey][key] = originalDescribe[key]; });
      context[prefix + 'context'] = context[describeKey];
    });

Although this is not the best solution, this is a temporary workaround until this issue can be fixed. This now works with jest-preset-angular package which also wraps the describe functions.

@nzacca Unfortunately, it's not so simple. If you do like this, you will have describe.each and test.each but bdd-lazy-var won't be able to track dynamically created describe statements in describe.each and therefore you will not be able to define lazy vars inside describe.each.

To make it work properly with bdd-lazy-var, you will need to use jest-each package to create own describe.each based on patched describe (by this lib) function

@stalniy Thanks for the response! I agree, what I proposed doesn't work for the describe.each. It was meant just as a workaround so that I could get tests to run with the jest-preset-angular package. I am not using describe.each but was still seeing errors show up because of the missing method.

I'm not using describe.each for this project at this moment which allows me to use this workaround but we will definitely need a better solution in the long term.