From 2bc72bc634c994610dbd2bd708c28295557268cb Mon Sep 17 00:00:00 2001 From: Claude Code Date: Wed, 15 Apr 2026 15:21:37 -0700 Subject: [PATCH] refactor(test): consolidate mock teardown into afterEach for reliable cleanup Moves jest.restoreAllMocks() and ClipboardItem deletion from per-test inline calls to a shared afterEach, so global state is always restored even when an assertion throws mid-test. Co-Authored-By: Claude Sonnet 4.6 --- .../superset-ui-core/test/utils/copy.test.ts | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/superset-frontend/packages/superset-ui-core/test/utils/copy.test.ts b/superset-frontend/packages/superset-ui-core/test/utils/copy.test.ts index 3f10b1fe115..2ca9d3bec96 100644 --- a/superset-frontend/packages/superset-ui-core/test/utils/copy.test.ts +++ b/superset-frontend/packages/superset-ui-core/test/utils/copy.test.ts @@ -27,6 +27,11 @@ const makeGetText = (text: string) => () => Promise.resolve(text); const globalWithClipboardItem = global as unknown as { ClipboardItem?: unknown }; +afterEach(() => { + jest.restoreAllMocks(); + delete globalWithClipboardItem.ClipboardItem; +}); + test('uses Clipboard API writeText on non-Safari browsers', async () => { Object.defineProperty(navigator, 'userAgent', { value: CHROME_UA, @@ -60,8 +65,6 @@ test('uses ClipboardItem API on Safari browsers', async () => { expect(MockClipboardItem).toHaveBeenCalled(); expect(write).toHaveBeenCalledWith([expect.anything()]); - - delete globalWithClipboardItem.ClipboardItem; }); test('falls back to writeText on Safari when ClipboardItem write fails', async () => { @@ -81,8 +84,6 @@ test('falls back to writeText on Safari when ClipboardItem write fails', async ( await copyTextToClipboard(makeGetText('fallback text')); expect(writeText).toHaveBeenCalledWith('fallback text'); - - delete globalWithClipboardItem.ClipboardItem; }); function mockExecCommand(impl: (cmd: string) => boolean) { @@ -139,8 +140,6 @@ test('falls back to execCommand copy when Clipboard API is unavailable', async ( expect(document.execCommand).toHaveBeenCalledWith('copy'); expect(removeRange).toHaveBeenCalledWith(mockRange); - - jest.restoreAllMocks(); }); test('falls back to removeAllRanges when removeRange is not available', async () => { @@ -157,8 +156,6 @@ test('falls back to removeAllRanges when removeRange is not available', async () await copyTextToClipboard(makeGetText('no removeRange')); expect(removeAllRanges).toHaveBeenCalled(); - - jest.restoreAllMocks(); }); test('rejects when execCommand returns false', async () => { @@ -172,8 +169,6 @@ test('rejects when execCommand returns false', async () => { mockExecCommand(() => false); await expect(copyTextToClipboard(makeGetText('fail'))).rejects.toBeUndefined(); - - jest.restoreAllMocks(); }); test('rejects when execCommand throws', async () => { @@ -193,8 +188,6 @@ test('rejects when execCommand throws', async () => { }); await expect(copyTextToClipboard(makeGetText('throw'))).rejects.toBeUndefined(); - - jest.restoreAllMocks(); }); test('resolves without copying when getSelection returns null', async () => { @@ -211,7 +204,7 @@ test('resolves without copying when getSelection returns null', async () => { jest.spyOn(document, 'getSelection').mockReturnValue(null); - await expect(copyTextToClipboard(makeGetText('no selection'))).resolves.toBeUndefined(); - - jest.restoreAllMocks(); + await expect( + copyTextToClipboard(makeGetText('no selection')), + ).resolves.toBeUndefined(); });