8
8
9
9
'use strict' ;
10
10
11
+ const H = require ( 'jest-haste-map/src/constants' ) ;
11
12
const Test = require ( './Test' ) ;
12
13
13
14
const createHasteMap = require ( './lib/createHasteMap' ) ;
@@ -16,6 +17,7 @@ const getCacheFilePath = require('jest-haste-map').getCacheFilePath;
16
17
const mkdirp = require ( 'mkdirp' ) ;
17
18
const path = require ( 'path' ) ;
18
19
const promisify = require ( './lib/promisify' ) ;
20
+ const resolveNodeModule = require ( './lib/resolveNodeModule' ) ;
19
21
const utils = require ( 'jest-util' ) ;
20
22
const workerFarm = require ( 'worker-farm' ) ;
21
23
@@ -55,13 +57,18 @@ class TestRunner {
55
57
}
56
58
}
57
59
58
- this . _resolver = createHasteMap ( config , {
60
+ this . _hasteMap = createHasteMap ( config , {
59
61
maxWorkers : options . runInBand ? 1 : this . _opts . maxWorkers ,
60
62
resetCache : ! config . cache ,
61
63
} ) ;
62
64
63
65
// warm-up and cache mocks
64
- this . _resolver . build ( ) :
66
+ console . time ( 'build' ) ;
67
+ this . _hasteMap . build ( ) . then ( data => {
68
+ console . timeEnd ( 'build' ) ;
69
+ console . log ( 'files' , Object . keys ( data . files ) . length ) ;
70
+ console . log ( 'modules' , Object . keys ( data . map ) . length ) ;
71
+ } ) ;
65
72
66
73
this . _testPathDirsRegExp = new RegExp (
67
74
config . testPathDirs
@@ -84,7 +91,7 @@ class TestRunner {
84
91
}
85
92
86
93
_getAllTestPaths ( ) {
87
- return this . _resolver
94
+ return this . _hasteMap
88
95
. matchFiles ( this . _config . testDirectoryName )
89
96
. then ( paths => paths . filter ( path => this . isTestFilePath ( path ) ) ) ;
90
97
}
@@ -107,15 +114,15 @@ class TestRunner {
107
114
const visitedModules = new Set ( ) ;
108
115
while ( changed . size ) {
109
116
changed = new Set ( moduleMap . filter ( module => (
110
- ! visitedModules . has ( module . path ) &&
117
+ ! visitedModules . has ( module . file ) &&
111
118
module . dependencies . some ( dep => dep && changed . has ( dep ) )
112
119
) ) . map ( module => {
113
- const path = module . path ;
114
- if ( this . isTestFilePath ( path ) ) {
115
- relatedPaths . add ( path ) ;
120
+ const file = module . file ;
121
+ if ( this . isTestFilePath ( file ) ) {
122
+ relatedPaths . add ( file ) ;
116
123
}
117
- visitedModules . add ( path ) ;
118
- return module . name ;
124
+ visitedModules . add ( file ) ;
125
+ return module . file ;
119
126
} ) ) ;
120
127
}
121
128
return relatedPaths ;
@@ -125,102 +132,49 @@ class TestRunner {
125
132
if ( ! changedPaths . size ) {
126
133
return Promise . resolve ( [ ] ) ;
127
134
}
135
+
128
136
const relatedPaths = new Set ( ) ;
129
- return this . _resolver . getAllModules ( ) . then ( allModules => {
137
+ return this . _hasteMap . build ( ) . then ( data => {
130
138
const changed = new Set ( ) ;
131
139
for ( const path of changedPaths ) {
132
140
if ( fileExists ( path ) ) {
133
- const module = this . _resolver . getModuleForPath ( path ) ;
141
+ const module = data . files [ path ] ;
134
142
if ( module ) {
135
- changed . add ( module . path ) ;
136
- if ( this . isTestFilePath ( module . path ) ) {
137
- relatedPaths . add ( module . path ) ;
143
+ changed . add ( path ) ;
144
+ if ( this . isTestFilePath ( path ) ) {
145
+ relatedPaths . add ( path ) ;
138
146
}
139
147
}
140
148
}
141
149
}
142
- return Promise . all ( Object . keys ( allModules ) . map ( path =>
143
- this . _resolver . getShallowDependencies ( path )
144
- . then ( response => ( {
145
- name : path ,
146
- path,
147
- dependencies : response . dependencies . map ( dep => dep . path ) ,
148
- } ) )
149
- ) ) . then ( moduleMap => Array . from ( this . collectChangedModules (
150
- relatedPaths ,
151
- moduleMap ,
152
- changed
153
- ) ) ) ;
154
- } ) ;
155
- }
156
-
157
- promiseHasteTestPathsRelatedTo ( changedPaths ) {
158
- if ( ! changedPaths . size ) {
159
- return Promise . resolve ( [ ] ) ;
160
- }
161
150
162
- return Promise . all ( [
163
- this . _getAllTestPaths ( ) ,
164
- this . _resolver . build ( ) ,
165
- ] ) . then ( response => {
166
- const testPaths = response [ 0 ] ;
167
- const hasteMap = response [ 1 ] ;
168
- const relatedPaths = new Set ( ) ;
169
- const changed = new Set ( ) ;
151
+ const platform = this . _config . haste . defaultPlatform ;
152
+ const extensions =
153
+ this . _config . moduleFileExtensions . map ( ext => '.' + ext ) ;
170
154
const moduleMap = [ ] ;
171
- testPaths . forEach ( path => {
172
- if ( changedPaths . has ( path ) && this . isTestFilePath ( path ) ) {
173
- relatedPaths . add ( path ) ;
174
- }
175
- moduleMap . push ( { name : path , path, dependencies : null } ) ;
176
- } ) ;
177
- const collectModules = list => {
178
- for ( const name in list ) {
179
- const path = list [ name ] ;
180
- if ( changedPaths . has ( path ) ) {
181
- changed . add ( name ) ;
182
- if ( this . isTestFilePath ( path ) ) {
183
- relatedPaths . add ( path ) ;
184
- }
155
+ for ( const file in data . files ) {
156
+ const fileData = data . files [ file ] ;
157
+ const dependencies = fileData [ H . DEPENDENCIES ] . map ( dep => {
158
+ const map = data . map [ dep ] ;
159
+ if ( data . map [ dep ] ) {
160
+ const module =
161
+ map [ platform ] || map [ H . GENERIC_PLATFORM ] ;
162
+ return module && module [ H . PATH ] ;
163
+ } else {
164
+ return resolveNodeModule ( dep , path . dirname ( file ) , extensions ) ;
185
165
}
186
- moduleMap . push ( { name, path, dependencies : null } ) ;
187
- }
188
- } ;
189
- collectModules ( hasteMap . modules ) ;
190
- collectModules ( hasteMap . mocks ) ;
191
-
192
- const deferreds = moduleMap . map ( ( ) => {
193
- let resolve ;
194
- const promise = new Promise ( _resolve => resolve = _resolve ) ;
195
- return { resolve, promise} ;
196
- } ) ;
197
- let i = 0 ;
198
- const nextResolution = ( ) => {
199
- if ( i >= moduleMap . length ) {
200
- return ;
201
- }
202
-
203
- const currentIndex = i ;
204
- const module = moduleMap [ currentIndex ] ;
205
- const deferred = deferreds [ currentIndex ] ;
206
- i ++ ;
207
- this . _resolver . getModuleForPath ( module . path ) . getDependencies ( )
208
- . then ( dependencies => {
209
- nextResolution ( ) ;
210
- moduleMap [ currentIndex ] . dependencies = dependencies ;
211
- } )
212
- . then ( ( ) => deferred . resolve ( ) ) ;
213
- } ;
214
-
215
- for ( let i = 0 ; i < 20 ; i ++ ) {
216
- nextResolution ( ) ;
166
+ } ) . filter (
167
+ dep => ! ! dep
168
+ ) ;
169
+ moduleMap . push ( { file, dependencies} ) ;
170
+ // should take moduleNameMapper into account
217
171
}
218
- return Promise . all ( deferreds . map ( deferred => deferred . promise ) )
219
- . then ( ( ) => Array . from ( this . collectChangedModules (
220
- relatedPaths ,
221
- moduleMap ,
222
- changed
223
- ) ) ) ;
172
+
173
+ return Array . from ( this . collectChangedModules (
174
+ relatedPaths ,
175
+ moduleMap ,
176
+ changed
177
+ ) ) ;
224
178
} ) ;
225
179
}
226
180
@@ -386,7 +340,7 @@ class TestRunner {
386
340
_createInBandTestRun ( testPaths , onTestResult , onRunFailure ) {
387
341
return testPaths . reduce ( ( promise , path ) =>
388
342
promise
389
- . then ( ( ) => this . _resolver . build ( ) )
343
+ . then ( ( ) => this . _hasteMap . build ( ) )
390
344
. then ( moduleMap => new Test ( path , this . _config , moduleMap ) . run ( ) )
391
345
. then ( result => onTestResult ( path , result ) )
392
346
. catch ( err => onRunFailure ( path , err ) ) ,
@@ -396,7 +350,7 @@ class TestRunner {
396
350
397
351
_createParallelTestRun ( testPaths , onTestResult , onRunFailure ) {
398
352
const config = this . _config ;
399
- return this . _resolver . build ( )
353
+ return this . _hasteMap . build ( )
400
354
. then ( ( ) => {
401
355
const farm = workerFarm ( {
402
356
autoStart : true ,
0 commit comments