From 7f1bf3d2d9ce9278ba578b064bbc0b701a337aa2 Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Fri, 4 Oct 2024 13:58:42 +0200 Subject: [PATCH 1/6] Improve cleanup of tests --- src/hooks/useAsyncValue.ts | 6 +- tests/_utils/turnOffErrors.ts | 20 +++++++ tests/_utils/turnoffdefaulterrorcatching.js | 19 ------- tests/ckeditor.test.tsx | 33 ++++------- tests/cloud/useCKEditorCloud.test.tsx | 18 +++--- tests/cloud/withCKEditorCloud.test.tsx | 2 +- tests/context/ckeditorcontext.test.tsx | 25 +++++---- tests/useMultiRootEditor.test.tsx | 62 ++++++++++----------- vitest-setup.ts | 4 ++ 9 files changed, 91 insertions(+), 98 deletions(-) create mode 100644 tests/_utils/turnOffErrors.ts delete mode 100644 tests/_utils/turnoffdefaulterrorcatching.js diff --git a/src/hooks/useAsyncValue.ts b/src/hooks/useAsyncValue.ts index a71b836d..63114ce1 100644 --- a/src/hooks/useAsyncValue.ts +++ b/src/hooks/useAsyncValue.ts @@ -5,8 +5,8 @@ import type { DependencyList } from 'react'; -import { useInstantEffect } from './useInstantEffect.js'; import { useAsyncCallback, type AsyncCallbackState } from './useAsyncCallback.js'; +import { useInstantEffect } from './useInstantEffect.js'; /** * A hook that allows to execute an asynchronous function and provides the state of the execution. @@ -37,8 +37,8 @@ import { useAsyncCallback, type AsyncCallbackState } from './useAsyncCallback.js * } * ``` */ -export const useAsyncValue = , R>( - callback: ( ...args: A ) => Promise, +export const useAsyncValue = ( + callback: () => Promise, deps: DependencyList ): AsyncValueHookResult => { const [ asyncCallback, asyncState ] = useAsyncCallback( callback ); diff --git a/tests/_utils/turnOffErrors.ts b/tests/_utils/turnOffErrors.ts new file mode 100644 index 00000000..a99f7ae2 --- /dev/null +++ b/tests/_utils/turnOffErrors.ts @@ -0,0 +1,20 @@ +/** + * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved. + * For licensing, see LICENSE.md. + */ + +import type { Awaitable } from '@ckeditor/ckeditor5-integrations-common'; + +export async function turnOffErrors( callback: () => Awaitable ): Promise { + const handler = ( evt: ErrorEvent ) => { + evt.preventDefault(); + }; + + window.addEventListener( 'error', handler, { capture: true, once: true } ); + + try { + await callback(); + } finally { + window.removeEventListener( 'error', handler ); + } +} diff --git a/tests/_utils/turnoffdefaulterrorcatching.js b/tests/_utils/turnoffdefaulterrorcatching.js deleted file mode 100644 index c2bfb30b..00000000 --- a/tests/_utils/turnoffdefaulterrorcatching.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved. - * For licensing, see LICENSE.md. - */ - -/* global window */ - -/** - * Turns off the default error catching - * so Mocha won't complain about errors caused by the called function. - */ -export default async function turnOffDefaultErrorCatching( fn ) { - const originalOnError = window.onerror; - window.onerror = () => {}; - - await fn(); - - window.onerror = originalOnError; -} diff --git a/tests/ckeditor.test.tsx b/tests/ckeditor.test.tsx index 81c862df..7c69c052 100644 --- a/tests/ckeditor.test.tsx +++ b/tests/ckeditor.test.tsx @@ -13,12 +13,12 @@ import MockedEditor from './_utils/editor.js'; import { timeout } from './_utils/timeout.js'; import { createDefer } from './_utils/defer.js'; import { PromiseManager } from './_utils/promisemanager.js'; -import turnOffDefaultErrorCatching from './_utils/turnoffdefaulterrorcatching.js'; import CKEditor, { type Props } from '../src/ckeditor.js'; import { expectToBeTruthy } from './_utils/expectToBeTruthy.js'; import type { LifeCycleElementSemaphore } from '../src/lifecycle/LifeCycleElementSemaphore.js'; import type { EditorSemaphoreMountResult } from '../src/lifecycle/LifeCycleEditorSemaphore.js'; +import { turnOffErrors } from './_utils/turnOffErrors.js'; const MockEditor = MockedEditor as any; @@ -1055,29 +1055,20 @@ describe( ' Component', () => { expect( firstEditor ).to.be.instanceOf( MockEditor ); - await turnOffDefaultErrorCatching( () => { - return new Promise( res => { - component?.rerender( - - ); - - setTimeout( () => { - throw new CKEditorError( 'foo', firstEditor ); - } ); + await manager.all(); + await turnOffErrors( async () => { + setTimeout( () => { + throw new CKEditorError( 'foo', firstEditor ); } ); - } ); - await waitFor( () => { - const { editor } = instanceRef.current!; + await timeout( 10 ); + await waitFor( () => { + const { editor } = instanceRef.current!; - expect( editor ).to.be.instanceOf( MockEditor ); - expect( firstEditor ).to.not.equal( editor ); - expect( onAfterDestroySpy ).toHaveBeenCalledOnce(); + expect( editor ).to.be.instanceOf( MockEditor ); + expect( firstEditor ).to.not.equal( editor ); + expect( onAfterDestroySpy ).toHaveBeenCalledOnce(); + } ); } ); } ); } ); diff --git a/tests/cloud/useCKEditorCloud.test.tsx b/tests/cloud/useCKEditorCloud.test.tsx index bdd7ac12..a99dc41a 100644 --- a/tests/cloud/useCKEditorCloud.test.tsx +++ b/tests/cloud/useCKEditorCloud.test.tsx @@ -11,7 +11,7 @@ import { removeAllCkCdnResources } from '@ckeditor/ckeditor5-integrations-common import useCKEditorCloud from '../../src/cloud/useCKEditorCloud.js'; -describe( 'useCKEditorCloud', () => { +describe( 'useCKEditorCloud', { timeout: 8000 }, () => { afterEach( () => { cleanup(); removeAllCkCdnResources(); @@ -31,7 +31,7 @@ describe( 'useCKEditorCloud', () => { if ( result.current.status === 'success' ) { expect( result.current.CKEditor ).toBeDefined(); } - } ); + }, { timeout: 5000 } ); } ); it( 'should load additional bundle after updating deps', async () => { @@ -52,7 +52,7 @@ describe( 'useCKEditorCloud', () => { expect( result.current.CKEditor ).toBeDefined(); expect( result.current.CKEditorPremiumFeatures ).toBeUndefined(); } - } ); + }, { timeout: 5000 } ); rerender( { version: '43.0.0', @@ -70,7 +70,7 @@ describe( 'useCKEditorCloud', () => { expect( result.current.CKEditor ).toBeDefined(); expect( result.current.CKEditorPremiumFeatures ).toBeDefined(); } - } ); + }, { timeout: 5000 } ); } ); describe( 'typings', () => { @@ -82,7 +82,7 @@ describe( 'useCKEditorCloud', () => { await waitFor( () => { expect( result.current.status ).toBe( 'success' ); - } ); + }, { timeout: 5000 } ); if ( result.current.status === 'success' ) { expectTypeOf( result.current.CKEditorPremiumFeatures ).not.toBeNullable(); @@ -97,7 +97,7 @@ describe( 'useCKEditorCloud', () => { await waitFor( () => { expect( result.current.status ).toBe( 'success' ); - } ); + }, { timeout: 5000 } ); if ( result.current.status === 'success' ) { expectTypeOf( result.current.CKEditorPremiumFeatures ).toBeNullable(); @@ -111,7 +111,7 @@ describe( 'useCKEditorCloud', () => { await waitFor( () => { expect( result.current.status ).toBe( 'success' ); - } ); + }, { timeout: 5000 } ); if ( result.current.status === 'success' ) { expectTypeOf( result.current.CKEditorPremiumFeatures ).toBeNullable(); @@ -128,7 +128,7 @@ describe( 'useCKEditorCloud', () => { await waitFor( () => { expect( result.current.status ).toBe( 'success' ); - } ); + }, { timeout: 5000 } ); if ( result.current.status === 'success' ) { expectTypeOf( result.current.CKBox ).not.toBeNullable(); @@ -142,7 +142,7 @@ describe( 'useCKEditorCloud', () => { await waitFor( () => { expect( result.current.status ).toBe( 'success' ); - } ); + }, { timeout: 5000 } ); if ( result.current.status === 'success' ) { expectTypeOf( result.current.CKBox ).toBeNullable(); diff --git a/tests/cloud/withCKEditorCloud.test.tsx b/tests/cloud/withCKEditorCloud.test.tsx index bb9d9923..736f0970 100644 --- a/tests/cloud/withCKEditorCloud.test.tsx +++ b/tests/cloud/withCKEditorCloud.test.tsx @@ -12,7 +12,7 @@ import { removeAllCkCdnResources } from '@ckeditor/ckeditor5-integrations-common import withCKEditorCloud, { type WithCKEditorCloudHocProps } from '../../src/cloud/withCKEditorCloud.js'; -describe( 'withCKEditorCloud', () => { +describe( 'withCKEditorCloud', { timeout: 5000 }, () => { const lastRenderedMockProps: MutableRefObject = { current: null }; diff --git a/tests/context/ckeditorcontext.test.tsx b/tests/context/ckeditorcontext.test.tsx index 7338c52e..4d347cbd 100644 --- a/tests/context/ckeditorcontext.test.tsx +++ b/tests/context/ckeditorcontext.test.tsx @@ -16,10 +16,10 @@ import CKEditorContext, { import CKEditor from '../../src/ckeditor.js'; import MockedEditor from '../_utils/editor.js'; import { ClassicEditor, ContextWatchdog, CKEditorError } from 'ckeditor5'; -import turnOffDefaultErrorCatching from '../_utils/turnoffdefaulterrorcatching.js'; import ContextMock, { DeferredContextMock } from '../_utils/context.js'; import { timeout } from '../_utils/timeout.js'; import { PromiseManager } from '../_utils/promisemanager.js'; +import { turnOffErrors } from '../_utils/turnOffErrors.js'; const MockEditor = MockedEditor as any; @@ -303,26 +303,27 @@ describe( ' Component', () => { } ); const { watchdog } = contextRef.current as ExtractContextWatchdogValueByStatus<'initialized'>; - const error = new CKEditorError( 'foo', watchdog.context ); - await turnOffDefaultErrorCatching( async () => { + await turnOffErrors( async () => { + const error = new CKEditorError( 'foo', watchdog.context ); + setTimeout( () => { throw error; } ); await timeout( 150 ); - } ); - expect( onErrorSpy ).toHaveBeenCalledOnce(); - const errorEventArgs = onErrorSpy.mock.calls[ 0 ]; + expect( onErrorSpy ).toHaveBeenCalledOnce(); + const errorEventArgs = onErrorSpy.mock.calls[ 0 ]; - expect( errorEventArgs[ 0 ] ).to.equal( error ); - expect( errorEventArgs[ 1 ] ).to.deep.equal( { - phase: 'runtime', - willContextRestart: true - } ); + expect( errorEventArgs[ 0 ] ).to.equal( error ); + expect( errorEventArgs[ 1 ] ).to.deep.equal( { + phase: 'runtime', + willContextRestart: true + } ); - expect( contextRef.current!.status ).to.be.equal( 'initialized' ); + expect( contextRef.current!.status ).to.be.equal( 'initialized' ); + } ); } ); it( 'displays an error if something went wrong and "onError" callback was not specified', async () => { diff --git a/tests/useMultiRootEditor.test.tsx b/tests/useMultiRootEditor.test.tsx index 8f82e0ca..7252a519 100644 --- a/tests/useMultiRootEditor.test.tsx +++ b/tests/useMultiRootEditor.test.tsx @@ -14,7 +14,7 @@ import { ContextWatchdogContext } from '../src/context/ckeditorcontext.js'; import { timeout } from './_utils/timeout.js'; import { createDefer } from './_utils/defer.js'; import { createTestMultiRootWatchdog, TestMultiRootEditor } from './_utils/multirooteditor.js'; -import turnOffDefaultErrorCatching from './_utils/turnoffdefaulterrorcatching.js'; +import { turnOffErrors } from './_utils/turnOffErrors.js'; describe( 'useMultiRootEditor', () => { const rootsContent = { @@ -120,16 +120,16 @@ describe( 'useMultiRootEditor', () => { // Mock the error. vi.spyOn( editor!, 'focus' ).mockImplementation( async () => { - await turnOffDefaultErrorCatching( () => { - return new Promise( () => { - setTimeout( () => { - throw new CKEditorError( 'a-custom-error', editor ); - } ); - } ); + setTimeout( () => { + throw new CKEditorError( 'a-custom-error', editor ); } ); } ); - // Throw the error. + const handler = ( evt: ErrorEvent ) => { + evt.preventDefault(); + }; + + window.addEventListener( 'error', handler, { capture: true, once: true } ); editor!.focus(); await waitFor( () => { @@ -208,24 +208,23 @@ describe( 'useMultiRootEditor', () => { const { editor, toolbarElement } = result.current; + // Mock the error. vi.spyOn( editor!, 'focus' ).mockImplementation( async () => { - await turnOffDefaultErrorCatching( () => { - return new Promise( () => { - setTimeout( () => { - throw new CKEditorError( 'a-custom-error', editor ); - } ); - } ); + setTimeout( () => { + throw new CKEditorError( 'a-custom-error', editor ); } ); } ); - // Throw the error. - editor!.focus(); + await turnOffErrors( async () => { + editor!.focus(); - await waitFor( () => { - const { toolbarElement: newToolbarElement } = result.current; + await timeout( 10 ); + await waitFor( () => { + const { toolbarElement: newToolbarElement } = result.current; - expect( newToolbarElement ).to.be.exist; - expect( newToolbarElement ).to.not.be.equal( toolbarElement ); + expect( newToolbarElement ).to.be.exist; + expect( newToolbarElement ).to.not.be.equal( toolbarElement ); + } ); } ); } ); } ); @@ -724,24 +723,21 @@ describe( 'useMultiRootEditor', () => { expect( result.current.editor ).to.be.instanceof( TestMultiRootEditor ); } ); - const { editor } = result.current; - // Mock the error. - vi.spyOn( editor!, 'focus' ).mockImplementation( async () => { - await turnOffDefaultErrorCatching( () => { - return new Promise( () => { - setTimeout( () => { - throw new CKEditorError( 'a-custom-error', editor ); - } ); + await turnOffErrors( async () => { + const { editor } = result.current; + + vi.spyOn( editor!, 'focus' ).mockImplementation( async () => { + setTimeout( () => { + throw new CKEditorError( 'a-custom-error', editor ); } ); } ); - } ); - // Throw the error. - editor!.focus(); + editor!.focus(); - await waitFor( () => { - expect( onAfterDestroyMock ).toHaveBeenCalledOnce(); + await waitFor( () => { + expect( onAfterDestroyMock ).toHaveBeenCalledOnce(); + } ); } ); } ); diff --git a/vitest-setup.ts b/vitest-setup.ts index bbd99e5f..b4b3227a 100644 --- a/vitest-setup.ts +++ b/vitest-setup.ts @@ -4,3 +4,7 @@ */ import '@testing-library/jest-dom/vitest'; +import { cleanup } from '@testing-library/react'; +import { beforeEach } from 'vitest'; + +beforeEach( cleanup ); From 9ad188e815ef1b1dcc80f932c838ce4d04d79869 Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Fri, 4 Oct 2024 14:09:18 +0200 Subject: [PATCH 2/6] Improve testing --- tests/_utils/turnOffErrors.ts | 2 ++ tests/ckeditor.test.tsx | 28 +++++++++++++++++--------- tests/context/ckeditorcontext.test.tsx | 20 +++++++++--------- tests/useMultiRootEditor.test.tsx | 26 ++++++++++-------------- 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/tests/_utils/turnOffErrors.ts b/tests/_utils/turnOffErrors.ts index a99f7ae2..340b683e 100644 --- a/tests/_utils/turnOffErrors.ts +++ b/tests/_utils/turnOffErrors.ts @@ -4,6 +4,7 @@ */ import type { Awaitable } from '@ckeditor/ckeditor5-integrations-common'; +import { timeout } from './timeout.js'; export async function turnOffErrors( callback: () => Awaitable ): Promise { const handler = ( evt: ErrorEvent ) => { @@ -14,6 +15,7 @@ export async function turnOffErrors( callback: () => Awaitable ): Promise< try { await callback(); + await timeout( 150 ); } finally { window.removeEventListener( 'error', handler ); } diff --git a/tests/ckeditor.test.tsx b/tests/ckeditor.test.tsx index 7c69c052..9e80f754 100644 --- a/tests/ckeditor.test.tsx +++ b/tests/ckeditor.test.tsx @@ -1057,18 +1057,28 @@ describe( ' Component', () => { await manager.all(); await turnOffErrors( async () => { - setTimeout( () => { - throw new CKEditorError( 'foo', firstEditor ); + await new Promise( resolve => { + component?.rerender( + + ); + + setTimeout( () => { + throw new CKEditorError( 'foo', firstEditor ); + } ); } ); + } ); - await timeout( 10 ); - await waitFor( () => { - const { editor } = instanceRef.current!; + await waitFor( () => { + const { editor } = instanceRef.current!; - expect( editor ).to.be.instanceOf( MockEditor ); - expect( firstEditor ).to.not.equal( editor ); - expect( onAfterDestroySpy ).toHaveBeenCalledOnce(); - } ); + expect( editor ).to.be.instanceOf( MockEditor ); + expect( firstEditor ).to.not.equal( editor ); + expect( onAfterDestroySpy ).toHaveBeenCalledOnce(); } ); } ); } ); diff --git a/tests/context/ckeditorcontext.test.tsx b/tests/context/ckeditorcontext.test.tsx index 4d347cbd..00fe6f5e 100644 --- a/tests/context/ckeditorcontext.test.tsx +++ b/tests/context/ckeditorcontext.test.tsx @@ -310,20 +310,18 @@ describe( ' Component', () => { setTimeout( () => { throw error; } ); + } ); - await timeout( 150 ); - - expect( onErrorSpy ).toHaveBeenCalledOnce(); - const errorEventArgs = onErrorSpy.mock.calls[ 0 ]; - - expect( errorEventArgs[ 0 ] ).to.equal( error ); - expect( errorEventArgs[ 1 ] ).to.deep.equal( { - phase: 'runtime', - willContextRestart: true - } ); + expect( onErrorSpy ).toHaveBeenCalledOnce(); + const errorEventArgs = onErrorSpy.mock.calls[ 0 ]; - expect( contextRef.current!.status ).to.be.equal( 'initialized' ); + expect( errorEventArgs[ 0 ] ).to.instanceOf( Error ); + expect( errorEventArgs[ 1 ] ).to.deep.equal( { + phase: 'runtime', + willContextRestart: true } ); + + expect( contextRef.current!.status ).to.be.equal( 'initialized' ); } ); it( 'displays an error if something went wrong and "onError" callback was not specified', async () => { diff --git a/tests/useMultiRootEditor.test.tsx b/tests/useMultiRootEditor.test.tsx index 7252a519..61f3644d 100644 --- a/tests/useMultiRootEditor.test.tsx +++ b/tests/useMultiRootEditor.test.tsx @@ -125,12 +125,9 @@ describe( 'useMultiRootEditor', () => { } ); } ); - const handler = ( evt: ErrorEvent ) => { - evt.preventDefault(); - }; - - window.addEventListener( 'error', handler, { capture: true, once: true } ); - editor!.focus(); + await turnOffErrors( async () => { + editor!.focus(); + } ); await waitFor( () => { const { editor: newEditor, data: newData, attributes: newAttributes } = result.current; @@ -217,14 +214,13 @@ describe( 'useMultiRootEditor', () => { await turnOffErrors( async () => { editor!.focus(); + } ); - await timeout( 10 ); - await waitFor( () => { - const { toolbarElement: newToolbarElement } = result.current; + await waitFor( () => { + const { toolbarElement: newToolbarElement } = result.current; - expect( newToolbarElement ).to.be.exist; - expect( newToolbarElement ).to.not.be.equal( toolbarElement ); - } ); + expect( newToolbarElement ).to.be.exist; + expect( newToolbarElement ).to.not.be.equal( toolbarElement ); } ); } ); } ); @@ -734,10 +730,10 @@ describe( 'useMultiRootEditor', () => { } ); editor!.focus(); + } ); - await waitFor( () => { - expect( onAfterDestroyMock ).toHaveBeenCalledOnce(); - } ); + await waitFor( () => { + expect( onAfterDestroyMock ).toHaveBeenCalledOnce(); } ); } ); From 0460bcd7578a81595968f08d8d4cf1257975560b Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Fri, 4 Oct 2024 14:14:00 +0200 Subject: [PATCH 3/6] Fix coverage --- src/useMultiRootEditor.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/useMultiRootEditor.tsx b/src/useMultiRootEditor.tsx index 301550ae..ac16a3e6 100644 --- a/src/useMultiRootEditor.tsx +++ b/src/useMultiRootEditor.tsx @@ -396,6 +396,7 @@ const useMultiRootEditor = ( props: MultiRootHookProps ): MultiRootHookReturns = } ); setTimeout( () => { + /* istanbul ignore next -- @preserve */ if ( props.onReady ) { props.onReady( watchdog!.editor ); } From 99c233298bc83b0108b5f715a4829083f593ad3e Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Fri, 4 Oct 2024 14:53:00 +0200 Subject: [PATCH 4/6] Fix flaky test --- tests/useMultiRootEditor.test.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/useMultiRootEditor.test.tsx b/tests/useMultiRootEditor.test.tsx index 61f3644d..1293a151 100644 --- a/tests/useMultiRootEditor.test.tsx +++ b/tests/useMultiRootEditor.test.tsx @@ -598,8 +598,10 @@ describe( 'useMultiRootEditor', () => { const newRootsAttributes: Record = { ...rootsAttributes }; delete newRootsAttributes.intro; - act( () => { - setAttributes( { ...newRootsAttributes } ); + await turnOffErrors( () => { + act( () => { + setAttributes( { ...newRootsAttributes } ); + } ); } ); expect( console.error ).toHaveBeenCalledWith( '`data` and `attributes` objects must have the same keys (roots).' ); From 797815ba1b22b06bb00907b08102f0e69c96e70a Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Fri, 4 Oct 2024 14:58:32 +0200 Subject: [PATCH 5/6] fix cleanups --- tests/cloud/useCKEditorCloud.test.tsx | 3 +-- tests/cloud/withCKEditorCloud.test.tsx | 3 +-- tests/hooks/useAsyncCallback.test.tsx | 6 ++---- tests/hooks/useAsyncValue.test.tsx | 6 ++---- tests/hooks/useInstantEditorEffect.test.tsx | 6 ++---- tests/hooks/useInstantEffect.test.tsx | 6 ++---- tests/hooks/useIsMountedRef.test.tsx | 6 ++---- tests/hooks/useIsUnmountedRef.test.tsx | 6 ++---- tests/hooks/useRefSafeCallback.test.tsx | 6 ++---- 9 files changed, 16 insertions(+), 32 deletions(-) diff --git a/tests/cloud/useCKEditorCloud.test.tsx b/tests/cloud/useCKEditorCloud.test.tsx index a99dc41a..3b71a0ce 100644 --- a/tests/cloud/useCKEditorCloud.test.tsx +++ b/tests/cloud/useCKEditorCloud.test.tsx @@ -4,7 +4,7 @@ */ import { afterEach, describe, expect, expectTypeOf, it } from 'vitest'; -import { renderHook, waitFor, act, cleanup } from '@testing-library/react'; +import { renderHook, waitFor, act } from '@testing-library/react'; import type { CKEditorCloudConfig } from '@ckeditor/ckeditor5-integrations-common'; import { removeAllCkCdnResources } from '@ckeditor/ckeditor5-integrations-common/test-utils'; @@ -13,7 +13,6 @@ import useCKEditorCloud from '../../src/cloud/useCKEditorCloud.js'; describe( 'useCKEditorCloud', { timeout: 8000 }, () => { afterEach( () => { - cleanup(); removeAllCkCdnResources(); } ); diff --git a/tests/cloud/withCKEditorCloud.test.tsx b/tests/cloud/withCKEditorCloud.test.tsx index 736f0970..2f5431ab 100644 --- a/tests/cloud/withCKEditorCloud.test.tsx +++ b/tests/cloud/withCKEditorCloud.test.tsx @@ -5,7 +5,7 @@ import React, { type MutableRefObject } from 'react'; import { afterEach, describe, expect, it } from 'vitest'; -import { cleanup, render } from '@testing-library/react'; +import { render } from '@testing-library/react'; import { createDefer } from '@ckeditor/ckeditor5-integrations-common'; import { removeAllCkCdnResources } from '@ckeditor/ckeditor5-integrations-common/test-utils'; @@ -18,7 +18,6 @@ describe( 'withCKEditorCloud', { timeout: 5000 }, () => { }; afterEach( () => { - cleanup(); removeAllCkCdnResources(); lastRenderedMockProps.current = null; } ); diff --git a/tests/hooks/useAsyncCallback.test.tsx b/tests/hooks/useAsyncCallback.test.tsx index bd8ff77e..44678d5b 100644 --- a/tests/hooks/useAsyncCallback.test.tsx +++ b/tests/hooks/useAsyncCallback.test.tsx @@ -3,14 +3,12 @@ * For licensing, see LICENSE.md. */ -import { afterEach, describe, expect, it, vi } from 'vitest'; -import { renderHook, act, waitFor, cleanup } from '@testing-library/react'; +import { describe, expect, it, vi } from 'vitest'; +import { renderHook, act, waitFor } from '@testing-library/react'; import { useAsyncCallback } from '../../src/hooks/useAsyncCallback.js'; import { timeout } from '../_utils/timeout.js'; describe( 'useAsyncCallback', () => { - afterEach( cleanup ); - it( 'should execute the callback and update the state correctly when the callback resolves', async () => { const fetchData = vi.fn().mockResolvedValue( 'data' ); diff --git a/tests/hooks/useAsyncValue.test.tsx b/tests/hooks/useAsyncValue.test.tsx index fefeae62..f90af4a9 100644 --- a/tests/hooks/useAsyncValue.test.tsx +++ b/tests/hooks/useAsyncValue.test.tsx @@ -3,13 +3,11 @@ * For licensing, see LICENSE.md. */ -import { afterEach, describe, expect, it } from 'vitest'; -import { cleanup, renderHook, waitFor } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; +import { renderHook, waitFor } from '@testing-library/react'; import { useAsyncValue } from '../../src/hooks/useAsyncValue.js'; describe( 'useAsyncValue', () => { - afterEach( cleanup ); - it( 'should return a mutable ref object', async () => { const { result } = renderHook( () => useAsyncValue( async () => 123, [] ) ); diff --git a/tests/hooks/useInstantEditorEffect.test.tsx b/tests/hooks/useInstantEditorEffect.test.tsx index f219e80b..b106b752 100644 --- a/tests/hooks/useInstantEditorEffect.test.tsx +++ b/tests/hooks/useInstantEditorEffect.test.tsx @@ -3,13 +3,11 @@ * For licensing, see LICENSE.md. */ -import { afterEach, describe, expect, it, vi } from 'vitest'; -import { cleanup, renderHook } from '@testing-library/react'; +import { describe, expect, it, vi } from 'vitest'; +import { renderHook } from '@testing-library/react'; import { useInstantEditorEffect } from '../../src/hooks/useInstantEditorEffect.js'; describe( 'useInstantEditorEffect', () => { - afterEach( cleanup ); - it( 'should execute the provided function after mounting of editor', () => { const semaphore = { runAfterMount: vi.fn() diff --git a/tests/hooks/useInstantEffect.test.tsx b/tests/hooks/useInstantEffect.test.tsx index 8ca80565..f808a41b 100644 --- a/tests/hooks/useInstantEffect.test.tsx +++ b/tests/hooks/useInstantEffect.test.tsx @@ -3,13 +3,11 @@ * For licensing, see LICENSE.md. */ -import { afterEach, describe, expect, it, vi } from 'vitest'; -import { cleanup, renderHook } from '@testing-library/react'; +import { describe, expect, it, vi } from 'vitest'; +import { renderHook } from '@testing-library/react'; import { useInstantEffect } from '../../src/hooks/useInstantEffect.js'; describe( 'useInstantEffect', () => { - afterEach( cleanup ); - it( 'should call the effect function when dependencies change', () => { const effectFn = vi.fn(); const { rerender } = renderHook( deps => useInstantEffect( effectFn, deps ), { diff --git a/tests/hooks/useIsMountedRef.test.tsx b/tests/hooks/useIsMountedRef.test.tsx index 03e8968a..87d8b1df 100644 --- a/tests/hooks/useIsMountedRef.test.tsx +++ b/tests/hooks/useIsMountedRef.test.tsx @@ -3,13 +3,11 @@ * For licensing, see LICENSE.md. */ -import { afterEach, describe, expect, it } from 'vitest'; -import { cleanup, renderHook } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; +import { renderHook } from '@testing-library/react'; import { useIsMountedRef } from '../../src/hooks/useIsMountedRef.js'; describe( 'useIsMountedRef', () => { - afterEach( cleanup ); - it( 'should return a mutable ref object', () => { const { result } = renderHook( () => useIsMountedRef() ); diff --git a/tests/hooks/useIsUnmountedRef.test.tsx b/tests/hooks/useIsUnmountedRef.test.tsx index 8369932c..087d98d7 100644 --- a/tests/hooks/useIsUnmountedRef.test.tsx +++ b/tests/hooks/useIsUnmountedRef.test.tsx @@ -3,13 +3,11 @@ * For licensing, see LICENSE.md. */ -import { afterEach, describe, expect, it } from 'vitest'; -import { cleanup, renderHook } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; +import { renderHook } from '@testing-library/react'; import { useIsUnmountedRef } from '../../src/hooks/useIsUnmountedRef.js'; describe( 'useIsUnmountedRef', () => { - afterEach( cleanup ); - it( 'should return a mutable ref object', () => { const { result } = renderHook( () => useIsUnmountedRef() ); diff --git a/tests/hooks/useRefSafeCallback.test.tsx b/tests/hooks/useRefSafeCallback.test.tsx index ee8a3226..3b8fa4a1 100644 --- a/tests/hooks/useRefSafeCallback.test.tsx +++ b/tests/hooks/useRefSafeCallback.test.tsx @@ -3,13 +3,11 @@ * For licensing, see LICENSE.md. */ -import { expect, it, describe, vi, afterEach } from 'vitest'; -import { renderHook, act, cleanup } from '@testing-library/react'; +import { expect, it, describe, vi } from 'vitest'; +import { renderHook, act } from '@testing-library/react'; import { useRefSafeCallback } from '../../src/hooks/useRefSafeCallback.js'; describe( 'useRefSafeCallback', () => { - afterEach( cleanup ); - it( 'should return a function', () => { const { result } = renderHook( () => useRefSafeCallback( () => {} ) ); From d6955fe4cfc93f24d9b861c7aba9c18920796055 Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Fri, 4 Oct 2024 15:02:37 +0200 Subject: [PATCH 6/6] Enforce cleanups twice --- vitest-setup.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vitest-setup.ts b/vitest-setup.ts index b4b3227a..7643b274 100644 --- a/vitest-setup.ts +++ b/vitest-setup.ts @@ -5,6 +5,7 @@ import '@testing-library/jest-dom/vitest'; import { cleanup } from '@testing-library/react'; -import { beforeEach } from 'vitest'; +import { beforeEach, afterEach } from 'vitest'; beforeEach( cleanup ); +afterEach( cleanup );