diff --git a/packages/benchmark.js-plugin/src/index.ts b/packages/benchmark.js-plugin/src/index.ts index a499bb1a..a7442a56 100644 --- a/packages/benchmark.js-plugin/src/index.ts +++ b/packages/benchmark.js-plugin/src/index.ts @@ -183,6 +183,10 @@ async function runBenchmarks({ benchPayload = bench.fn as CallableFunction; } + if (typeof bench.options.setup === "function") { + await bench.options.setup(); + } + if (isAsync) { await optimizeFunction(benchPayload); await (async function __codspeed_root_frame__() { @@ -198,6 +202,11 @@ async function runBenchmarks({ Measurement.stopInstrumentation(uri); })(); } + + if (typeof bench.options.teardown === "function") { + await bench.options.teardown(); + } + console.log(` ✔ Measured ${uri}`); benchmarkCompletedListeners.forEach((listener) => listener()); } diff --git a/packages/benchmark.js-plugin/tests/index.integ.test.ts b/packages/benchmark.js-plugin/tests/index.integ.test.ts index 49d80d80..27087984 100644 --- a/packages/benchmark.js-plugin/tests/index.integ.test.ts +++ b/packages/benchmark.js-plugin/tests/index.integ.test.ts @@ -104,6 +104,23 @@ describe("Benchmark", () => { } } ); + it("should call setup and teardown", async () => { + mockCore.Measurement.isInstrumented.mockReturnValue(true); + const setup = jest.fn(); + const teardown = jest.fn(); + const bench = withCodSpeed( + new Benchmark( + "RegExpSingle", + function () { + /o/.test("Hello World!"); + }, + { ...benchOptions, setup, teardown } + ) + ); + await bench.run(); + expect(setup).toHaveBeenCalled(); + expect(teardown).toHaveBeenCalled(); + }); }); describe("Benchmark.Suite", () => { @@ -250,4 +267,21 @@ describe("Benchmark.Suite", () => { expect(mockCore.setupCore).toHaveBeenCalledTimes(1); expect(mockCore.teardownCore).toHaveBeenCalledTimes(1); }); + it("should call setup and teardown", async () => { + mockCore.Measurement.isInstrumented.mockReturnValue(true); + const setup = jest.fn(); + const teardown = jest.fn(); + + const suite = withCodSpeed(new Benchmark.Suite("thesuite")).add( + "RegExpSingle", + function () { + /o/.test("Hello World!"); + }, + { ...benchOptions, setup, teardown } + ); + await suite.run(); + + expect(setup).toHaveBeenCalled(); + expect(teardown).toHaveBeenCalled(); + }); });