@@ -15,46 +15,72 @@ beforeEach(() => {
15
15
jest . clearAllMocks ( ) ;
16
16
} ) ;
17
17
18
+ const benchOptions : Benchmark . Options = {
19
+ maxTime : 0.01 ,
20
+ } ;
21
+
18
22
describe ( "Benchmark" , ( ) => {
19
23
it ( "simple benchmark" , ( ) => {
20
24
mockCore . isInstrumented . mockReturnValue ( false ) ;
21
25
const bench = withCodSpeed (
22
- new Benchmark ( "RegExp" , function ( ) {
23
- / o / . test ( "Hello World!" ) ;
24
- } )
26
+ new Benchmark (
27
+ "RegExp" ,
28
+ function ( ) {
29
+ / o / . test ( "Hello World!" ) ;
30
+ } ,
31
+ benchOptions
32
+ )
25
33
) ;
26
34
const onComplete = jest . fn ( ) ;
27
35
bench . on ( "complete" , onComplete ) ;
28
- bench . run ( {
29
- initCount : 0 ,
30
- maxTime : - Infinity ,
31
- } ) ;
36
+ bench . run ( ) ;
32
37
expect ( onComplete ) . toHaveBeenCalled ( ) ;
33
38
expect ( mockCore . startInstrumentation ) . not . toHaveBeenCalled ( ) ;
34
39
expect ( mockCore . stopInstrumentation ) . not . toHaveBeenCalled ( ) ;
35
40
} ) ;
36
41
it ( "check core methods are called" , ( ) => {
37
42
mockCore . isInstrumented . mockReturnValue ( true ) ;
38
43
withCodSpeed (
39
- new Benchmark ( "RegExpSingle" , function ( ) {
40
- / o / . test ( "Hello World!" ) ;
41
- } )
44
+ new Benchmark (
45
+ "RegExpSingle" ,
46
+ function ( ) {
47
+ / o / . test ( "Hello World!" ) ;
48
+ } ,
49
+ benchOptions
50
+ )
42
51
) . run ( ) ;
43
52
expect ( mockCore . startInstrumentation ) . toHaveBeenCalled ( ) ;
44
53
expect ( mockCore . stopInstrumentation ) . toHaveBeenCalledWith (
45
54
"packages/benchmark.js-plugin/tests/unit.test.ts::RegExpSingle"
46
55
) ;
47
56
} ) ;
57
+ it ( "check error handling" , async ( ) => {
58
+ mockCore . isInstrumented . mockReturnValue ( true ) ;
59
+ const bench = withCodSpeed (
60
+ new Benchmark (
61
+ "throwing" ,
62
+ ( ) => {
63
+ throw new Error ( "test" ) ;
64
+ } ,
65
+ benchOptions
66
+ )
67
+ ) ;
68
+ expect ( ( ) => bench . run ( ) ) . toThrowError ( "test" ) ;
69
+ } ) ;
48
70
it . each ( [ true , false ] ) (
49
71
"check console output(instrumented=%p) " ,
50
72
async ( instrumented ) => {
51
73
const logSpy = jest . spyOn ( console , "log" ) ;
52
74
const warnSpy = jest . spyOn ( console , "warn" ) ;
53
75
mockCore . isInstrumented . mockReturnValue ( instrumented ) ;
54
76
withCodSpeed (
55
- new Benchmark ( "RegExpSingle" , function ( ) {
56
- / o / . test ( "Hello World!" ) ;
57
- } )
77
+ new Benchmark (
78
+ "RegExpSingle" ,
79
+ function ( ) {
80
+ / o / . test ( "Hello World!" ) ;
81
+ } ,
82
+ benchOptions
83
+ )
58
84
) . run ( ) ;
59
85
expect ( {
60
86
log : logSpy . mock . calls ,
@@ -68,9 +94,13 @@ describe("Benchmark.Suite", () => {
68
94
it ( "simple suite" , ( ) => {
69
95
mockCore . isInstrumented . mockReturnValue ( false ) ;
70
96
const suite = withCodSpeed ( new Benchmark . Suite ( ) ) ;
71
- suite . add ( "RegExp" , function ( ) {
72
- / o / . test ( "Hello World!" ) ;
73
- } ) ;
97
+ suite . add (
98
+ "RegExp" ,
99
+ function ( ) {
100
+ / o / . test ( "Hello World!" ) ;
101
+ } ,
102
+ benchOptions
103
+ ) ;
74
104
const onComplete = jest . fn ( ) ;
75
105
suite . on ( "complete" , onComplete ) ;
76
106
suite . run ( { maxTime : 0.1 , initCount : 1 } ) ;
@@ -81,9 +111,13 @@ describe("Benchmark.Suite", () => {
81
111
it ( "check core methods are called" , ( ) => {
82
112
mockCore . isInstrumented . mockReturnValue ( true ) ;
83
113
withCodSpeed ( new Benchmark . Suite ( ) )
84
- . add ( "RegExp" , function ( ) {
85
- / o / . test ( "Hello World!" ) ;
86
- } )
114
+ . add (
115
+ "RegExp" ,
116
+ function ( ) {
117
+ / o / . test ( "Hello World!" ) ;
118
+ } ,
119
+ benchOptions
120
+ )
87
121
. run ( ) ;
88
122
expect ( mockCore . startInstrumentation ) . toHaveBeenCalled ( ) ;
89
123
expect ( mockCore . stopInstrumentation ) . toHaveBeenCalledWith (
@@ -93,12 +127,16 @@ describe("Benchmark.Suite", () => {
93
127
it ( "check suite name is in the uri" , ( ) => {
94
128
mockCore . isInstrumented . mockReturnValue ( true ) ;
95
129
withCodSpeed ( new Benchmark . Suite ( "thesuite" ) )
96
- . add ( "RegExp" , function ( ) {
97
- / o / . test ( "Hello World!" ) ;
98
- } )
130
+ . add (
131
+ "RegExp" ,
132
+ function ( ) {
133
+ / o / . test ( "Hello World!" ) ;
134
+ } ,
135
+ benchOptions
136
+ )
99
137
. add ( ( ) => {
100
138
/ o / . test ( "Hello World!" ) ;
101
- } )
139
+ } , benchOptions )
102
140
. run ( ) ;
103
141
expect ( mockCore . stopInstrumentation ) . toHaveBeenCalledWith (
104
142
"packages/benchmark.js-plugin/tests/unit.test.ts::thesuite::RegExp"
@@ -107,19 +145,33 @@ describe("Benchmark.Suite", () => {
107
145
"packages/benchmark.js-plugin/tests/unit.test.ts::thesuite::unknown_1"
108
146
) ;
109
147
} ) ;
148
+ it ( "check error handling" , async ( ) => {
149
+ mockCore . isInstrumented . mockReturnValue ( true ) ;
150
+ const bench = withCodSpeed ( new Benchmark . Suite ( "thesuite" ) ) . add (
151
+ "throwing" ,
152
+ ( ) => {
153
+ throw new Error ( "test" ) ;
154
+ }
155
+ ) ;
156
+ expect ( ( ) => bench . run ( ) ) . toThrowError ( "test" ) ;
157
+ } ) ;
110
158
it . each ( [ true , false ] ) (
111
159
"check console output(instrumented=%p) " ,
112
160
async ( instrumented ) => {
113
161
const logSpy = jest . spyOn ( console , "log" ) ;
114
162
const warnSpy = jest . spyOn ( console , "warn" ) ;
115
163
mockCore . isInstrumented . mockReturnValue ( instrumented ) ;
116
164
withCodSpeed ( new Benchmark . Suite ( "thesuite" ) )
117
- . add ( "RegExp" , function ( ) {
118
- / o / . test ( "Hello World!" ) ;
119
- } )
165
+ . add (
166
+ "RegExp" ,
167
+ function ( ) {
168
+ / o / . test ( "Hello World!" ) ;
169
+ } ,
170
+ benchOptions
171
+ )
120
172
. add ( ( ) => {
121
173
/ o / . test ( "Hello World!" ) ;
122
- } )
174
+ } , benchOptions )
123
175
. run ( ) ;
124
176
expect ( {
125
177
log : logSpy . mock . calls ,
0 commit comments