@@ -6,6 +6,7 @@ const testSpecified = process.argv[2];
6
6
let totalFilesTested = 0 ;
7
7
let totalTestsPassed = 0 ;
8
8
let totalTestsFailed = 0 ;
9
+ const tests = [ ] ;
9
10
10
11
function escapeRegExp ( s ) {
11
12
return s . replace ( / [ - [ \] { } ( ) * + ? . , \\ ^ $ | # \s ] / g, '\\$&' ) ;
@@ -14,7 +15,7 @@ function escapeRegExp(s) {
14
15
function arrContains ( list , val ) {
15
16
let isContains = false ;
16
17
17
- list . forEach ( ( ele ) => {
18
+ list . forEach ( ele => {
18
19
let eleRegExp ;
19
20
20
21
if ( typeof ele === 'string' ) {
@@ -37,15 +38,18 @@ function isDir(dirPath) {
37
38
}
38
39
39
40
function fileScannerSync ( args ) {
40
- if ( fs . existsSync ( args . path ) && ! arrContains ( args . excludeList , path . basename ( args . path ) ) ) {
41
+ if (
42
+ fs . existsSync ( args . path ) &&
43
+ ! arrContains ( args . excludeList , path . basename ( args . path ) )
44
+ ) {
41
45
if ( ! isDir ( args . path ) ) {
42
46
args . callback ( path . normalize ( args . path ) , false ) ;
43
47
return ;
44
48
}
45
49
46
50
const dirList = fs . readdirSync ( args . path ) ;
47
51
48
- dirList . forEach ( ( item ) => {
52
+ dirList . forEach ( item => {
49
53
if ( ! arrContains ( args . excludeList , path . basename ( item ) ) ) {
50
54
const relativePath = path . join ( args . path , item ) ;
51
55
@@ -66,14 +70,14 @@ function fileScannerSync(args) {
66
70
}
67
71
68
72
function isEqual ( a , b ) {
69
- if ( typeof a !== 'object' && typeof b !== ' object' ) {
73
+ if ( typeof a !== 'object' && typeof b !== 'object' ) {
70
74
return a === b ;
71
75
}
72
76
73
77
var aProps = Object . getOwnPropertyNames ( a ) ;
74
78
var bProps = Object . getOwnPropertyNames ( b ) ;
75
79
76
- if ( aProps . length != bProps . length ) {
80
+ if ( aProps . length !== bProps . length ) {
77
81
return false ;
78
82
}
79
83
@@ -113,12 +117,12 @@ function runTest(test, func) {
113
117
114
118
function displayTestSuite ( tSuite ) {
115
119
process . stdout . write ( `\nTest: ${ tSuite . fileName } => ${ tSuite . funcName } \n\n` ) ;
116
- tSuite . tests . forEach ( ( test ) => {
120
+ tSuite . tests . forEach ( test => {
117
121
if ( test . hasPassed ) {
118
122
process . stdout . write (
119
- `Test: ${ test . desc } \u2713 | ${ test . args . join ( ', ' ) } => ${ test . exp } in ${
120
- test . timeTaken
121
- } ms\n`,
123
+ `Test: ${ test . desc } \u2713 | ${ test . args . join ( ', ' ) } => ${
124
+ test . exp
125
+ } in ${ test . timeTaken } ms\n`,
122
126
) ;
123
127
} else {
124
128
process . stdout . write (
@@ -135,45 +139,69 @@ function displayTestSuite(tSuite) {
135
139
) ;
136
140
}
137
141
138
- function displayFinalStats ( totalFilesTested , totalTestsPassed , totalTestsFailed ) {
142
+ function displayFinalStats (
143
+ totalFilesTested ,
144
+ totalTestsPassed ,
145
+ totalTestsFailed ,
146
+ ) {
139
147
process . stdout . write ( `\nTotal files tested: ${ totalFilesTested } \n\n` ) ;
140
- process . stdout . write ( `Total tests ran: ${ totalTestsPassed + totalTestsFailed } \n` ) ;
148
+ process . stdout . write (
149
+ `Total tests ran: ${ totalTestsPassed + totalTestsFailed } \n` ,
150
+ ) ;
141
151
process . stdout . write ( `Passed: ${ totalTestsPassed } \n` ) ;
142
152
process . stdout . write ( `Failed: ${ totalTestsFailed } \n` ) ;
143
153
}
144
154
155
+ function getImports ( pathFile ) {
156
+ const importTobeTested = require ( pathFile ) ;
157
+ let funcsToBeTested = [ ] ;
158
+
159
+ if ( typeof importTobeTested === 'function' ) {
160
+ funcsToBeTested . push ( importTobeTested ) ;
161
+ } else if ( Array . isArray ( importTobeTested ) ) {
162
+ funcsToBeTested = Object . assign ( [ ] , importTobeTested ) ;
163
+ } else {
164
+ throw `${ tSuite . fileName } doesn't export a function or array or functions` ;
165
+ }
166
+
167
+ return funcsToBeTested ;
168
+ }
169
+
145
170
fileScannerSync ( {
146
171
path : '.' ,
147
172
recursive : true ,
148
173
excludeList : [ 'tests' , '.git' ] ,
149
174
callback : ( relativePath , isDir ) => {
150
- if ( ! isDir ) {
151
- const tests = [ ] ;
152
- const tSuite = {
153
- fileName : '' ,
154
- funcName : '' ,
155
- tests : [ ] ,
156
- } ;
157
-
158
- if (
159
- path . extname ( relativePath ) === '.js' &&
160
- ( testSpecified === undefined || relativePath . indexOf ( testSpecified ) !== - 1 )
161
- ) {
162
- const testPath = path . join ( path . dirname ( relativePath ) , 'test.json' ) ;
163
-
164
- if ( fs . existsSync ( testPath ) ) {
165
- totalFilesTested ++ ;
166
- const funcToBeTested = require ( path . join ( '..' + path . sep , relativePath ) ) ;
167
-
168
- if ( ! tests [ testPath ] ) {
169
- tests [ testPath ] = JSON . parse ( fs . readFileSync ( testPath , 'utf-8' ) ) ;
170
- }
171
-
172
- tSuite . fileName = path . basename ( relativePath ) ;
173
- tSuite . funcName = funcToBeTested . name ;
174
-
175
- tests [ testPath ] . tests . forEach ( ( test ) => {
176
- const testRes = runTest ( test , funcToBeTested ) ;
175
+ if (
176
+ ! isDir &&
177
+ path . extname ( relativePath ) === '.js' &&
178
+ ( testSpecified === undefined ||
179
+ relativePath . indexOf ( testSpecified ) !== - 1 )
180
+ ) {
181
+ const testPath = path . join ( path . dirname ( relativePath ) , 'test.json' ) ;
182
+
183
+ if ( fs . existsSync ( testPath ) ) {
184
+ totalFilesTested ++ ;
185
+
186
+ const tSuite = {
187
+ fileName : '' ,
188
+ funcName : '' ,
189
+ tests : [ ] ,
190
+ } ;
191
+
192
+ tSuite . fileName = path . basename ( relativePath ) ;
193
+ const pathImport = path . join ( '..' + path . sep , relativePath ) ;
194
+ const funcsToBeTested = getImports ( pathImport ) ;
195
+
196
+ if ( ! tests [ testPath ] ) {
197
+ tests [ testPath ] = JSON . parse ( fs . readFileSync ( testPath , 'utf-8' ) ) ;
198
+ }
199
+
200
+ funcsToBeTested . forEach ( ( func , index ) => {
201
+ tSuite . funcName = func . name ;
202
+
203
+ tests [ testPath ] . tests . forEach ( test => {
204
+ const testRes = runTest ( test , func ) ;
177
205
tSuite . tests . push ( Object . assign ( test , testRes ) ) ;
178
206
179
207
if ( testRes . hasPassed ) {
@@ -183,7 +211,8 @@ fileScannerSync({
183
211
}
184
212
} ) ;
185
213
displayTestSuite ( tSuite ) ;
186
- }
214
+ tSuite . tests = [ ] ;
215
+ } ) ;
187
216
}
188
217
}
189
218
} ,
0 commit comments