Skip to content

Commit

Permalink
test(workspace): Speed up slowest vitest tests (#3171)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten authored Apr 20, 2023
1 parent 8b37c22 commit 40cdb73
Show file tree
Hide file tree
Showing 19 changed files with 321 additions and 252 deletions.
6 changes: 5 additions & 1 deletion exchanges/graphcache/src/offlineExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
OperationResult,
} from '@urql/core';
import { print } from 'graphql';
import { vi, expect, it, describe } from 'vitest';
import { vi, expect, it, describe, beforeAll } from 'vitest';

import { pipe, share, map, makeSubject, tap, publish } from 'wonka';
import { queryResponse } from '../../../packages/core/src/test-utils';
Expand Down Expand Up @@ -104,6 +104,10 @@ describe('storage', () => {
});

describe('offline', () => {
beforeAll(() => {
global.navigator = { onLine: true } as any;
});

it('should intercept errored mutations', () => {
const onlineSpy = vi.spyOn(navigator, 'onLine', 'get');

Expand Down
2 changes: 2 additions & 0 deletions exchanges/refocus/src/refocusExchange.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @vitest-environment jsdom

import { pipe, map, makeSubject, publish, tap } from 'wonka';
import { vi, expect, it, beforeEach } from 'vitest';

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"typescript": "^4.9.5",
"vite": "^3.2.4",
"vite-tsconfig-paths": "^4.0.7",
"vitest": "^0.29.3"
"vitest": "^0.30.1"
},
"dependencies": {
"@actions/github": "^5.1.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/internal/fetchOptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @vitest-environment jsdom

import { expect, describe, it } from 'vitest';
import { makeOperation } from '../utils/operation';
import { queryOperation, mutationOperation } from '../test-utils';
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/utils/variables.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @vitest-environment jsdom

import { stringifyVariables, extractFiles } from './variables';
import { describe, it, expect } from 'vitest';

Expand Down
17 changes: 11 additions & 6 deletions packages/preact-urql/src/components/Mutation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @vitest-environment jsdom

import { h } from 'preact';
import { act, cleanup, render } from '@testing-library/preact';
import { pipe, fromValue, delay } from 'wonka';
Expand All @@ -16,6 +18,8 @@ const query = 'mutation Example { example }';

describe('Mutation', () => {
beforeEach(() => {
vi.useFakeTimers();

vi.spyOn(global.console, 'error').mockImplementation(() => {
// do nothing
});
Expand All @@ -25,7 +29,7 @@ describe('Mutation', () => {
cleanup();
});

it('Should execute the mutation', async () => {
it('Should execute the mutation', () => {
// eslint-disable-next-line
let execute = () => {},
props = {};
Expand Down Expand Up @@ -55,20 +59,21 @@ describe('Mutation', () => {
fetching: false,
error: undefined,
});

act(() => {
execute();
});

expect(props).toStrictEqual({
data: undefined,
fetching: true,
error: undefined,
});

await new Promise(res => {
setTimeout(() => {
expect(props).toStrictEqual({ data: 1, fetching: false, error: 2 });
res(null);
}, 400);
act(() => {
vi.advanceTimersByTime(400);
});

expect(props).toStrictEqual({ data: 1, fetching: false, error: 2 });
});
});
2 changes: 2 additions & 0 deletions packages/preact-urql/src/components/Query.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @vitest-environment jsdom

import { h } from 'preact';
import { cleanup, render } from '@testing-library/preact';
import { map, interval, pipe } from 'wonka';
Expand Down
26 changes: 15 additions & 11 deletions packages/preact-urql/src/components/Subscription.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @vitest-environment jsdom

import { h } from 'preact';
import { cleanup, render } from '@testing-library/preact';
import { cleanup, render, act } from '@testing-library/preact';
import { map, interval, pipe } from 'wonka';
import { vi, expect, it, beforeEach, describe, afterEach } from 'vitest';

Expand All @@ -8,16 +10,17 @@ import { Subscription } from './Subscription';

const query = 'subscription Example { example }';
const client = {
executeSubscription: vi.fn(() =>
pipe(
executeSubscription: vi.fn(() => {
return pipe(
interval(200),
map((i: number) => ({ data: i, error: i + 1 }))
)
),
);
}),
};

describe('Subscription', () => {
beforeEach(() => {
vi.useFakeTimers();
vi.spyOn(global.console, 'error').mockImplementation(() => {
// do nothing
});
Expand All @@ -27,7 +30,7 @@ describe('Subscription', () => {
cleanup();
});

it('Should execute the subscription', async () => {
it('Should execute the subscription', () => {
let props = {};
const Test = () => h('p', {}, 'hi');
const App = () => {
Expand All @@ -44,18 +47,19 @@ describe('Subscription', () => {
],
});
};

render(h(App, {}));

expect(props).toStrictEqual({
data: undefined,
fetching: true,
error: undefined,
});

await new Promise(res => {
setTimeout(() => {
expect(props).toStrictEqual({ data: 0, fetching: true, error: 1 });
res(null);
}, 300);
act(() => {
vi.advanceTimersByTime(200);
});

expect(props).toStrictEqual({ data: 0, fetching: true, error: 1 });
});
});
2 changes: 2 additions & 0 deletions packages/preact-urql/src/hooks/useMutation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @vitest-environment jsdom

import { FunctionalComponent as FC, h } from 'preact';
import { render, cleanup, act } from '@testing-library/preact';
import { print } from 'graphql';
Expand Down
135 changes: 60 additions & 75 deletions packages/preact-urql/src/hooks/useQuery.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
// @vitest-environment jsdom

import { FunctionalComponent as FC, h } from 'preact';
import { render, cleanup, act } from '@testing-library/preact';
import { OperationContext } from '@urql/core';
import { map, interval, pipe, never, onStart, onEnd, empty } from 'wonka';
import {
vi,
expect,
it,
beforeEach,
describe,
beforeAll,
Mock,
afterEach,
} from 'vitest';

import { vi, expect, it, beforeEach, describe, afterEach, Mock } from 'vitest';

import { useQuery, UseQueryArgs, UseQueryState } from './useQuery';
import { Provider } from '../context';
Expand Down Expand Up @@ -46,7 +40,8 @@ const QueryUser: FC<UseQueryArgs<{ myVar: number }>> = ({
return h('p', {}, state.data);
};

beforeAll(() => {
beforeEach(() => {
vi.useFakeTimers();
vi.spyOn(global.console, 'error');
});

Expand Down Expand Up @@ -106,7 +101,7 @@ describe('useQuery', () => {
expect(state).toHaveProperty('fetching', true);
});

it('forwards data response', async () => {
it('forwards data response', () => {
const { rerender } = render(
h(Provider, {
value: client as any,
Expand All @@ -121,21 +116,20 @@ describe('useQuery', () => {
})
);

await new Promise(res => {
setTimeout(() => {
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props })],
})
);
expect(state).toHaveProperty('data', 0);
res(null);
}, 400);
act(() => {
vi.advanceTimersByTime(400);
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props })],
})
);
});

expect(state).toHaveProperty('data', 0);
});

it('forwards error response', async () => {
it('forwards error response', () => {
const { rerender } = render(
h(Provider, {
value: client as any,
Expand All @@ -150,54 +144,48 @@ describe('useQuery', () => {
})
);

await new Promise(res => {
setTimeout(() => {
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props })],
})
);
expect(state).toHaveProperty('error', 1);
res(null);
}, 400);
act(() => {
vi.advanceTimersByTime(400);
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props })],
})
);
});

expect(state).toHaveProperty('error', 1);
});

it('forwards extensions response', async () => {
it('forwards extensions response', () => {
const { rerender } = render(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props })],
})
);
/**
* Have to call update (without changes) in order to see the
* result of the state change.
*/

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 });
res(null);
}, 400);
act(() => {
vi.advanceTimersByTime(400);
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props })],
})
);
});

expect(state).toHaveProperty('extensions', { i: 1 });
});

it('sets fetching to false', async () => {
it('sets fetching to false', () => {
const { rerender } = render(
h(Provider, {
value: client as any,
Expand All @@ -212,18 +200,17 @@ describe('useQuery', () => {
})
);

await new Promise(res => {
setTimeout(() => {
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props })],
})
);
expect(state).toHaveProperty('fetching', false);
res(null);
}, 400);
act(() => {
vi.advanceTimersByTime(400);
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props })],
})
);
});

expect(state).toHaveProperty('fetching', false);
});

describe('on change', () => {
Expand All @@ -237,24 +224,22 @@ describe('useQuery', () => {
})
);

/**
* Have to call update twice for the change to be detected.
* Only a single change is detected (updating 5 times still only calls
* execute subscription twice).
*/
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props, query: q })],
})
);
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props, query: q })],
})
);

act(() => {
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props, query: q })],
})
);
});

expect(client.executeQuery).toBeCalledTimes(2);
});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/preact-urql/src/hooks/useSubscription.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @vitest-environment jsdom

import { FunctionalComponent as FC, h } from 'preact';
import { render, cleanup, act } from '@testing-library/preact';
import { OperationContext } from '@urql/core';
Expand Down
Loading

0 comments on commit 40cdb73

Please sign in to comment.