9
9
10
10
import type { Environment } from 'types/Environment' ;
11
11
12
- import util from 'util' ;
13
- import chalk from 'chalk' ;
14
- import pretty from 'pretty-format' ;
15
-
16
- type Table = Array < Array < any >> ;
17
-
18
- const EXPECTED_COLOR = chalk . green ;
19
- const RECEIVED_COLOR = chalk . red ;
12
+ import { bind as bindEach } from 'jest-each' ;
20
13
21
14
export default ( environment : Environment ) : void => {
22
15
environment . global . it . each = bindEach ( environment . global . it ) ;
@@ -26,74 +19,3 @@ export default (environment: Environment): void => {
26
19
environment . global . xdescribe . each = bindEach ( environment . global . xdescribe ) ;
27
20
environment . global . fdescribe . each = bindEach ( environment . global . fdescribe ) ;
28
21
} ;
29
-
30
- const bindEach = ( cb : Function ) => ( ...args : any ) => (
31
- title : string ,
32
- test : Function ,
33
- ) : void => {
34
- if ( args . length === 1 ) {
35
- const table : Table = args [ 0 ] ;
36
- return table . forEach ( row =>
37
- cb ( util . format ( title , ...row ) , applyRestParams ( row , test ) ) ,
38
- ) ;
39
- }
40
-
41
- const templateStrings = args [ 0 ] ;
42
- const data = args . slice ( 1 ) ;
43
-
44
- const keys = getHeadingKeys ( templateStrings [ 0 ] ) ;
45
- const table = buildTable ( data , keys . length , keys ) ;
46
-
47
- if ( data . length % keys . length !== 0 ) {
48
- return cb ( title , ( ) => {
49
- throw new Error (
50
- 'Not enough arguments supplied for given headings:\n' +
51
- EXPECTED_COLOR ( keys . join ( ' | ' ) ) +
52
- '\n\n' +
53
- 'Received:\n' +
54
- RECEIVED_COLOR ( pretty ( data ) ) +
55
- '\n\n' +
56
- `Missing ${ RECEIVED_COLOR ( `${ data . length % keys . length } ` ) } arguments` ,
57
- ) ;
58
- } ) ;
59
- }
60
-
61
- return table . forEach ( row =>
62
- cb ( interpolate ( title , row ) , applyObjectParams ( row , test ) ) ,
63
- ) ;
64
- } ;
65
-
66
- const applyRestParams = ( params : Array < any > , test : Function ) => {
67
- if ( params . length < test . length ) return done => test ( ...params , done ) ;
68
-
69
- return ( ) => test ( ...params ) ;
70
- } ;
71
-
72
- const getHeadingKeys = ( headings : string ) : Array < string > =>
73
- headings . replace ( / \s / g, '' ) . split ( '|' ) ;
74
-
75
- const buildTable = (
76
- data : Array < any > ,
77
- rowSize : number ,
78
- keys : Array < string > ,
79
- ) : Array < any > =>
80
- Array . from ( { length : data . length / rowSize } )
81
- . map ( ( _ , index ) => data . slice ( index * rowSize , index * rowSize + rowSize ) )
82
- . map ( row =>
83
- row . reduce (
84
- ( acc , value , index ) => Object . assign ( { } , acc , { [ keys [ index ] ] : value } ) ,
85
- { } ,
86
- ) ,
87
- ) ;
88
-
89
- const interpolate = ( title : string , data : any ) =>
90
- Object . keys ( data ) . reduce (
91
- ( acc , key ) => acc . replace ( '$' + key , data [ key ] ) ,
92
- title ,
93
- ) ;
94
-
95
- const applyObjectParams = ( obj : any , test : Function ) => {
96
- if ( test . length > 1 ) return done => test ( obj , done ) ;
97
-
98
- return ( ) => test ( obj ) ;
99
- } ;
0 commit comments