diff --git a/README.md b/README.md index 257e2ba..a204d14 100644 --- a/README.md +++ b/README.md @@ -115,12 +115,12 @@ This method returns a teardown function. ```ts function show( - partialLogger?: PartialLogger = console + partialLogger: PartialLogger = console ): void function show( match: string | RegExp | MatchPredicate | Observable, - partialLogger?: PartialLogger = console + partialLogger: PartialLogger = console ): void ``` @@ -133,13 +133,17 @@ The logged information is retrieved from the most recent snapshot, so if snapsho ### log ```ts +function log( + partialLogger: PartialLogger = console +): () => void + function log( match: string | RegExp | MatchPredicate | Observable, partialLogger: PartialLogger = console ): () => void ``` -Wires up an instance of the log plugin for matching observables. +Wires up an instance of the log plugin for matching observables. If no `match` is specified, all tagged observables will be logged. All `subscribe`, `next`, `complete`, `error` and `unsubscribe` events will be logged to the console or to the specified logger. diff --git a/source/spy-spec.ts b/source/spy-spec.ts index bb52ea7..be3e095 100644 --- a/source/spy-spec.ts +++ b/source/spy-spec.ts @@ -69,6 +69,23 @@ describe("spy", () => { expect(calls).to.not.be.empty; expect(calls[0]).to.deep.equal(["Tag = people; event = unsubscribe"]); }); + + it("should log all/any tagged observables", () => { + + teardown = spy({ plugins: [] }); + + const subject = new Subject(); + const calls: any[][] = []; + + log({ + log(...args: any[]): void { calls.push(args); } + }); + + const subscription = subject.tag("people").subscribe(); + expect(calls).to.not.be.empty; + expect(calls[0]).to.deep.equal(["Tag = people; event = subscribe"]); + expect(calls[1]).to.deep.equal([" Matching", "/.+/"]); + }); }); describe("patch", () => { diff --git a/source/spy.ts b/source/spy.ts index 17b1402..099e046 100644 --- a/source/spy.ts +++ b/source/spy.ts @@ -128,7 +128,17 @@ export function flush(): void { plugins_.forEach((plugin) => plugin.flush()); } -export function log(match: Match, partialLogger?: PartialLogger): () => void { +export function log(partialLogger?: PartialLogger): () => void; +export function log(match: Match, partialLogger?: PartialLogger): () => void; +export function log(match: any, partialLogger?: PartialLogger): () => void { + + const anyTagged = /.+/; + if (!match) { + match = anyTagged; + } else if (typeof match.log === "function") { + partialLogger = match; + match = anyTagged; + } return plugin(new LogPlugin(match, partialLogger), `log(${matchToString(match)})`); }