Skip to content

Commit 3aa88de

Browse files
chore(tinybench-plugin): use vitest instead of jest
1 parent 66f902f commit 3aa88de

File tree

7 files changed

+91
-68
lines changed

7 files changed

+91
-68
lines changed

packages/tinybench-plugin/moon.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ tasks:
77
platform: "system"
88
options:
99
cache: false
10+
test:
11+
command: vitest --run
12+
inputs:
13+
- "./vitest.config.ts"
14+
15+
test/integ:
16+
command: noop

packages/tinybench-plugin/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"license": "Apache-2.0",
2727
"devDependencies": {
2828
"@types/stack-trace": "^0.0.30",
29-
"jest-mock-extended": "^3.0.4",
30-
"tinybench": "^2.5.0"
29+
"tinybench": "^2.5.0",
30+
"vitest": "^1.2.2"
3131
},
3232
"dependencies": {
3333
"@codspeed/core": "workspace:^4.0.1",

packages/tinybench-plugin/tests/__snapshots__/index.integ.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`Benchmark.Suite check console output(instrumented=false) 1`] = `
3+
exports[`Benchmark.Suite > check console output(instrumented=%p) false 1`] = `
44
{
55
"log": [],
66
"warn": [
@@ -11,7 +11,7 @@ exports[`Benchmark.Suite check console output(instrumented=false) 1`] = `
1111
}
1212
`;
1313

14-
exports[`Benchmark.Suite check console output(instrumented=true) 1`] = `
14+
exports[`Benchmark.Suite > check console output(instrumented=%p) true 1`] = `
1515
{
1616
"log": [
1717
[

packages/tinybench-plugin/tests/index.integ.test.ts

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
1-
import { mockDeep, mockReset } from "jest-mock-extended";
2-
const mockCore = mockDeep<typeof core>();
3-
4-
import * as core from "@codspeed/core";
51
import { Bench } from "tinybench";
6-
import { withCodSpeed } from "..";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import { withCodSpeed } from "../src";
74
import { registerBenchmarks } from "./registerBenchmarks";
85
import { registerOtherBenchmarks } from "./registerOtherBenchmarks";
96

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+
};
1335
});
1436

1537
beforeEach(() => {
16-
mockReset(mockCore);
17-
jest.clearAllMocks();
38+
vi.clearAllMocks();
1839
});
1940

2041
describe("Benchmark.Suite", () => {
2142
it("simple suite", async () => {
2243
mockCore.Measurement.isInstrumented.mockReturnValue(false);
2344
const bench = withCodSpeed(new Bench({ time: 100 }));
24-
const onComplete = jest.fn();
45+
const onComplete = vi.fn();
2546
bench.add("RegExp", function () {
2647
/o/.test("Hello World!");
2748
});
@@ -87,8 +108,8 @@ describe("Benchmark.Suite", () => {
87108
it.each([true, false])(
88109
"check console output(instrumented=%p) ",
89110
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");
92113
mockCore.Measurement.isInstrumented.mockReturnValue(instrumented);
93114
await withCodSpeed(new Bench({ time: 100 }))
94115
.add("RegExp", function () {
@@ -128,8 +149,8 @@ describe("Benchmark.Suite", () => {
128149
);
129150
});
130151
// 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(
133154
"check that benchmarks with same name have different URIs when registered in different files",
134155
async () => {
135156
mockCore.Measurement.isInstrumented.mockReturnValue(true);
@@ -152,10 +173,10 @@ describe("Benchmark.Suite", () => {
152173
mockCore.optimizeFunction.mockImplementation(async (fn) => {
153174
await fn();
154175
});
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();
159180

160181
await withCodSpeed(new Bench())
161182
.add(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"types": ["jest", "node"]
4+
"types": ["vitest/globals", "node"]
55
},
66
"include": ["tests/**/*.ts", "benches/**/*.ts", "jest.config.ts"]
77
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
define: {
5+
__VERSION__: JSON.stringify("1.0.0"),
6+
},
7+
test: {
8+
exclude: ["**/node_modules/**", "**/.rollup.cache/**"],
9+
mockReset: true,
10+
},
11+
});

0 commit comments

Comments
 (0)