@@ -2,7 +2,15 @@ import * as path from 'path';
2
2
import * as Mocha from 'mocha' ;
3
3
import * as glob from 'glob' ;
4
4
5
- export function run ( ) : Promise < void > {
5
+ import * as fs from 'fs-extra' ;
6
+ import * as baseConfig from "@istanbuljs/nyc-config-typescript" ;
7
+
8
+ import 'ts-node/register' ;
9
+ import 'source-map-support/register' ;
10
+
11
+ const NYC = require ( 'nyc' ) ;
12
+
13
+ export async function run ( ) : Promise < void > {
6
14
// Create the mocha test
7
15
const mocha = new Mocha ( {
8
16
ui : 'tdd'
@@ -11,28 +19,61 @@ export function run(): Promise<void> {
11
19
12
20
const testsRoot = path . resolve ( __dirname , '..' ) ;
13
21
14
- return new Promise ( ( c , e ) => {
15
- glob ( '**/**.test.js' , { cwd : testsRoot } , ( err , files ) => {
16
- if ( err ) {
17
- return e ( err ) ;
18
- }
19
-
20
- // Add files to the test suite
21
- files . forEach ( f => mocha . addFile ( path . resolve ( testsRoot , f ) ) ) ;
22
-
23
- try {
24
- // Run the mocha test
25
- mocha . run ( failures => {
26
- if ( failures > 0 ) {
27
- e ( new Error ( `${ failures } tests failed.` ) ) ;
28
- } else {
29
- c ( ) ;
30
- }
31
- } ) ;
32
- } catch ( err ) {
33
- console . error ( err ) ;
34
- e ( err ) ;
35
- }
36
- } ) ;
22
+ const nyc = await setupNYC ( ) ;
23
+
24
+ // Add all test files to mocha
25
+ const testFiles = glob . sync ( '**/*.test.js' , { cwd : testsRoot } ) ;
26
+ testFiles . forEach ( f => mocha . addFile ( path . resolve ( testsRoot , f ) ) ) ;
27
+
28
+ const failures : number = await new Promise ( resolve => mocha . run ( resolve ) ) ;
29
+
30
+ await reportCodeCoverage ( nyc ) ;
31
+
32
+ if ( failures > 0 ) {
33
+ throw new Error ( `${ failures } tests failed.` ) ;
34
+ }
35
+ }
36
+
37
+ async function setupNYC ( ) {
38
+ let nyc = new NYC ( {
39
+ ...baseConfig ,
40
+ all : true ,
41
+ cwd : path . join ( __dirname , '..' , '..' , '..' ) ,
42
+ exclude : [ "out/test/**" ] ,
43
+ include : [ "out/**/*.js" ] ,
44
+ instrument : true ,
45
+ reporter : [ 'text-summary' , 'html' ] ,
46
+ hookRequire : true ,
47
+ hookRunInContext : true ,
48
+ hookRunInThisContext : true ,
49
+ silent : false
37
50
} ) ;
38
- }
51
+ await nyc . wrap ( ) ;
52
+
53
+ // Delete the 'coverage' folder first to make sure the HTML report is only for current run of npm test
54
+ const tempDirectory = nyc . tempDirectory ( ) ;
55
+ if ( fs . existsSync ( tempDirectory ) ) {
56
+ fs . removeSync ( tempDirectory ) ;
57
+ }
58
+ await nyc . createTempDirectory ( ) ;
59
+
60
+ return nyc ;
61
+ }
62
+
63
+ async function reportCodeCoverage ( nyc ) {
64
+ await nyc . writeCoverageFile ( ) ;
65
+
66
+ let textReport = '' ;
67
+
68
+ let currentWrite = process . stdout . write ;
69
+ process . stdout . write = ( s ) => { textReport = textReport + s ; return true ; } ;
70
+
71
+ await nyc . report . bind ( nyc ) ( ) ;
72
+
73
+ process . stdout . write = currentWrite ;
74
+
75
+ console . log ( textReport ) ;
76
+ console . log ( "--------------------------------------------------------" ) ;
77
+ console . log ( "Open coverage folder to check detailed report in HTML." ) ;
78
+ console . log ( "--------------------------------------------------------" ) ;
79
+ }
0 commit comments