3
3
* @module tests/reporters/Notifier
4
4
*/
5
5
6
- import type { OneOrMany } from '@flex-development/tutils'
6
+ import { cast , isArray , type OneOrMany } from '@flex-development/tutils'
7
7
import notifier from 'node-notifier'
8
8
import type NotificationCenter from 'node-notifier/notifiers/notificationcenter'
9
9
import { performance } from 'node:perf_hooks'
@@ -20,22 +20,28 @@ import type { File, Reporter, Task, Test, Vitest } from 'vitest'
20
20
*/
21
21
class Notifier implements Reporter {
22
22
/**
23
+ * Test reporter context.
24
+ *
23
25
* @public
24
- * @member {Vitest} ctx - Test reporter context
26
+ * @member {Vitest} ctx
25
27
*/
26
- public ctx : Vitest = { } as Vitest
28
+ public ctx ! : Vitest
27
29
28
30
/**
31
+ * Test run end time (in milliseconds).
32
+ *
29
33
* @public
30
- * @member {number} end - Test run end time (in milliseconds)
34
+ * @member {number} end
31
35
*/
32
- public end : number = 0
36
+ public end ! : number
33
37
34
38
/**
39
+ * Test run start time (in milliseconds).
40
+ *
35
41
* @public
36
- * @member {number} start - Test run start time (in milliseconds)
42
+ * @member {number} start
37
43
*/
38
- public start : number = 0
44
+ public start ! : number
39
45
40
46
/**
41
47
* Sends a notification.
@@ -52,19 +58,39 @@ class Notifier implements Reporter {
52
58
files : File [ ] = this . ctx . state . getFiles ( ) ,
53
59
errors : unknown [ ] = this . ctx . state . getUnhandledErrors ( )
54
60
) : Promise < void > {
55
- /** @const {Test[]} tests - Tests run */
61
+ /**
62
+ * Tests that have been run.
63
+ *
64
+ * @const {Test[]} tests
65
+ */
56
66
const tests : Test [ ] = this . tests ( files )
57
67
58
- /** @const {number} fails - Total number of failed tests */
68
+ /**
69
+ * Total number of failed tests.
70
+ *
71
+ * @const {number} fails
72
+ */
59
73
const fails : number = tests . filter ( t => t . result ?. state === 'fail' ) . length
60
74
61
- /** @const {number} passes - Total number of passed tests */
75
+ /**
76
+ * Total number of passed tests.
77
+ *
78
+ * @const {number} passes
79
+ */
62
80
const passes : number = tests . filter ( t => t . result ?. state === 'pass' ) . length
63
81
64
- /** @var {string} message - Notification message */
82
+ /**
83
+ * Notification message.
84
+ *
85
+ * @var {string} message
86
+ */
65
87
let message : string = ''
66
88
67
- /** @var {string} title - Notification title */
89
+ /**
90
+ * Notification title.
91
+ *
92
+ * @var {string} title
93
+ */
68
94
let title : string = ''
69
95
70
96
// get notification title and message based on number of failed tests
@@ -76,7 +102,11 @@ class Notifier implements Reporter {
76
102
77
103
title = '\u274C Failed'
78
104
} else {
79
- /** @const {number} time - Time to run all tests (in milliseconds) */
105
+ /**
106
+ * Time to run all tests (in milliseconds).
107
+ *
108
+ * @const {number} time
109
+ */
80
110
const time : number = this . end - this . start
81
111
82
112
message = dedent `
@@ -128,29 +158,25 @@ class Notifier implements Reporter {
128
158
*/
129
159
public onInit ( context : Vitest ) : void {
130
160
this . ctx = context
131
- return void ( this . start = performance . now ( ) )
161
+ return void ( ( this . start = performance . now ( ) ) && ( this . end = 0 ) )
132
162
}
133
163
134
164
/**
135
- * Returns an array of {@linkcode Test} objects.
165
+ * Returns an array of {@link Test} objects.
136
166
*
137
167
* @protected
138
168
*
139
169
* @param {OneOrMany<Task> } [tasks=[]] - Tasks to collect tests from
140
170
* @return {Test[] } `Test` object array
141
171
*/
142
172
protected tests ( tasks : OneOrMany < Task > = [ ] ) : Test [ ] {
143
- const { mode } = this . ctx
144
-
145
- return ( Array . isArray ( tasks ) ? tasks : [ tasks ] ) . flatMap ( task => {
146
- const { type } = task
147
-
148
- return mode === 'typecheck' && type === 'suite' && task . tasks . length === 0
149
- ? ( [ task ] as unknown as [ Test ] )
150
- : type === 'test'
173
+ return ( isArray < Task > ( tasks ) ? tasks : [ tasks ] ) . flatMap ( task => {
174
+ return task . type === 'custom'
175
+ ? [ cast ( task ) ]
176
+ : task . type === 'test'
151
177
? [ task ]
152
178
: 'tasks' in task
153
- ? task . tasks . flatMap ( t => ( t . type === 'test' ? [ t ] : this . tests ( t ) ) )
179
+ ? task . tasks . flatMap ( task => this . tests ( task ) )
154
180
: [ ]
155
181
} )
156
182
}
0 commit comments