1
- import { mockDeep , mockReset } from "jest-mock-extended" ;
2
- const mockCore = mockDeep < typeof core > ( ) ;
3
-
4
- import * as core from "@codspeed/core" ;
5
1
import { Bench } from "tinybench" ;
6
- import { withCodSpeed } from ".." ;
2
+ import { beforeEach , describe , expect , it , vi } from "vitest" ;
3
+ import { withCodSpeed } from "../src" ;
7
4
import { registerBenchmarks } from "./registerBenchmarks" ;
8
5
import { registerOtherBenchmarks } from "./registerOtherBenchmarks" ;
9
6
10
- jest . mock ( "@codspeed/core" , ( ) => {
11
- mockCore . getGitDir = jest . requireActual ( "@codspeed/core" ) . getGitDir ;
12
- return mockCore ;
7
+ const mockCore = vi . hoisted ( ( ) => {
8
+ return {
9
+ mongoMeasurement : {
10
+ start : vi . fn ( ) ,
11
+ stop : vi . fn ( ) ,
12
+ setupInstruments : vi . fn ( ) ,
13
+ } ,
14
+ Measurement : {
15
+ isInstrumented : vi . fn ( ) ,
16
+ startInstrumentation : vi . fn ( ) ,
17
+ stopInstrumentation : vi . fn ( ) ,
18
+ } ,
19
+ optimizeFunction : vi
20
+ . fn ( )
21
+ . mockImplementation ( async ( fn : ( ) => Promise < void > ) => {
22
+ await fn ( ) ;
23
+ } ) ,
24
+ setupCore : vi . fn ( ) ,
25
+ teardownCore : vi . fn ( ) ,
26
+ } ;
27
+ } ) ;
28
+
29
+ vi . mock ( "@codspeed/core" , async ( importOriginal ) => {
30
+ const actual = await importOriginal < typeof import ( "@codspeed/core" ) > ( ) ;
31
+ return {
32
+ ...actual ,
33
+ ...mockCore ,
34
+ } ;
13
35
} ) ;
14
36
15
37
beforeEach ( ( ) => {
16
- mockReset ( mockCore ) ;
17
- jest . clearAllMocks ( ) ;
38
+ vi . clearAllMocks ( ) ;
18
39
} ) ;
19
40
20
41
describe ( "Benchmark.Suite" , ( ) => {
21
42
it ( "simple suite" , async ( ) => {
22
43
mockCore . Measurement . isInstrumented . mockReturnValue ( false ) ;
23
44
const bench = withCodSpeed ( new Bench ( { time : 100 } ) ) ;
24
- const onComplete = jest . fn ( ) ;
45
+ const onComplete = vi . fn ( ) ;
25
46
bench . add ( "RegExp" , function ( ) {
26
47
/ o / . test ( "Hello World!" ) ;
27
48
} ) ;
@@ -87,8 +108,8 @@ describe("Benchmark.Suite", () => {
87
108
it . each ( [ true , false ] ) (
88
109
"check console output(instrumented=%p) " ,
89
110
async ( instrumented ) => {
90
- const logSpy = jest . spyOn ( console , "log" ) ;
91
- const warnSpy = jest . spyOn ( console , "warn" ) ;
111
+ const logSpy = vi . spyOn ( console , "log" ) ;
112
+ const warnSpy = vi . spyOn ( console , "warn" ) ;
92
113
mockCore . Measurement . isInstrumented . mockReturnValue ( instrumented ) ;
93
114
await withCodSpeed ( new Bench ( { time : 100 } ) )
94
115
. add ( "RegExp" , function ( ) {
@@ -128,8 +149,8 @@ describe("Benchmark.Suite", () => {
128
149
) ;
129
150
} ) ;
130
151
// TODO: this is not supported at the moment as tinybench does not support tasks with same name
131
- // remove `.failing ` when tinybench supports it
132
- it . failing (
152
+ // remove `.fails ` when tinybench supports it
153
+ it . fails (
133
154
"check that benchmarks with same name have different URIs when registered in different files" ,
134
155
async ( ) => {
135
156
mockCore . Measurement . isInstrumented . mockReturnValue ( true ) ;
@@ -152,10 +173,10 @@ describe("Benchmark.Suite", () => {
152
173
mockCore . optimizeFunction . mockImplementation ( async ( fn ) => {
153
174
await fn ( ) ;
154
175
} ) ;
155
- const beforeAll = jest . fn ( ) ;
156
- const beforeEach = jest . fn ( ) ;
157
- const afterEach = jest . fn ( ) ;
158
- const afterAll = jest . fn ( ) ;
176
+ const beforeAll = vi . fn ( ) ;
177
+ const beforeEach = vi . fn ( ) ;
178
+ const afterEach = vi . fn ( ) ;
179
+ const afterAll = vi . fn ( ) ;
159
180
160
181
await withCodSpeed ( new Bench ( ) )
161
182
. add (
0 commit comments