diff --git a/exchanges/auth/src/authExchange.test.ts b/exchanges/auth/src/authExchange.test.ts index 8c2fdc1446..463caad7a1 100644 --- a/exchanges/auth/src/authExchange.test.ts +++ b/exchanges/auth/src/authExchange.test.ts @@ -19,7 +19,7 @@ import { Operation, OperationResult, } from '@urql/core'; -import { queryOperation } from '@urql/core/test-utils'; +import { queryOperation } from '../../../packages/core/src/test-utils'; const makeExchangeArgs = () => { const operations: Operation[] = []; diff --git a/exchanges/context/src/context.test.ts b/exchanges/context/src/context.test.ts index 41877d26cc..39759a2438 100644 --- a/exchanges/context/src/context.test.ts +++ b/exchanges/context/src/context.test.ts @@ -75,7 +75,7 @@ it(`calls getContext`, () => { expect(result).toHaveBeenCalledTimes(1); }); -it(`calls getContext async`, done => { +it(`calls getContext async`, async () => { const response = vi.fn( (forwardOp: Operation): OperationResult => { return { @@ -108,10 +108,12 @@ it(`calls getContext async`, done => { next(op); - setTimeout(() => { - expect(response).toHaveBeenCalledTimes(1); - expect(response.mock.calls[0][0].context.headers).toEqual(headers); - expect(result).toHaveBeenCalledTimes(1); - done(); - }, 10); + await new Promise(res => { + setTimeout(() => { + expect(response).toHaveBeenCalledTimes(1); + expect(response.mock.calls[0][0].context.headers).toEqual(headers); + expect(result).toHaveBeenCalledTimes(1); + res(); + }, 10); + }); }); diff --git a/exchanges/execute/src/execute.test.ts b/exchanges/execute/src/execute.test.ts index b63cb69088..2e75a086b4 100644 --- a/exchanges/execute/src/execute.test.ts +++ b/exchanges/execute/src/execute.test.ts @@ -26,7 +26,7 @@ import { context, queryOperation, subscriptionOperation, -} from '@urql/core/src/test-utils'; +} from '../../../packages/core/src/test-utils'; import { makeErrorResult, makeOperation, diff --git a/exchanges/request-policy/src/requestPolicyExchange.test.ts b/exchanges/request-policy/src/requestPolicyExchange.test.ts index 7a49e62837..94c4c34e3c 100644 --- a/exchanges/request-policy/src/requestPolicyExchange.test.ts +++ b/exchanges/request-policy/src/requestPolicyExchange.test.ts @@ -45,7 +45,7 @@ beforeEach(() => { ({ source: ops$, next } = makeSubject()); }); -it(`upgrades to cache-and-network`, done => { +it(`upgrades to cache-and-network`, async () => { const response = vi.fn( (forwardOp: Operation): OperationResult => { return { @@ -78,18 +78,20 @@ it(`upgrades to cache-and-network`, done => { ); expect(result).toHaveBeenCalledTimes(1); - setTimeout(() => { - next(op); - expect(response).toHaveBeenCalledTimes(2); - expect(response.mock.calls[1][0].context.requestPolicy).toEqual( - 'cache-and-network' - ); - expect(result).toHaveBeenCalledTimes(2); - done(); - }, 10); + await new Promise(res => { + setTimeout(() => { + next(op); + expect(response).toHaveBeenCalledTimes(2); + expect(response.mock.calls[1][0].context.requestPolicy).toEqual( + 'cache-and-network' + ); + expect(result).toHaveBeenCalledTimes(2); + res(); + }, 10); + }); }); -it(`doesn't upgrade when shouldUpgrade returns false`, done => { +it(`doesn't upgrade when shouldUpgrade returns false`, async () => { const response = vi.fn( (forwardOp: Operation): OperationResult => { return { @@ -123,14 +125,16 @@ it(`doesn't upgrade when shouldUpgrade returns false`, done => { ); expect(result).toHaveBeenCalledTimes(1); - setTimeout(() => { - next(op); - expect(response).toHaveBeenCalledTimes(2); - expect(response.mock.calls[1][0].context.requestPolicy).toEqual( - 'cache-first' - ); - expect(result).toHaveBeenCalledTimes(2); - expect(shouldUpgrade).toBeCalledTimes(2); - done(); - }, 10); + await new Promise(res => { + setTimeout(() => { + next(op); + expect(response).toHaveBeenCalledTimes(2); + expect(response.mock.calls[1][0].context.requestPolicy).toEqual( + 'cache-first' + ); + expect(result).toHaveBeenCalledTimes(2); + expect(shouldUpgrade).toBeCalledTimes(2); + res(); + }, 10); + }); }); diff --git a/packages/core/src/utils/request.test.ts b/packages/core/src/utils/request.test.ts index 8da185ee25..1f52afd1ac 100644 --- a/packages/core/src/utils/request.test.ts +++ b/packages/core/src/utils/request.test.ts @@ -1,7 +1,10 @@ -vi.mock('./hash', async () => ({ - hash: await vi.importActual('./hash').hash, - phash: (x: number) => x, -})); +vi.mock('./hash', async () => { + const hash = await vi.importActual('./hash'); + return { + ...hash, + phash: (x: number) => x, + }; +}); import { parse, print } from 'graphql'; import { gql } from '../gql'; diff --git a/packages/preact-urql/src/components/Mutation.test.tsx b/packages/preact-urql/src/components/Mutation.test.tsx index ef36208246..8aa5170b18 100644 --- a/packages/preact-urql/src/components/Mutation.test.tsx +++ b/packages/preact-urql/src/components/Mutation.test.tsx @@ -21,7 +21,7 @@ describe('Mutation', () => { cleanup(); }); - it('Should execute the mutation', done => { + it('Should execute the mutation', async () => { // eslint-disable-next-line let execute = () => {}, props = {}; @@ -60,9 +60,12 @@ describe('Mutation', () => { fetching: true, error: undefined, }); - setTimeout(() => { - expect(props).toStrictEqual({ data: 1, fetching: false, error: 2 }); - done(); - }, 400); + + await new Promise(res => { + setTimeout(() => { + expect(props).toStrictEqual({ data: 1, fetching: false, error: 2 }); + res(); + }, 400); + }); }); }); diff --git a/packages/preact-urql/src/components/Query.test.tsx b/packages/preact-urql/src/components/Query.test.tsx index ddcde3b424..62dc167280 100644 --- a/packages/preact-urql/src/components/Query.test.tsx +++ b/packages/preact-urql/src/components/Query.test.tsx @@ -27,7 +27,7 @@ describe('Query', () => { cleanup(); }); - it('Should execute the query', done => { + it('Should execute the query', async () => { let props = {}; const Test = () => h('p', {}, 'hi'); const App = () => { @@ -50,9 +50,12 @@ describe('Query', () => { fetching: true, error: undefined, }); - setTimeout(() => { - expect(props).toStrictEqual({ data: 0, fetching: false, error: 1 }); - done(); - }, 250); + + await new Promise(res => { + setTimeout(() => { + expect(props).toStrictEqual({ data: 0, fetching: false, error: 1 }); + res(); + }, 250); + }); }); }); diff --git a/packages/preact-urql/src/components/Subscription.test.tsx b/packages/preact-urql/src/components/Subscription.test.tsx index 4eb4995934..af738f2e4f 100644 --- a/packages/preact-urql/src/components/Subscription.test.tsx +++ b/packages/preact-urql/src/components/Subscription.test.tsx @@ -23,7 +23,7 @@ describe('Subscription', () => { cleanup(); }); - it('Should execute the subscription', done => { + it('Should execute the subscription', async () => { let props = {}; const Test = () => h('p', {}, 'hi'); const App = () => { @@ -46,9 +46,12 @@ describe('Subscription', () => { fetching: true, error: undefined, }); - setTimeout(() => { - expect(props).toStrictEqual({ data: 0, fetching: true, error: 1 }); - done(); - }, 300); + + await new Promise(res => { + setTimeout(() => { + expect(props).toStrictEqual({ data: 0, fetching: true, error: 1 }); + res(); + }, 300); + }); }); }); diff --git a/packages/preact-urql/src/hooks/useQuery.test.tsx b/packages/preact-urql/src/hooks/useQuery.test.tsx index edad850f3a..fa3a9f1858 100644 --- a/packages/preact-urql/src/hooks/useQuery.test.tsx +++ b/packages/preact-urql/src/hooks/useQuery.test.tsx @@ -95,7 +95,7 @@ describe('useQuery', () => { expect(state).toHaveProperty('fetching', true); }); - it('forwards data response', done => { + it('forwards data response', async () => { const { rerender } = render( h(Provider, { value: client as any, @@ -110,19 +110,21 @@ describe('useQuery', () => { }) ); - setTimeout(() => { - rerender( - h(Provider, { - value: client as any, - children: [h(QueryUser, { ...props })], - }) - ); - expect(state).toHaveProperty('data', 0); - done(); - }, 400); + await new Promise(res => { + setTimeout(() => { + rerender( + h(Provider, { + value: client as any, + children: [h(QueryUser, { ...props })], + }) + ); + expect(state).toHaveProperty('data', 0); + res(); + }, 400); + }); }); - it('forwards error response', done => { + it('forwards error response', async () => { const { rerender } = render( h(Provider, { value: client as any, @@ -137,19 +139,21 @@ describe('useQuery', () => { }) ); - setTimeout(() => { - rerender( - h(Provider, { - value: client as any, - children: [h(QueryUser, { ...props })], - }) - ); - expect(state).toHaveProperty('error', 1); - done(); - }, 400); + await new Promise(res => { + setTimeout(() => { + rerender( + h(Provider, { + value: client as any, + children: [h(QueryUser, { ...props })], + }) + ); + expect(state).toHaveProperty('error', 1); + res(); + }, 400); + }); }); - it('forwards extensions response', done => { + it('forwards extensions response', async () => { const { rerender } = render( h(Provider, { value: client as any, @@ -167,20 +171,22 @@ describe('useQuery', () => { }) ); - setTimeout(() => { - rerender( - h(Provider, { - value: client as any, - children: [h(QueryUser, { ...props })], - }) - ); + await new Promise(res => { + setTimeout(() => { + rerender( + h(Provider, { + value: client as any, + children: [h(QueryUser, { ...props })], + }) + ); - expect(state).toHaveProperty('extensions', { i: 1 }); - done(); - }, 400); + expect(state).toHaveProperty('extensions', { i: 1 }); + res(); + }, 400); + }); }); - it('sets fetching to false', done => { + it('sets fetching to false', async () => { const { rerender } = render( h(Provider, { value: client as any, @@ -195,16 +201,18 @@ describe('useQuery', () => { }) ); - setTimeout(() => { - rerender( - h(Provider, { - value: client as any, - children: [h(QueryUser, { ...props })], - }) - ); - expect(state).toHaveProperty('fetching', false); - done(); - }, 400); + await new Promise(res => { + setTimeout(() => { + rerender( + h(Provider, { + value: client as any, + children: [h(QueryUser, { ...props })], + }) + ); + expect(state).toHaveProperty('fetching', false); + res(); + }, 400); + }); }); describe('on change', () => { diff --git a/packages/react-urql/src/components/Mutation.test.tsx b/packages/react-urql/src/components/Mutation.test.tsx index 81911454ff..95870840fe 100644 --- a/packages/react-urql/src/components/Mutation.test.tsx +++ b/packages/react-urql/src/components/Mutation.test.tsx @@ -32,7 +32,7 @@ describe('Mutation', () => { cleanup(); }); - it('Should execute the mutation', done => { + it('Should execute the mutation', async () => { let execute = () => { /* noop */ }, @@ -65,9 +65,11 @@ describe('Mutation', () => { fetching: true, error: undefined, }); - setTimeout(() => { - expect(props).toStrictEqual({ data: 1, fetching: false, error: 2 }); - done(); - }, 400); + await new Promise(res => { + setTimeout(() => { + expect(props).toStrictEqual({ data: 1, fetching: false, error: 2 }); + res(); + }, 400); + }); }); }); diff --git a/packages/react-urql/src/components/Query.test.tsx b/packages/react-urql/src/components/Query.test.tsx index 7bbc3e9643..c50bb7c4c0 100644 --- a/packages/react-urql/src/components/Query.test.tsx +++ b/packages/react-urql/src/components/Query.test.tsx @@ -34,7 +34,7 @@ describe('Query', () => { cleanup(); }); - it('Should execute the query', done => { + it('Should execute the query', async () => { let props = {}; const Test = () =>

Hi

; const App = () => { @@ -54,9 +54,11 @@ describe('Query', () => { fetching: true, error: undefined, }); - setTimeout(() => { - expect(props).toStrictEqual({ data: 0, fetching: false, error: 1 }); - done(); - }, 200); + await new Promise(res => { + setTimeout(() => { + expect(props).toStrictEqual({ data: 0, fetching: false, error: 1 }); + res(); + }, 200); + }); }); }); diff --git a/packages/react-urql/src/hooks/useQuery.test.tsx b/packages/react-urql/src/hooks/useQuery.test.tsx index ed67e92b32..b1b9aee748 100644 --- a/packages/react-urql/src/hooks/useQuery.test.tsx +++ b/packages/react-urql/src/hooks/useQuery.test.tsx @@ -97,7 +97,7 @@ describe('on subscription', () => { }); describe('on subscription update', () => { - it('forwards data response', done => { + it('forwards data response', async () => { const wrapper = renderer.create(); /** * Have to call update (without changes) in order to see the @@ -105,14 +105,16 @@ describe('on subscription update', () => { */ wrapper.update(); - setTimeout(() => { - wrapper.update(); - expect(state).toHaveProperty('data', 0); - done(); - }, 400); + await new Promise(res => { + setTimeout(() => { + wrapper.update(); + expect(state).toHaveProperty('data', 0); + res(); + }, 400); + }); }); - it('forwards error response', done => { + it('forwards error response', async () => { const wrapper = renderer.create(); /** * Have to call update (without changes) in order to see the @@ -120,14 +122,16 @@ describe('on subscription update', () => { */ wrapper.update(); - setTimeout(() => { - wrapper.update(); - expect(state).toHaveProperty('error', 1); - done(); - }, 400); + await new Promise(res => { + setTimeout(() => { + wrapper.update(); + expect(state).toHaveProperty('error', 1); + res(); + }, 400); + }); }); - it('forwards extensions response', done => { + it('forwards extensions response', async () => { const wrapper = renderer.create(); /** * Have to call update (without changes) in order to see the @@ -135,14 +139,16 @@ describe('on subscription update', () => { */ wrapper.update(); - setTimeout(() => { - wrapper.update(); - expect(state).toHaveProperty('extensions', { i: 1 }); - done(); - }, 400); + await new Promise(res => { + setTimeout(() => { + wrapper.update(); + expect(state).toHaveProperty('extensions', { i: 1 }); + res(null); + }, 400); + }); }); - it('sets fetching to false', done => { + it('sets fetching to false', async () => { const wrapper = renderer.create(); /** * Have to call update (without changes) in order to see the @@ -150,11 +156,13 @@ describe('on subscription update', () => { */ wrapper.update(); - setTimeout(() => { - wrapper.update(); - expect(state).toHaveProperty('fetching', false); - done(); - }, 400); + await new Promise(res => { + setTimeout(() => { + wrapper.update(); + expect(state).toHaveProperty('fetching', false); + res(null); + }, 400); + }); }); }); diff --git a/packages/react-urql/src/hooks/useSubscription.test.tsx b/packages/react-urql/src/hooks/useSubscription.test.tsx index 2d115d56f8..f9152ff68e 100644 --- a/packages/react-urql/src/hooks/useSubscription.test.tsx +++ b/packages/react-urql/src/hooks/useSubscription.test.tsx @@ -1,9 +1,9 @@ /* eslint-disable react-hooks/rules-of-hooks */ // Note: Testing for hooks is not yet supported in Enzyme - https://github.com/airbnb/enzyme/issues/2011 -vi.mock('../context', () => { +vi.mock('../context', async () => { const d = { data: 1234, error: 5678 }; - const { merge, fromValue, never } = vi.importActual('wonka'); + const { merge, fromValue, never } = await vi.importActual('wonka'); const mock = { executeSubscription: vi.fn(() => merge([fromValue(d), never])), }; diff --git a/packages/vue-urql/src/useMutation.test.ts b/packages/vue-urql/src/useMutation.test.ts index 987501d710..66cb9a6135 100644 --- a/packages/vue-urql/src/useMutation.test.ts +++ b/packages/vue-urql/src/useMutation.test.ts @@ -20,7 +20,7 @@ beforeEach(() => { }); describe('useMutation', () => { - it('provides an execute method that resolves a promise', done => { + it('provides an execute method that resolves a promise', async () => { const subject = makeSubject(); const clientMutation = vi .spyOn(client, 'executeMutation') @@ -55,12 +55,11 @@ describe('useMutation', () => { expect(clientMutation).toHaveBeenCalledTimes(1); subject.next({ data: { test: true } }); - promise.then(function () { + await promise.then(function () { expect(mutation.fetching).toBe(false); expect(mutation.stale).toBe(false); expect(mutation.error).toBe(undefined); expect(mutation.data).toEqual({ test: true }); - done(); }); }); }); diff --git a/packages/vue-urql/src/useQuery.test.ts b/packages/vue-urql/src/useQuery.test.ts index 58b371b043..243e76e05b 100644 --- a/packages/vue-urql/src/useQuery.test.ts +++ b/packages/vue-urql/src/useQuery.test.ts @@ -1,6 +1,6 @@ import { nextTick, reactive, ref } from 'vue'; -vi.mock('./useClient.ts', () => ({ +vi.mock('./useClient.ts', async () => ({ __esModule: true, ...(await vi.importActual('./useClient.ts')), useClient: () => ref(client), diff --git a/packages/vue-urql/src/useSubscription.test.ts b/packages/vue-urql/src/useSubscription.test.ts index ff23a13714..2abba35370 100644 --- a/packages/vue-urql/src/useSubscription.test.ts +++ b/packages/vue-urql/src/useSubscription.test.ts @@ -1,6 +1,6 @@ import { nextTick, reactive, ref } from 'vue'; -vi.mock('./useClient.ts', () => ({ +vi.mock('./useClient.ts', async () => ({ __esModule: true, ...(await vi.importActual('./useClient.ts')), useClient: () => ref(client),