|
| 1 | +import "mocha"; |
| 2 | + |
| 3 | +import { emitterFor, HTTP, Mode, Message, Emitter } from "../../src"; |
| 4 | + |
| 5 | +import { fixture, assertStructured } from "./emitter_factory_test"; |
| 6 | + |
| 7 | +import { rejects, doesNotReject } from "assert"; |
| 8 | + |
| 9 | +describe("Emitter Singleton", () => { |
| 10 | + it("emit a Node.js 'cloudevent' event as an EventEmitter", async () => { |
| 11 | + const msg: Message | unknown = await new Promise((resolve) => { |
| 12 | + const fn = async (message: Message) => { |
| 13 | + resolve(message); |
| 14 | + }; |
| 15 | + const emitter = emitterFor(fn, { binding: HTTP, mode: Mode.STRUCTURED }); |
| 16 | + Emitter.on("cloudevent", emitter); |
| 17 | + |
| 18 | + fixture.emit(false); |
| 19 | + }); |
| 20 | + let body: unknown = (<Message>(<unknown>msg)).body; |
| 21 | + if (typeof body === "string") { |
| 22 | + body = JSON.parse(body); |
| 23 | + } |
| 24 | + assertStructured({ ...(<any>body), ...(<Message>(<unknown>msg)).headers }); |
| 25 | + }); |
| 26 | + |
| 27 | + it("emit a Node.js 'cloudevent' event as an EventEmitter with ensureDelivery", async () => { |
| 28 | + let msg: Message | unknown = undefined; |
| 29 | + const fn = async (message: Message) => { |
| 30 | + msg = message; |
| 31 | + }; |
| 32 | + const emitter = emitterFor(fn, { binding: HTTP, mode: Mode.STRUCTURED }); |
| 33 | + Emitter.on("cloudevent", emitter); |
| 34 | + await fixture.emit(true); |
| 35 | + let body: any = (<Message>msg).body; |
| 36 | + if (typeof body === "string") { |
| 37 | + body = JSON.parse(body); |
| 38 | + } |
| 39 | + assertStructured({ ...(<any>body), ...(<Message>(<unknown>msg)).headers }); |
| 40 | + }); |
| 41 | + |
| 42 | + it("emit a Node.js 'cloudevent' event as an EventEmitter with ensureDelivery Error", async () => { |
| 43 | + const emitter = async () => { |
| 44 | + throw new Error("Not sent"); |
| 45 | + }; |
| 46 | + Emitter.on("cloudevent", emitter); |
| 47 | + // Should fail with emitWithEnsureDelivery |
| 48 | + await rejects(() => fixture.emit(true)); |
| 49 | + // Should not fail with emitWithEnsureDelivery |
| 50 | + // Work locally but not on Github Actions |
| 51 | + if (!process.env.GITHUB_WORKFLOW) { |
| 52 | + await doesNotReject(() => fixture.emit(false)); |
| 53 | + } |
| 54 | + }); |
| 55 | +}); |
0 commit comments