diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29bb..3f99216a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1 @@ +- fix(v1): Call onInit for schedule.onRun functions (#1801) diff --git a/spec/v1/providers/pubsub.spec.ts b/spec/v1/providers/pubsub.spec.ts index eed99bc85..b115bd22b 100644 --- a/spec/v1/providers/pubsub.spec.ts +++ b/spec/v1/providers/pubsub.spec.ts @@ -22,7 +22,7 @@ import { expect } from "chai"; -import { LegacyEvent, RESET_VALUE } from "../../../src/v1"; +import { LegacyEvent, RESET_VALUE, onInit } from "../../../src/v1"; import { MINIMAL_V1_ENDPOINT } from "../../fixtures"; import { MINIMAL_SCHEDULE_TRIGGER } from "./fixtures"; import * as functions from "../../../src/v1"; @@ -256,6 +256,32 @@ describe("Pubsub Functions", () => { } ); + it("should call onInit before executing scheduled function", async () => { + const context = { + eventId: "00000", + timestamp: "2016-11-04T21:29:03.496Z", + eventType: "google.pubsub.topic.publish", + resource: { + service: "pubsub.googleapis.com", + name: "projects/project-id/topics/topic-name", + }, + }; + + let initCalled = false; + onInit(() => { + initCalled = true; + }); + + const scheduledFunc = pubsub.schedule("every 5 minutes").onRun(() => { + expect(initCalled).to.be.true; + return null; + }); + + expect(initCalled).to.be.false; + await scheduledFunc(null, context); + expect(initCalled).to.be.true; + }); + it("should return an appropriate trigger/endpoint when called with region and options", () => { const result = functions .region("us-east1") diff --git a/src/v1/cloud-functions.ts b/src/v1/cloud-functions.ts index 14ef037b4..7fe9e8741 100644 --- a/src/v1/cloud-functions.ts +++ b/src/v1/cloud-functions.ts @@ -393,6 +393,7 @@ export function makeCloudFunction({ triggerResource, }: MakeCloudFunctionArgs): CloudFunction { handler = withInit(handler ?? contextOnlyHandler); + const wrappedContextOnlyHandler = contextOnlyHandler ? withInit(contextOnlyHandler) : undefined; const cloudFunction: any = (data: any, context: any) => { if (legacyEventType && context.eventType === legacyEventType) { /* @@ -433,7 +434,7 @@ export function makeCloudFunction({ let promise; if (labels && labels["deployment-scheduled"]) { // Scheduled function do not have meaningful data, so exclude it - promise = contextOnlyHandler(context); + promise = wrappedContextOnlyHandler(context); } else { const dataOrChange = dataConstructor(event); promise = handler(dataOrChange, context);