Skip to content

Commit 1900d97

Browse files
committed
test: fixed conflicts between wait-for-expect and jest timers
1 parent 0ac2ef1 commit 1900d97

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

packages/batch/test/batch.spec.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import { useClient, useQuery } from '../../villus/src';
66
import waitForExpect from 'wait-for-expect';
77
import { PostQuery, PostsQuery, QueryErrorWith500, QueryWithNetworkError } from 'villus/test/mocks/queries';
88

9+
beforeEach(() => {
10+
jest.useFakeTimers();
11+
});
12+
13+
afterEach(() => {
14+
jest.useRealTimers();
15+
});
16+
917
describe('batch plugin', () => {
1018
test('batches queries with batcher', async () => {
11-
jest.useFakeTimers();
1219
mount({
1320
setup() {
1421
useClient({
@@ -32,16 +39,17 @@ describe('batch plugin', () => {
3239

3340
jest.advanceTimersByTime(100);
3441
await flushPromises();
42+
43+
// wait-for-expect uses timers under the hood, so we need to reset here
44+
jest.useRealTimers();
3545
await waitForExpect(() => {
3646
expect(fetch).toHaveBeenCalledTimes(1);
3747
expect(document.querySelector('#multi')?.children).toHaveLength(5);
3848
expect(document.querySelector('#single')?.textContent).toContain('Awesome');
3949
});
40-
jest.useRealTimers();
4150
});
4251

4352
test('results with non-200 code will be evaluated separately', async () => {
44-
jest.useFakeTimers();
4553
mount({
4654
setup() {
4755
useClient({
@@ -65,16 +73,17 @@ describe('batch plugin', () => {
6573

6674
jest.advanceTimersByTime(100);
6775
await flushPromises();
76+
77+
// wait-for-expect uses timers under the hood, so we need to reset here
78+
jest.useRealTimers();
6879
await waitForExpect(() => {
6980
expect(fetch).toHaveBeenCalledTimes(1);
7081
expect(document.querySelector('#multi')?.children).toHaveLength(5);
7182
expect(document.querySelector('#error')?.textContent).toContain('Not authenticated');
7283
});
73-
jest.useRealTimers();
7484
});
7585

7686
test('handles network errors', async () => {
77-
jest.useFakeTimers();
7887
mount({
7988
setup() {
8089
useClient({
@@ -94,9 +103,10 @@ describe('batch plugin', () => {
94103

95104
jest.advanceTimersByTime(100);
96105
await flushPromises();
106+
// wait-for-expect uses timers under the hood, so we need to reset here
107+
jest.useRealTimers();
97108
await waitForExpect(() => {
98109
expect(document.querySelector('#error')?.textContent).toContain('Failed to connect');
99110
});
100-
jest.useRealTimers();
101111
});
102112
});

packages/villus/test/helpers/observer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
let interval: any;
12
export function makeObservable(throws = false, simulateError = false) {
2-
let interval: any;
33
let counter = 0;
44
const observable = {
55
subscribe({ next, error }: { error: (err: Error) => any; next: (value: any) => any }) {
@@ -28,6 +28,10 @@ export function makeObservable(throws = false, simulateError = false) {
2828
return observable;
2929
}
3030

31+
afterAll(() => {
32+
clearTimeout(interval);
33+
});
34+
3135
export function tick(ticks = 1) {
3236
jest.advanceTimersByTime(ticks * 100 + 1);
3337
}

packages/villus/test/mocks/handlers.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function makePost(id: number, title = 'Awesome Post') {
44
return { id, title: `${id} ${title}` };
55
}
66

7-
export const handlers = [
7+
export const handlers: any[] = [
88
// Handles a "GetUserInfo" query
99
graphql.query('Posts', (req, res, ctx) => {
1010
return res(
@@ -77,11 +77,11 @@ export const handlers = [
7777
return res.networkError('Failed to connect');
7878
}),
7979
// Handles Batched requests
80-
rest.post('https://test.com/graphql', async (req, res) => {
80+
rest.post('https://test.com/graphql', async (req, res, ctx) => {
8181
if (!Array.isArray(req.body)) {
8282
throw new Error('Unknown operation');
8383
}
84-
const data = await Promise.all(
84+
const responses = await Promise.all(
8585
req.body.map(async op => {
8686
const partReq = { ...req, body: op };
8787
const handler = handlers.find(h => h.test(partReq));
@@ -93,15 +93,10 @@ export const handlers = [
9393
})
9494
);
9595

96-
return res(res => {
97-
res.headers.set('content-type', 'application/json');
98-
res.body = JSON.stringify(
99-
data.map(d => {
100-
return JSON.parse(d?.response?.body) || {};
101-
})
102-
);
103-
104-
return res;
96+
const batchedResponse = responses.map(d => {
97+
return JSON.parse(d?.response?.body) || {};
10598
});
99+
100+
return res(ctx.json(batchedResponse));
106101
}),
107102
];

0 commit comments

Comments
 (0)