@itee/tasks 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.idea/inspectionProfiles/Project_Default.xml +6 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/tasks.iml +8 -0
  4. package/.idea/vcs.xml +6 -0
  5. package/CHANGELOG.md +56 -0
  6. package/README.md +1 -0
  7. package/configs/builds/build.conf.mjs +1 -0
  8. package/configs/cleans/clean.conf.mjs +1 -0
  9. package/configs/docs/doc.conf.json +1 -0
  10. package/configs/lints/lint.conf.mjs +91 -0
  11. package/configs/refresh.conf.mjs +1 -0
  12. package/configs/tests/benchmarks/compute-benchmarks.conf.mjs +5 -0
  13. package/configs/tests/benchmarks/run-benchmarks-for-frontend.conf.mjs +37 -0
  14. package/configs/tests/benchmarks/run-benchmarks.conf.mjs +7 -0
  15. package/configs/tests/bundlings/check-bundling-from-esm-build-import.conf.mjs +45 -0
  16. package/configs/tests/bundlings/check-bundling-from-esm-files-direct.conf.mjs +51 -0
  17. package/configs/tests/bundlings/check-bundling-from-esm-files-import.conf.mjs +48 -0
  18. package/configs/tests/units/compute-unit-tests.conf.mjs +5 -0
  19. package/configs/tests/units/run-unit-tests-for-frontend.conf.mjs +14 -0
  20. package/configs/tests/units/run-unit-tests.conf.mjs +7 -0
  21. package/package.json +100 -0
  22. package/sources/_utils.mjs +412 -0
  23. package/sources/builds/build.task.mjs +52 -0
  24. package/sources/cleans/clean.task.mjs +32 -0
  25. package/sources/docs/doc.task.mjs +48 -0
  26. package/sources/helps/help.task.mjs +155 -0
  27. package/sources/index.mjs +21 -0
  28. package/sources/lints/lint.task.mjs +46 -0
  29. package/sources/refresh.mjs +100 -0
  30. package/sources/releases/release.task.mjs +32 -0
  31. package/sources/tests/benchmarks/compute-benchmarks.task.mjs +236 -0
  32. package/sources/tests/benchmarks/run-benchmarks-for-backend.task.mjs +43 -0
  33. package/sources/tests/benchmarks/run-benchmarks-for-frontend.task.mjs +48 -0
  34. package/sources/tests/benchmarks/run-benchmarks.task.mjs +16 -0
  35. package/sources/tests/bundlings/check-bundling-from-esm-build-import.task.mjs +127 -0
  36. package/sources/tests/bundlings/check-bundling-from-esm-files-direct.task.mjs +94 -0
  37. package/sources/tests/bundlings/check-bundling-from-esm-files-import.task.mjs +113 -0
  38. package/sources/tests/bundlings/check-bundling.task.mjs +24 -0
  39. package/sources/tests/run-tests.task.mjs +22 -0
  40. package/sources/tests/units/compute-unit-tests.task.mjs +547 -0
  41. package/sources/tests/units/run-unit-tests-for-backend.task.mjs +48 -0
  42. package/sources/tests/units/run-unit-tests-for-frontend.task.mjs +48 -0
  43. package/sources/tests/units/run-unit-tests.task.mjs +16 -0
@@ -0,0 +1,547 @@
1
+ import colors from 'ansi-colors'
2
+ import childProcess from 'child_process'
3
+ import log from 'fancy-log'
4
+ import { glob } from 'glob'
5
+ import { isNotEmptyArray } from 'itee-validators'
6
+ import {
7
+ basename,
8
+ dirname,
9
+ extname,
10
+ join,
11
+ normalize,
12
+ relative
13
+ } from 'path'
14
+ import {
15
+ createDirectoryIfNotExist,
16
+ createFile,
17
+ getPrettyPackageName,
18
+ getTaskConfigurationFor,
19
+ Indenter,
20
+ logLoadingTask,
21
+ packageName,
22
+ packageNodeModulesDirectory,
23
+ packageSourcesDirectory,
24
+ packageTestsUnitsDirectory
25
+ } from '../../_utils.mjs'
26
+
27
+ logLoadingTask( import.meta.filename )
28
+
29
+ const {
30
+ red,
31
+ yellow,
32
+ } = colors
33
+
34
+ /**
35
+ * @description Will generate unit test files from source code using type inference from comments
36
+ */
37
+ const computeUnitTestsTask = async ( done ) => {
38
+
39
+ createDirectoryIfNotExist( packageTestsUnitsDirectory )
40
+
41
+ // Get task configuration
42
+ const filePathsToIgnore = await getTaskConfigurationFor( import.meta.filename )
43
+
44
+ // Get source files to process
45
+ const pattern = join( packageSourcesDirectory, '**' )
46
+ const sourceFiles = glob.sync( pattern )
47
+ .map( filePath => normalize( filePath ) )
48
+ .filter( filePath => {
49
+ const fileName = basename( filePath )
50
+ const isJsFile = fileName.endsWith( '.js' )
51
+ const isNotPrivateFile = !fileName.startsWith( '_' )
52
+ const isNotIgnoredFile = !filePathsToIgnore.includes( fileName )
53
+ return isJsFile && isNotPrivateFile && isNotIgnoredFile
54
+ } )
55
+
56
+ const unitsImportMap = []
57
+ for ( let sourceFile of sourceFiles ) {
58
+
59
+ const specificFilePath = sourceFile.replace( packageSourcesDirectory, '' )
60
+ const specificDir = dirname( specificFilePath )
61
+
62
+ const fileName = basename( sourceFile, extname( sourceFile ) )
63
+ const unitFileName = `${ fileName }.unit.mjs`
64
+ const unitDirPath = join( packageTestsUnitsDirectory, specificDir )
65
+ const unitFilePath = join( unitDirPath, unitFileName )
66
+
67
+ const nsName = `${ fileName }Namespace`
68
+ const unitName = `${ fileName }Units`
69
+ const importDirPath = relative( unitDirPath, packageSourcesDirectory )
70
+ const importFilePath = join( importDirPath, specificFilePath ).replace( /\\/g, '/' )
71
+
72
+ try {
73
+
74
+ const jsdocPath = join( packageNodeModulesDirectory, '/jsdoc/jsdoc.js' )
75
+ const jsdocOutput = childProcess.execFileSync( 'node', [ jsdocPath, '-X', sourceFile ] ).toString()
76
+
77
+ const classNames = []
78
+ const usedLongnames = []
79
+ const jsonData = JSON.parse( jsdocOutput ).filter( data => {
80
+
81
+ const longName = data.longname
82
+
83
+ const kind = data.kind
84
+ if ( kind !== 'function' ) {
85
+ if ( kind === 'class' && !classNames.includes( longName ) ) {
86
+ classNames.push( longName )
87
+ }
88
+ return false
89
+ }
90
+
91
+ const undocumented = data.undocumented
92
+ if ( undocumented ) {
93
+ return false
94
+ }
95
+
96
+ const scope = data.scope
97
+ if ( ![ 'global', 'static' ].includes( scope ) ) {
98
+ return false
99
+ }
100
+
101
+ if ( longName.includes( ' ' ) || longName.includes( '~' ) || usedLongnames.includes( longName ) ) {
102
+ return false
103
+ }
104
+
105
+ for ( let className of classNames ) {
106
+ if ( longName.includes( className ) ) {
107
+ return false
108
+ }
109
+ }
110
+
111
+ usedLongnames.push( longName )
112
+
113
+ return true
114
+
115
+ } )
116
+
117
+ if ( jsonData.length === 0 ) {
118
+ log( 'Ignoring', yellow( `${ sourceFile }, no usable exports found` ) )
119
+ continue
120
+ }
121
+
122
+ let describes = ''
123
+ const {
124
+ I,
125
+ I_,
126
+ I__,
127
+ I___,
128
+ } = new Indenter( '\t', 3 )
129
+
130
+ for ( let docData of jsonData ) {
131
+
132
+ try {
133
+
134
+ //check input parameters and types
135
+ const docParameters = docData.params || []
136
+ const parameters = []
137
+ for ( let pIndex = 0 ; pIndex < docParameters.length ; pIndex++ ) {
138
+ const param = docParameters[ pIndex ]
139
+ let paramName = param.name
140
+ if ( !paramName ) {
141
+ paramName = `param${ pIndex }`
142
+ log( yellow( `Missing parameter name for [${ docData.longname }]. Defaulting to [${ paramName }]` ) )
143
+ }
144
+
145
+ const paramType = param.type
146
+ if ( !paramType ) {
147
+ throw new ReferenceError( `Missing parameter type. Unable to create unit test for [${ docData.longname }] !` )
148
+ }
149
+
150
+ const parameter = {
151
+ name: paramName,
152
+ types: []
153
+ }
154
+
155
+ const paramTypeNames = paramType.names
156
+ for ( let type of paramTypeNames ) {
157
+ parameter.types.push( type )
158
+ }
159
+
160
+ parameters.push( parameter )
161
+ }
162
+
163
+ // Check returns types
164
+ const docReturns = docData.returns || []
165
+ const returns = []
166
+ for ( let docReturn of docReturns ) {
167
+ const returnType = docReturn.type
168
+ if ( !returnType ) {
169
+ throw new ReferenceError( `Missing return type for [${ docData.longname }]. Ignore current target !` )
170
+ }
171
+ returns.push( ...returnType.names )
172
+ }
173
+
174
+ // Todo check throws
175
+
176
+ // Get user define rules
177
+ // const rules = []
178
+
179
+
180
+ // Infer basic rules
181
+ const baseIndent = 2
182
+ let its = ''
183
+
184
+ if ( parameters.length === 0 ) {
185
+
186
+ if ( returns.length === 0 ) {
187
+
188
+ const result = `${ I._( baseIndent + 1 ) }const result = ${ nsName }.${ docData.name }()` + '\n'
189
+ const expect = `${ I._( baseIndent + 1 ) }expect(result).to.be.a('undefined')` + '\n'
190
+
191
+ its += '' +
192
+ `${ I._( baseIndent ) }it( 'should return undefined value on call', async function () {` + '\n' +
193
+ '\n' +
194
+ `${ result }` +
195
+ `${ expect }` +
196
+ '\n' +
197
+ `${ I._( baseIndent ) }} )` + '\n'
198
+
199
+ } else if ( returns.length === 1 ) {
200
+
201
+ const firstReturnType = returns[ 0 ]
202
+ const lowerName = firstReturnType.toLowerCase()
203
+
204
+ const result = `${ I._( baseIndent + 1 ) }const result = ${ nsName }.${ docData.name }()` + '\n'
205
+
206
+ let expect = ''
207
+ if ( lowerName.startsWith( 'array' ) ) {
208
+ //todo array of...
209
+ expect += `${ I._( baseIndent + 1 ) }expect(result).to.be.a('array')` + '\n'
210
+ } else {
211
+ expect += `${ I._( baseIndent + 1 ) }expect(result).to.be.a('${ lowerName }')` + '\n'
212
+ }
213
+
214
+ its += '' +
215
+ `${ I._( baseIndent ) }it( 'should return value of type ${ lowerName }', async function() {` + '\n' +
216
+ '\n' +
217
+ `${ result }` +
218
+ `${ expect }` +
219
+ '\n' +
220
+ `${ I._( baseIndent ) }} )` + '\n'
221
+
222
+ } else {
223
+
224
+ const result = `${ I._( baseIndent + 1 ) }const result = ${ nsName }.${ docData.name }()` + '\n'
225
+
226
+ let returnTypesLabel = []
227
+ let oneOf = []
228
+ for ( let returnType of returns ) {
229
+
230
+ const lowerName = returnType.toLowerCase()
231
+ returnTypesLabel.push( lowerName )
232
+
233
+ if ( lowerName.startsWith( 'array' ) ) {
234
+ //todo array of...
235
+ oneOf.push( 'array' )
236
+ } else {
237
+ oneOf.push( `'${ lowerName }'` )
238
+ }
239
+
240
+ }
241
+
242
+ const underlyingType = `${ I._( baseIndent + 1 ) }const resultType = (result === null) ? 'null' : typeof result` + '\n'
243
+ const expect = `${ I._( baseIndent + 1 ) }expect(resultType).to.be.oneOf([${ oneOf.join( ',' ) }])` + '\n'
244
+
245
+ its += '' +
246
+ `${ I._( baseIndent ) }it( 'should return value where type is ${ returnTypesLabel.join( ' or ' ) }', async function() {` + '\n' +
247
+ '\n' +
248
+ `${ result }` +
249
+ `${ underlyingType }` +
250
+ `${ expect }` +
251
+ '\n' +
252
+ `${ I._( baseIndent ) }} )` + '\n'
253
+
254
+ }
255
+
256
+ } else {
257
+
258
+ if ( returns.length === 0 ) {
259
+
260
+ let itDeclaration = []
261
+ let index = 0
262
+ let indent = baseIndent + 1
263
+ let localIndent = indent
264
+ let dataSets = ''
265
+ let forLoopOpens = ''
266
+ let forLoopCloses = ''
267
+ let args = []
268
+ for ( let parameter of parameters ) {
269
+
270
+ const parameterType = parameter.types[ 0 ]
271
+ itDeclaration.push( `${ parameter.name } is of type ${ parameterType }` )
272
+
273
+ dataSets += `${ I._( indent ) }const dataSet${ index } = _dataMap[ '${ parameterType }s' ]` + '\n'
274
+ // dataSets += `${ I._( indent ) }const dataSet${ index } = this._dataMap[ '${ parameterType }s' ]` + '\n'
275
+ forLoopOpens += '' + '\n' +
276
+ `${ I._( localIndent ) }for ( let key${ index } in dataSet${ index } ) {` + '\n' +
277
+ `${ I._( localIndent + 1 ) }const dataSetValue${ index } = dataSet${ index }[ key${ index } ]` + '\n'
278
+
279
+ args.push( `dataSetValue${ index }` )
280
+
281
+ forLoopCloses = `${ I._( localIndent ) }}` + '\n' + `${ forLoopCloses }`
282
+
283
+ index++
284
+ localIndent++
285
+ }
286
+
287
+ const result = `${ I._( localIndent ) }const result = ${ nsName }.${ docData.name }( ${ args.join( ', ' ) } )` + '\n'
288
+ const expect = `${ I._( localIndent ) }expect(result).to.be.a('undefined')` + '\n'
289
+
290
+ const param = '' +
291
+ `${ dataSets }` +
292
+ `${ forLoopOpens }` +
293
+ `${ result }` +
294
+ `${ expect }` +
295
+ `${ forLoopCloses }`
296
+
297
+ its += '' +
298
+ `${ I._( baseIndent ) }it( 'should return undefined value when ${ itDeclaration.join( ' and ' ) }', async function() {` + '\n' +
299
+ '\n' +
300
+ `${ param }` +
301
+ '\n' +
302
+ `${ I._( baseIndent ) }} )` + '\n'
303
+
304
+ } else if ( returns.length === 1 ) {
305
+
306
+ const firstReturnType = returns[ 0 ]
307
+ const lowerName = firstReturnType.toLowerCase()
308
+
309
+ let itDeclaration = []
310
+ let index = 0
311
+ let indent = baseIndent + 1
312
+ let localIndent = indent
313
+ let dataSets = ''
314
+ let forLoopOpens = ''
315
+ let forLoopCloses = ''
316
+ let args = []
317
+ for ( let parameter of parameters ) {
318
+
319
+ const parameterType = parameter.types[ 0 ]
320
+ const isAnyType = ( parameterType === '*' || parameterType.toLowerCase() === 'any' )
321
+ const declaration = ( isAnyType )
322
+ ? `${ parameter.name } is of any type`
323
+ : `${ parameter.name } is of type ${ parameterType }`
324
+ itDeclaration.push( declaration )
325
+
326
+ if ( isAnyType ) {
327
+
328
+ dataSets += `${ I._( indent ) }const dataMap${ index } = _dataMap` + '\n' +
329
+ // dataSets += `${ I._( indent ) }const dataMap${ index } = this._dataMap` + '\n' +
330
+ `${ I._( localIndent ) }for ( let dataSetKey${ index } in dataMap${ index } ) {` + '\n'
331
+
332
+ localIndent++
333
+ dataSets += `${ I._( indent + 1 ) }const dataSet${ index } = dataMap${ index }[ dataSetKey${ index } ]` + '\n'
334
+ forLoopOpens += '' + '\n' +
335
+ `${ I._( localIndent ) }for ( let key${ index } in dataSet${ index } ) {` + '\n' +
336
+ `${ I._( localIndent + 1 ) }const dataSetValue${ index } = dataSet${ index }[ key${ index } ]` + '\n'
337
+
338
+ args.push( `dataSetValue${ index }` )
339
+
340
+ forLoopCloses = `${ I._( localIndent ) }}` + '\n' +
341
+ `${ I._( localIndent - 1 ) }}` + '\n' +
342
+ `${ forLoopCloses }`
343
+
344
+ } else {
345
+
346
+ dataSets += `${ I._( indent ) }const dataSet${ index } = _dataMap[ '${ parameterType }s' ]` + '\n'
347
+ // dataSets += `${ I._( indent ) }const dataSet${ index } = this._dataMap[ '${ parameterType }s' ]` + '\n'
348
+ forLoopOpens += '' + '\n' +
349
+ `${ I._( localIndent ) }for ( let key${ index } in dataSet${ index } ) {` + '\n' +
350
+ `${ I._( localIndent + 1 ) }const dataSetValue${ index } = dataSet${ index }[ key${ index } ]` + '\n'
351
+
352
+ args.push( `dataSetValue${ index }` )
353
+
354
+ forLoopCloses = `${ I._( localIndent ) }}` + '\n' + `${ forLoopCloses }`
355
+
356
+ }
357
+
358
+
359
+ index++
360
+ localIndent++
361
+ }
362
+
363
+ const result = `${ I._( localIndent ) }const result = ${ nsName }.${ docData.name }( ${ args.join( ', ' ) } )` + '\n'
364
+
365
+ let expect = ''
366
+ if ( lowerName.startsWith( 'array' ) ) {
367
+ expect = `${ I._( localIndent ) }expect(result).to.be.a('array')` + '\n'
368
+ //todo array of...
369
+ } else {
370
+ expect = `${ I._( localIndent ) }expect(result).to.be.a('${ lowerName }')` + '\n'
371
+ }
372
+
373
+ const param = '' +
374
+ `${ dataSets }` +
375
+ `${ forLoopOpens }` +
376
+ `${ result }` +
377
+ `${ expect }` +
378
+ `${ forLoopCloses }`
379
+
380
+ its += '' +
381
+ `${ I._( baseIndent ) }it( 'should return value of type ${ lowerName } when ${ itDeclaration.join( ' and ' ) }', async function() {` + '\n' +
382
+ '\n' +
383
+ `${ param }` +
384
+ '\n' +
385
+ `${ I._( baseIndent ) }} )` + '\n'
386
+
387
+ } else {
388
+
389
+ let itDeclaration = []
390
+ let index = 0
391
+ let indent = baseIndent + 1
392
+ let localIndent = indent
393
+ let dataSets = ''
394
+ let forLoopOpens = ''
395
+ let forLoopCloses = ''
396
+ let args = []
397
+ for ( let parameter of parameters ) {
398
+
399
+ const parameterType = parameter.types[ 0 ]
400
+ itDeclaration.push( `${ parameter.name } is of type ${ parameterType }` )
401
+
402
+ dataSets += `${ I._( localIndent ) }const dataSet${ index } = _dataMap[ '${ parameterType }s' ]` + '\n'
403
+ // dataSets += `${ I._( indent ) }const dataSet${ index } = this._dataMap[ '${ parameterType }s' ]` + '\n'
404
+ forLoopOpens += '' + '\n' +
405
+ `${ I._( localIndent ) }for ( let key${ index } in dataSet${ index } ) {` + '\n' +
406
+ `${ I._( localIndent + 1 ) }const dataSetValue${ index } = dataSet${ index }[ key${ index } ]` + '\n'
407
+
408
+ args.push( `dataSetValue${ index }` )
409
+
410
+ forLoopCloses = `${ I._( localIndent ) }}` + '\n' + `${ forLoopCloses }`
411
+
412
+ index++
413
+ localIndent++
414
+ }
415
+
416
+ const result = `${ I._( localIndent + 1 ) }const result = ${ nsName }.${ docData.name }( ${ args.join( ', ' ) } )` + '\n'
417
+
418
+ let returnTypesLabel = []
419
+ let oneOf = []
420
+ for ( let returnType of returns ) {
421
+
422
+ const lowerName = returnType.toLowerCase()
423
+ returnTypesLabel.push( lowerName )
424
+
425
+ if ( lowerName.startsWith( 'array' ) ) {
426
+ //todo array of...
427
+ oneOf.push( 'array' )
428
+ } else {
429
+ oneOf.push( `'${ lowerName }'` )
430
+ }
431
+
432
+ }
433
+
434
+ const underlyingType = `${ I._( localIndent + 1 ) }const resultType = (result === null) ? 'null' : typeof result` + '\n'
435
+ const expect = `${ I._( localIndent + 1 ) }expect(resultType).to.be.oneOf([${ oneOf.join( ',' ) }])` + '\n'
436
+
437
+ const param = '' +
438
+ `${ dataSets }` +
439
+ `${ forLoopOpens }` +
440
+ `${ result }` +
441
+ `${ underlyingType }` +
442
+ `${ expect }` +
443
+ `${ forLoopCloses }`
444
+
445
+ its += '' +
446
+ `${ I._( baseIndent ) }it( 'should return value of type ${ returnTypesLabel.join( ' or ' ) } when ${ itDeclaration.join( ' and ' ) }', async function() {` + '\n' +
447
+ '\n' +
448
+ `${ param }` +
449
+ '\n' +
450
+ `${ I._( baseIndent ) }} )` + '\n'
451
+
452
+ }
453
+
454
+ }
455
+
456
+ describes += '' +
457
+ `${ I_ }describe( '${ docData.name }()', function () {` + '\n' +
458
+ '\n' +
459
+ `${ I__ }it( 'should be bundlable', async function () {` + '\n' +
460
+ '\n' +
461
+ `${ I___ }expect(${ nsName }.${ docData.name }).to.exist` + '\n' +
462
+ '\n' +
463
+ `${ I__ }} )` + '\n' +
464
+ '\n' +
465
+ `${ its }` +
466
+ '\n' +
467
+ `${ I_ }} )` + '\n' +
468
+ '\n'
469
+
470
+ } catch ( error ) {
471
+
472
+ log( red( error.message ) )
473
+
474
+ }
475
+
476
+ }
477
+
478
+ const template = '' +
479
+ `import { expect } from 'chai'` + '\n' +
480
+ `import { Testing } from 'itee-utils/sources/testings/benchmarks.js'` + '\n' +
481
+ `import * as ${ nsName } from '${ importFilePath }'` + '\n' +
482
+ '\n' +
483
+ `describe( '${ unitName }', function () {` + '\n' +
484
+ '\n' +
485
+ `${ I_ }let _dataMap` + '\n' +
486
+ `${ I_ }before( function() {` + '\n' +
487
+ `${ I__ }_dataMap = Testing.createDataMap()` + '\n' +
488
+ `${ I_ }} )` + '\n' +
489
+ '\n' +
490
+ `${ describes }` +
491
+ '' +
492
+ `} )` + '\n'
493
+
494
+ const importUnitFilePath = relative( packageTestsUnitsDirectory, unitFilePath )
495
+ unitsImportMap.push( {
496
+ exportName: unitName,
497
+ path: importUnitFilePath.replace( /\\/g, '/' )
498
+ } )
499
+
500
+ createDirectoryIfNotExist( unitDirPath )
501
+ createFile( unitFilePath, template )
502
+
503
+ } catch ( error ) {
504
+
505
+ log( red( error.message ) )
506
+
507
+ }
508
+
509
+ }
510
+
511
+ // If some tests to import generate global units file
512
+ let unitsTemplate
513
+ if ( isNotEmptyArray( unitsImportMap ) ) {
514
+
515
+ let computedImports = []
516
+ for ( let entry of unitsImportMap ) {
517
+ // computedImports.push(`import { ${ entry.exportName } } from './${ entry.path }'`)
518
+ computedImports.push( `export * from './${ entry.path }'` )
519
+ }
520
+
521
+ unitsTemplate = computedImports.join( '\n' )
522
+
523
+ } else {
524
+
525
+ log( 'Warning ', yellow( 'No tests were generated. Create fallback global root import file.' ) )
526
+ const defaultUnitsDir = join( packageTestsUnitsDirectory, 'default' )
527
+ const defaultUnitsPath = join( defaultUnitsDir, 'default.unit.mjs' )
528
+
529
+ createDirectoryIfNotExist( defaultUnitsDir )
530
+ createFile( defaultUnitsPath, '// Avoid web test runner crash on empty benches' )
531
+
532
+ const prettyPackageName = getPrettyPackageName( '#' )
533
+ unitsTemplate = `describe( '${ prettyPackageName }', () => {} )` + '\n'
534
+
535
+ }
536
+
537
+ const unitsFilePath = join( packageTestsUnitsDirectory, `${ packageName }.units.mjs` )
538
+ createFile( unitsFilePath, unitsTemplate )
539
+
540
+ done()
541
+
542
+ }
543
+ computeUnitTestsTask.displayName = basename( import.meta.filename, '.task.mjs' )
544
+ computeUnitTestsTask.description = 'Will generate unit test files from source code using type inference from comments'
545
+ computeUnitTestsTask.flags = null
546
+
547
+ export { computeUnitTestsTask }
@@ -0,0 +1,48 @@
1
+ import colors from 'ansi-colors'
2
+ import { spawn } from 'child_process'
3
+ import log from 'fancy-log'
4
+ import { existsSync } from 'fs'
5
+ import { basename } from 'node:path'
6
+ import { join } from 'path'
7
+ import {
8
+ logLoadingTask,
9
+ packageName,
10
+ packageNodeModulesDirectory,
11
+ packageTestsUnitsDirectory
12
+ } from '../../_utils.mjs'
13
+
14
+ logLoadingTask( import.meta.filename )
15
+
16
+ const {
17
+ red,
18
+ yellow,
19
+ } = colors
20
+
21
+ /**
22
+ * @description Will run unit tests with node
23
+ */
24
+ const runUnitTestsForBackendTask = ( done ) => {
25
+
26
+ const testsPath = join( packageTestsUnitsDirectory, `/${ packageName }.units.mjs` )
27
+ if ( !existsSync( testsPath ) ) {
28
+ log( yellow( `${ testsPath } does not exist, skip backend unit tests...` ) )
29
+ done()
30
+ return
31
+ }
32
+
33
+ const mochaPath = join( packageNodeModulesDirectory, '/mocha/bin/mocha' )
34
+ const mocha = spawn( 'node', [ mochaPath, testsPath ], { stdio: 'inherit' } )
35
+ mocha.on( 'close', ( code ) => {
36
+
37
+ ( code === 0 )
38
+ ? done()
39
+ : done( red( `mocha exited with code ${ code }` ) )
40
+
41
+ } )
42
+
43
+ }
44
+ runUnitTestsForBackendTask.displayName = basename( import.meta.filename, '.task.mjs' )
45
+ runUnitTestsForBackendTask.description = 'Will run unit tests with node'
46
+ runUnitTestsForBackendTask.flags = null
47
+
48
+ export { runUnitTestsForBackendTask }
@@ -0,0 +1,48 @@
1
+ import { startTestRunner } from '@web/test-runner'
2
+ import colors from 'ansi-colors'
3
+ import { basename } from 'node:path'
4
+ import {
5
+ getTaskConfigurationFor,
6
+ logLoadingTask
7
+ } from '../../_utils.mjs'
8
+
9
+ logLoadingTask( import.meta.filename )
10
+
11
+ const { red } = colors
12
+
13
+ /**
14
+ * @description Will run unit tests with web-test-runner
15
+ */
16
+ const runUnitTestsForFrontendTask = () => {
17
+ return new Promise( async ( resolve, reject ) => {
18
+
19
+ const configuration = await getTaskConfigurationFor( import.meta.filename )
20
+ const testRunner = await startTestRunner( {
21
+ config: configuration,
22
+ readCliArgs: false,
23
+ readFileConfig: false,
24
+ autoExitProcess: false,
25
+ } )
26
+
27
+ if ( !testRunner ) {
28
+ reject( red( 'Internal test runner error.' ) )
29
+ return
30
+ }
31
+
32
+ // To ensure that testRunner exit event won't be used by other instance of test runner,
33
+ // we need to be sure that current test runner is ended
34
+ testRunner.on( 'finished', () => {
35
+ testRunner.stop()
36
+ } )
37
+
38
+ testRunner.on( 'stopped', () => {
39
+ resolve()
40
+ } )
41
+
42
+ } )
43
+ }
44
+ runUnitTestsForFrontendTask.displayName = basename( import.meta.filename, '.task.mjs' )
45
+ runUnitTestsForFrontendTask.description = 'Will run unit tests with web-test-runner'
46
+ runUnitTestsForFrontendTask.flags = null
47
+
48
+ export { runUnitTestsForFrontendTask }
@@ -0,0 +1,16 @@
1
+ import { basename } from 'node:path'
2
+ import {
3
+ getTaskConfigurationFor,
4
+ logLoadingTask,
5
+ serializeTasksFrom
6
+ } from '../../_utils.mjs'
7
+
8
+ logLoadingTask( import.meta.filename )
9
+
10
+ const configuration = await getTaskConfigurationFor( import.meta.filename )
11
+ const runUnitTestsTask = await serializeTasksFrom( configuration )
12
+ runUnitTestsTask.displayName = basename( import.meta.filename, '.task.mjs' )
13
+ runUnitTestsTask.description = 'Will run unit tests in back and front environments.'
14
+ runUnitTestsTask.flags = null
15
+
16
+ export { runUnitTestsTask }