This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
82 lines (65 loc) · 1.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const fs = require( 'fs' );
const JestRunner = require( 'jest-runner' );
const { parse } = require( 'jest-docblock' );
const TestRunner = Object.prototype.hasOwnProperty.call( JestRunner, 'default' ) ? JestRunner.default : JestRunner;
const ARG_PREFIX = '--group=';
class GroupRunner extends TestRunner {
static getGroups( args ) {
const include = [];
const exclude = [];
args.forEach( ( arg ) => {
if ( arg.startsWith( ARG_PREFIX ) ) {
const group = arg.substring( ARG_PREFIX.length );
if ( group.startsWith( '-' ) ) {
exclude.push( group.substring( 1 ) );
} else {
include.push( group );
}
}
} );
return {
include,
exclude,
};
}
static filterTest( { include, exclude }, test ) {
let found = include.length === 0;
const parsed = parse( fs.readFileSync( test.path, 'utf8' ) );
if ( parsed.group ) {
const parsedGroup = Array.isArray( parsed.group ) ? parsed.group : [parsed.group];
for ( let i = 0, len = parsedGroup.length; i < len; i++ ) {
if ( typeof parsedGroup[i] === 'string' ) {
if ( exclude.find( ( group ) => parsedGroup[i].startsWith( group ) ) ) {
found = false;
break;
}
if ( include.find( ( group ) => parsedGroup[i].startsWith( group ) ) ) {
found = true;
}
}
}
}
return found;
}
runTests( tests, watcher, onStart, onResult, onFailure, options ) {
const groups = GroupRunner.getGroups( process.argv );
groups.include.forEach( ( group ) => {
if ( groups.exclude.includes( group ) ) {
return;
}
const name = group.replace( /\W/g, '_' ).toUpperCase();
process.env[`JEST_GROUP_${ name }`] = '1';
} );
return super.runTests(
groups.include.length > 0 || groups.exclude.length > 0
? tests.filter( ( test ) => GroupRunner.filterTest( groups, test ) )
: tests,
watcher,
onStart,
onResult,
onFailure,
options,
);
}
}
module.exports = GroupRunner;