@@ -15,46 +15,72 @@ beforeEach(() => {
1515 jest . clearAllMocks ( ) ;
1616} ) ;
1717
18+ const benchOptions : Benchmark . Options = {
19+ maxTime : 0.01 ,
20+ } ;
21+
1822describe ( "Benchmark" , ( ) => {
1923 it ( "simple benchmark" , ( ) => {
2024 mockCore . isInstrumented . mockReturnValue ( false ) ;
2125 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+ )
2533 ) ;
2634 const onComplete = jest . fn ( ) ;
2735 bench . on ( "complete" , onComplete ) ;
28- bench . run ( {
29- initCount : 0 ,
30- maxTime : - Infinity ,
31- } ) ;
36+ bench . run ( ) ;
3237 expect ( onComplete ) . toHaveBeenCalled ( ) ;
3338 expect ( mockCore . startInstrumentation ) . not . toHaveBeenCalled ( ) ;
3439 expect ( mockCore . stopInstrumentation ) . not . toHaveBeenCalled ( ) ;
3540 } ) ;
3641 it ( "check core methods are called" , ( ) => {
3742 mockCore . isInstrumented . mockReturnValue ( true ) ;
3843 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+ )
4251 ) . run ( ) ;
4352 expect ( mockCore . startInstrumentation ) . toHaveBeenCalled ( ) ;
4453 expect ( mockCore . stopInstrumentation ) . toHaveBeenCalledWith (
4554 "packages/benchmark.js-plugin/tests/unit.test.ts::RegExpSingle"
4655 ) ;
4756 } ) ;
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+ } ) ;
4870 it . each ( [ true , false ] ) (
4971 "check console output(instrumented=%p) " ,
5072 async ( instrumented ) => {
5173 const logSpy = jest . spyOn ( console , "log" ) ;
5274 const warnSpy = jest . spyOn ( console , "warn" ) ;
5375 mockCore . isInstrumented . mockReturnValue ( instrumented ) ;
5476 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+ )
5884 ) . run ( ) ;
5985 expect ( {
6086 log : logSpy . mock . calls ,
@@ -68,9 +94,13 @@ describe("Benchmark.Suite", () => {
6894 it ( "simple suite" , ( ) => {
6995 mockCore . isInstrumented . mockReturnValue ( false ) ;
7096 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+ ) ;
74104 const onComplete = jest . fn ( ) ;
75105 suite . on ( "complete" , onComplete ) ;
76106 suite . run ( { maxTime : 0.1 , initCount : 1 } ) ;
@@ -81,9 +111,13 @@ describe("Benchmark.Suite", () => {
81111 it ( "check core methods are called" , ( ) => {
82112 mockCore . isInstrumented . mockReturnValue ( true ) ;
83113 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+ )
87121 . run ( ) ;
88122 expect ( mockCore . startInstrumentation ) . toHaveBeenCalled ( ) ;
89123 expect ( mockCore . stopInstrumentation ) . toHaveBeenCalledWith (
@@ -93,12 +127,16 @@ describe("Benchmark.Suite", () => {
93127 it ( "check suite name is in the uri" , ( ) => {
94128 mockCore . isInstrumented . mockReturnValue ( true ) ;
95129 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+ )
99137 . add ( ( ) => {
100138 / o / . test ( "Hello World!" ) ;
101- } )
139+ } , benchOptions )
102140 . run ( ) ;
103141 expect ( mockCore . stopInstrumentation ) . toHaveBeenCalledWith (
104142 "packages/benchmark.js-plugin/tests/unit.test.ts::thesuite::RegExp"
@@ -107,19 +145,33 @@ describe("Benchmark.Suite", () => {
107145 "packages/benchmark.js-plugin/tests/unit.test.ts::thesuite::unknown_1"
108146 ) ;
109147 } ) ;
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+ } ) ;
110158 it . each ( [ true , false ] ) (
111159 "check console output(instrumented=%p) " ,
112160 async ( instrumented ) => {
113161 const logSpy = jest . spyOn ( console , "log" ) ;
114162 const warnSpy = jest . spyOn ( console , "warn" ) ;
115163 mockCore . isInstrumented . mockReturnValue ( instrumented ) ;
116164 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+ )
120172 . add ( ( ) => {
121173 / o / . test ( "Hello World!" ) ;
122- } )
174+ } , benchOptions )
123175 . run ( ) ;
124176 expect ( {
125177 log : logSpy . mock . calls ,
0 commit comments