Skip to content

Commit 5b68849

Browse files
committed
fix: add tests checking error handling
1 parent 7eb75f4 commit 5b68849

File tree

3 files changed

+88
-28
lines changed

3 files changed

+88
-28
lines changed

.moon/project.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ tasks:
6161
NODE_NO_WARNINGS: "1"
6262

6363
test:
64-
command: "jest --passWithNoTests"
64+
command: "jest --passWithNoTests --silent"
6565
inputs:
6666
- "@globs(sources)"
6767
- "@globs(tests)"

packages/benchmark.js-plugin/tests/unit.test.ts

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,72 @@ beforeEach(() => {
1515
jest.clearAllMocks();
1616
});
1717

18+
const benchOptions: Benchmark.Options = {
19+
maxTime: 0.01,
20+
};
21+
1822
describe("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,

packages/tinybench-plugin/tests/unit.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ describe("Benchmark.Suite", () => {
5959
"packages/tinybench-plugin/tests/unit.test.ts::RegExp2"
6060
);
6161
});
62+
it("check error handling", async () => {
63+
mockCore.isInstrumented.mockReturnValue(true);
64+
const bench = withCodSpeed(new Bench());
65+
bench.add("throwing", async () => {
66+
throw new Error("test");
67+
});
68+
await expect(bench.run()).rejects.toThrowError("test");
69+
});
6270
it.each([true, false])(
6371
"check console output(instrumented=%p) ",
6472
async (instrumented) => {

0 commit comments

Comments
 (0)