From 2f138626ff2bfd1a6d2e2d7b39c7262aee815ffd Mon Sep 17 00:00:00 2001 From: Dylan O'Gara Date: Tue, 2 Apr 2024 12:34:15 -0400 Subject: [PATCH] Create mocked APIs each time build is called Signed-off-by: Dylan O'Gara --- .changeset/gorgeous-cameras-cross.md | 5 ++ .../src/test-utils.tsx | 80 +++++++++---------- 2 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 .changeset/gorgeous-cameras-cross.md diff --git a/.changeset/gorgeous-cameras-cross.md b/.changeset/gorgeous-cameras-cross.md new file mode 100644 index 0000000000..50d7ddb2d7 --- /dev/null +++ b/.changeset/gorgeous-cameras-cross.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-techdocs-addons-test-utils': patch +--- + +Fix bug in TechDocsAddonTester when jest.resetAllMocks is called between tests diff --git a/plugins/techdocs-addons-test-utils/src/test-utils.tsx b/plugins/techdocs-addons-test-utils/src/test-utils.tsx index 51a4650f99..2066acbb48 100644 --- a/plugins/techdocs-addons-test-utils/src/test-utils.tsx +++ b/plugins/techdocs-addons-test-utils/src/test-utils.tsx @@ -49,46 +49,6 @@ if (!global.TextEncoder) { const { renderToStaticMarkup } = require('react-dom/server') as typeof import('react-dom/server'); -const techdocsApi = { - getTechDocsMetadata: jest.fn(), - getEntityMetadata: jest.fn(), - getCookie: jest.fn().mockReturnValue({ - // Expires in 10 minutes - expiresAt: new Date(Date.now() + 10 * 60 * 1000).toISOString(), - }), -}; - -const techdocsStorageApi = { - getApiOrigin: jest.fn(), - getBaseUrl: jest.fn(), - getEntityDocs: jest.fn(), - syncEntityDocs: jest.fn(), -}; - -const searchApi = { - query: jest.fn().mockResolvedValue({ results: [] }), -}; - -const scmIntegrationsApi = { - fromConfig: jest.fn().mockReturnValue({}), -}; - -const discoveryApi = { - getBaseUrl: jest - .fn() - .mockResolvedValue('https://backstage.example.com/api/techdocs'), -}; - -const fetchApi = { - fetch: jest.fn().mockResolvedValue({ - ok: true, - json: jest.fn().mockResolvedValue({ - // Expires in 10 minutes - expiresAt: new Date(Date.now() + 10 * 60 * 1000).toISOString(), - }), - }), -}; - /** @ignore */ type TechDocsAddonTesterTestApiPair = TApi extends infer TImpl ? readonly [ApiRef, Partial] @@ -223,6 +183,46 @@ export class TechDocsAddonTester { * App instance, using the given Addon(s). */ build() { + const techdocsApi = { + getTechDocsMetadata: jest.fn(), + getEntityMetadata: jest.fn(), + getCookie: jest.fn().mockReturnValue({ + // Expires in 10 minutes + expiresAt: new Date(Date.now() + 10 * 60 * 1000).toISOString(), + }), + }; + + const techdocsStorageApi = { + getApiOrigin: jest.fn(), + getBaseUrl: jest.fn(), + getEntityDocs: jest.fn(), + syncEntityDocs: jest.fn(), + }; + + const searchApi = { + query: jest.fn().mockResolvedValue({ results: [] }), + }; + + const scmIntegrationsApi = { + fromConfig: jest.fn().mockReturnValue({}), + }; + + const discoveryApi = { + getBaseUrl: jest + .fn() + .mockResolvedValue('https://backstage.example.com/api/techdocs'), + }; + + const fetchApi = { + fetch: jest.fn().mockResolvedValue({ + ok: true, + json: jest.fn().mockResolvedValue({ + // Expires in 10 minutes + expiresAt: new Date(Date.now() + 10 * 60 * 1000).toISOString(), + }), + }), + }; + const apis: TechdocsAddonTesterApis = [ [fetchApiRef, fetchApi], [discoveryApiRef, discoveryApi],