Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit 8d32f82

Browse files
Added group names to the environment variables.
1 parent f478710 commit 8d32f82

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [Unreleased] - TBD
44

5+
- Added information about the currently running groups to environment variables.
56
- Updated dependencies to the latest versions.
67

78
## [v2.1.0] (2021-05-27)

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ If you want to exclude a subgroup from being executed, add minus character to th
109109
jest --group=foo --group=-foo/baz
110110
```
111111

112+
### Knowing which gruops are running
113+
114+
When you run your tests using jest-runner-groups, you can check which group is currently running by checking the current process environment variables. This can be handy if you want to use different fixtures for different groups or skip a certain functionality for a specific group.
115+
116+
Each group is added with the `JEST_GROUP_` prefix and all non-word characters in the group name are replaced with underscores. For example, if you run the following command:
117+
118+
```sh-session
119+
npm test -- --group=unit/classes --group=unit/services
120+
```
121+
122+
Then you can check groups in your jest tests:
123+
124+
```js
125+
/**
126+
* Admin dashboard tests
127+
*
128+
* @group unit/classes
129+
* @group unit/services
130+
* @group unit/utility
131+
*/
132+
133+
it( '...', () => {
134+
expect( process.env.JEST_GROUP_UNIT_CLASSES ).toBeTruthy();
135+
expect( process.env.JEST_GROUP_UNIT_SERVICES ).toBeTruthy();
136+
expect( process.env.JEST_GROUP_UNIT_UTILITY ).not.toBeTruthy();
137+
} );
138+
```
139+
112140
## Contribute
113141

114142
Want to help or have a suggestion? Open a [new ticket](https://github.com/eugene-manuilov/jest-runner-groups/issues/new) and we can discuss it or submit a pull request.

index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,22 @@ class GroupRunner extends TestRunner {
5353
return found;
5454
}
5555

56-
static filterTests( args, tests ) {
57-
const groups = GroupRunner.getGroups( args );
58-
return groups.include.length > 0 || groups.exclude.length > 0
59-
? tests.filter( ( test ) => GroupRunner.filterTest( groups, test ) )
60-
: tests;
61-
}
62-
6356
runTests( tests, watcher, onStart, onResult, onFailure, options ) {
57+
const groups = GroupRunner.getGroups( process.argv );
58+
59+
groups.include.forEach( ( group ) => {
60+
if ( groups.exclude.includes( group ) ) {
61+
return;
62+
}
63+
64+
const name = group.replace( /\W/g, '_' ).toUpperCase();
65+
process.env[`JEST_GROUP_${ name }`] = '1';
66+
} );
67+
6468
return super.runTests(
65-
GroupRunner.filterTests( process.argv, tests ),
69+
groups.include.length > 0 || groups.exclude.length > 0
70+
? tests.filter( ( test ) => GroupRunner.filterTest( groups, test ) )
71+
: tests,
6672
watcher,
6773
onStart,
6874
onResult,

tests/valid-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
describe( 'Valid Test', () => {
88
it( 'should always run', () => {
99
expect( 1 ).toBe( 1 );
10+
expect( process.env.JEST_GROUP_VALID ).not.toBeFalsy();
1011
} );
1112
} );

0 commit comments

Comments
 (0)