eugene-manuilov / jest-runner-groups

A custom runner that allows to tag test files and run groups of tests with Jest.
MIT License
128 stars 14 forks source link

Is there a way to determine what group has been passed in? #40

Closed worldomonation closed 2 years ago

worldomonation commented 2 years ago

Hi 👋 Thank you for an excellent plugin that solves a real problem for many folks.

I have a question about whether it's possible to tell (at a test file level) what group was specified when the command was given. The kind of problem I am attempting to solve is as follows:


yarn jest --group 'FeatureA'

/**
* @group FeatureA
* @group FeatureB
**/

describe('Test for Feature', function() {
  const username = group === 'FeatureA' ? 'featureAUser' : 'featureBUser'; <-- this part
});

In short, a test file may be shared between two or more groups that follow the same steps in order to reduce duplication in the test code. However, the test user to be used differ depending on the group selected.

Thanks and I appreciate any reply or pointers!

eugene-manuilov commented 2 years ago

Hi @worldomonation.

I have just published a tiny update that adds this information to test files using environment variables. In your case you can check whether the process.env.JEST_GROUP_FEATUREA is truthy:

/**
* @group FeatureA
* @group FeatureB
**/

describe('Test for Feature', function() {
  const username = !! process.env.JEST_GROUP_FEATUREA ? 'featureAUser' : 'featureBUser';
});

Install the latest version of jest-runner-groups (2.2.0) and let me know if it helps to solve your problem.

worldomonation commented 2 years ago

@eugene-manuilov I appreciate your really quick turnaround on this question!

I pulled 2.2.0 and tried it out and it works exactly like I would expect. Thank you!

eugene-manuilov commented 2 years ago

Awesome! Gald it works.