From dd168c3468e029c4b17dc34856c0e53cdf8583dc Mon Sep 17 00:00:00 2001 From: Alex Klarfeld Date: Sun, 21 Apr 2024 18:27:49 -0700 Subject: [PATCH] Explicit capture --- src/index.ts | 15 ++++++++++- test/e2e/explicit-capture.e2e.test.ts | 36 ++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index a74e39b..4855b61 100644 --- a/src/index.ts +++ b/src/index.ts @@ -488,9 +488,22 @@ const Supergood = () => { supergoodAsyncLocalStorage.disable(); } + const getAsyncLocalStorage = () => supergoodAsyncLocalStorage.getStore(); + // Set up cleanup catch for exit signals onExit(() => close(), { alwaysLast: true }); - return { close, flushCache, waitAndFlushCache, withTags, init, withCapture, startCapture, stopCapture }; + + return { + close, + flushCache, + waitAndFlushCache, + withTags, + init, + withCapture, + startCapture, + stopCapture, + getAsyncLocalStorage + }; }; export = Supergood(); diff --git a/test/e2e/explicit-capture.e2e.test.ts b/test/e2e/explicit-capture.e2e.test.ts index 2b1cdc9..8fa3c9a 100644 --- a/test/e2e/explicit-capture.e2e.test.ts +++ b/test/e2e/explicit-capture.e2e.test.ts @@ -118,7 +118,6 @@ describe('capture functionality', () => { await axios.get(`${MOCK_DATA_SERVER}/posts`), 250 }); getInterval.unref(); - await Supergood.startCapture( { config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true }, @@ -133,6 +132,7 @@ describe('capture functionality', () => { } await Supergood.stopCapture(); + await axios.get(`${MOCK_DATA_SERVER}/posts`); clearInterval(getInterval); @@ -147,5 +147,39 @@ describe('capture functionality', () => { }); }); + it('should capture calls without an async kickoff, with useRemoteConfig set to false', async () => { + const numberOfHttpCalls = 5; + + const getInterval = setInterval(async () => { + await axios.get(`${MOCK_DATA_SERVER}/posts`), 250 + }); + getInterval.unref(); + Supergood.startCapture( + { + config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true, useRemoteConfig: false }, + clientId: SUPERGOOD_CLIENT_ID, + clientSecret: SUPERGOOD_CLIENT_SECRET, + baseUrl: SUPERGOOD_SERVER + }); + + + for (let i = 0; i < numberOfHttpCalls; i++) { + await axios.get(`${MOCK_DATA_SERVER}/posts`); + } + + await Supergood.stopCapture(); + await axios.get(`${MOCK_DATA_SERVER}/posts`); + clearInterval(getInterval); + + checkPostedEvents(postEventsMock, numberOfHttpCalls, { + request: expect.objectContaining({ + requestedAt: expect.any(Date) + }), + response: expect.objectContaining({ + respondedAt: expect.any(Date) + }) + }); + }); }); +