Skip to content

Commit 7eb75f4

Browse files
committed
feat: test the console output of the modules
1 parent cb5eaca commit 7eb75f4

File tree

4 files changed

+151
-1
lines changed

4 files changed

+151
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Benchmark check console output(instrumented=false) 1`] = `
4+
{
5+
"log": [],
6+
"warn": [
7+
[
8+
"[CodSpeed] bench detected but no instrumentation found, falling back to benchmark.js",
9+
],
10+
],
11+
}
12+
`;
13+
14+
exports[`Benchmark check console output(instrumented=true) 1`] = `
15+
{
16+
"log": [
17+
[
18+
"[CodSpeed] running with @codspeed/benchmark.js v1.0.0",
19+
],
20+
[
21+
" ✔ Measured packages/benchmark.js-plugin/tests/unit.test.ts::RegExpSingle",
22+
],
23+
[
24+
"[CodSpeed] Done running 1 bench.",
25+
],
26+
],
27+
"warn": [],
28+
}
29+
`;
30+
31+
exports[`Benchmark.Suite check console output(instrumented=false) 1`] = `
32+
{
33+
"log": [],
34+
"warn": [
35+
[
36+
"[CodSpeed] 2 benches detected but no instrumentation found, falling back to benchmark.js",
37+
],
38+
],
39+
}
40+
`;
41+
42+
exports[`Benchmark.Suite check console output(instrumented=true) 1`] = `
43+
{
44+
"log": [
45+
[
46+
"[CodSpeed] running with @codspeed/benchmark.js v1.0.0",
47+
],
48+
[
49+
" ✔ Measured packages/benchmark.js-plugin/tests/unit.test.ts::thesuite::RegExp",
50+
],
51+
[
52+
" ✔ Measured packages/benchmark.js-plugin/tests/unit.test.ts::thesuite::unknown_1",
53+
],
54+
[
55+
"[CodSpeed] Done running 2 benches.",
56+
],
57+
],
58+
"warn": [],
59+
}
60+
`;

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jest.mock("@codspeed/core", () => ({
1212

1313
beforeEach(() => {
1414
mockReset(mockCore);
15+
jest.clearAllMocks();
1516
});
1617

1718
describe("Benchmark", () => {
@@ -34,7 +35,6 @@ describe("Benchmark", () => {
3435
});
3536
it("check core methods are called", () => {
3637
mockCore.isInstrumented.mockReturnValue(true);
37-
expect(mockCore.isInstrumented()).toBe(true);
3838
withCodSpeed(
3939
new Benchmark("RegExpSingle", function () {
4040
/o/.test("Hello World!");
@@ -45,6 +45,23 @@ describe("Benchmark", () => {
4545
"packages/benchmark.js-plugin/tests/unit.test.ts::RegExpSingle"
4646
);
4747
});
48+
it.each([true, false])(
49+
"check console output(instrumented=%p) ",
50+
async (instrumented) => {
51+
const logSpy = jest.spyOn(console, "log");
52+
const warnSpy = jest.spyOn(console, "warn");
53+
mockCore.isInstrumented.mockReturnValue(instrumented);
54+
withCodSpeed(
55+
new Benchmark("RegExpSingle", function () {
56+
/o/.test("Hello World!");
57+
})
58+
).run();
59+
expect({
60+
log: logSpy.mock.calls,
61+
warn: warnSpy.mock.calls,
62+
}).toMatchSnapshot();
63+
}
64+
);
4865
});
4966

5067
describe("Benchmark.Suite", () => {
@@ -90,4 +107,24 @@ describe("Benchmark.Suite", () => {
90107
"packages/benchmark.js-plugin/tests/unit.test.ts::thesuite::unknown_1"
91108
);
92109
});
110+
it.each([true, false])(
111+
"check console output(instrumented=%p) ",
112+
async (instrumented) => {
113+
const logSpy = jest.spyOn(console, "log");
114+
const warnSpy = jest.spyOn(console, "warn");
115+
mockCore.isInstrumented.mockReturnValue(instrumented);
116+
withCodSpeed(new Benchmark.Suite("thesuite"))
117+
.add("RegExp", function () {
118+
/o/.test("Hello World!");
119+
})
120+
.add(() => {
121+
/o/.test("Hello World!");
122+
})
123+
.run();
124+
expect({
125+
log: logSpy.mock.calls,
126+
warn: warnSpy.mock.calls,
127+
}).toMatchSnapshot();
128+
}
129+
);
93130
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Benchmark.Suite check console output(instrumented=false) 1`] = `
4+
{
5+
"log": [],
6+
"warn": [
7+
[
8+
"[CodSpeed] 2 benches detected but no instrumentation found, falling back to tinybench",
9+
],
10+
],
11+
}
12+
`;
13+
14+
exports[`Benchmark.Suite check console output(instrumented=true) 1`] = `
15+
{
16+
"log": [
17+
[
18+
"[CodSpeed] running with @codspeed/tinybench v1.0.0",
19+
],
20+
[
21+
" ✔ Measured packages/tinybench-plugin/tests/unit.test.ts::RegExp",
22+
],
23+
[
24+
" ✔ Measured packages/tinybench-plugin/tests/unit.test.ts::RegExp2",
25+
],
26+
[
27+
"[CodSpeed] Done running 2 benches.",
28+
],
29+
],
30+
"warn": [],
31+
}
32+
`;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jest.mock("@codspeed/core", () => ({
1212

1313
beforeEach(() => {
1414
mockReset(mockCore);
15+
jest.clearAllMocks();
1516
});
1617

1718
describe("Benchmark.Suite", () => {
@@ -58,4 +59,24 @@ describe("Benchmark.Suite", () => {
5859
"packages/tinybench-plugin/tests/unit.test.ts::RegExp2"
5960
);
6061
});
62+
it.each([true, false])(
63+
"check console output(instrumented=%p) ",
64+
async (instrumented) => {
65+
const logSpy = jest.spyOn(console, "log");
66+
const warnSpy = jest.spyOn(console, "warn");
67+
mockCore.isInstrumented.mockReturnValue(instrumented);
68+
await withCodSpeed(new Bench({ time: 100 }))
69+
.add("RegExp", function () {
70+
/o/.test("Hello World!");
71+
})
72+
.add("RegExp2", () => {
73+
/o/.test("Hello World!");
74+
})
75+
.run();
76+
expect({
77+
log: logSpy.mock.calls,
78+
warn: warnSpy.mock.calls,
79+
}).toMatchSnapshot();
80+
}
81+
);
6182
});

0 commit comments

Comments
 (0)