Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/ignore type errors from graphiql-toolkit's tests #3749

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { vi } from 'vitest';
import { Mock } from 'vitest';
import { parse, getIntrospectionQuery } from 'graphql';
import { createGraphiQLFetcher } from '../createFetcher';

Expand All @@ -11,25 +11,44 @@ vi.mock('graphql-ws');
vi.mock('subscriptions-transport-ws');

import {
createWebsocketsFetcherFromUrl,
createMultipartFetcher,
createSimpleFetcher,
createWebsocketsFetcherFromClient,
createLegacyWebsocketsFetcher,
createWebsocketsFetcherFromUrl as _createWebsocketsFetcherFromUrl,
createMultipartFetcher as _createMultipartFetcher,
createSimpleFetcher as _createSimpleFetcher,
createWebsocketsFetcherFromClient as _createWebsocketsFetcherFromClient,
createLegacyWebsocketsFetcher as _createLegacyWebsocketsFetcher,
} from '../lib';
import { createClient } from 'graphql-ws';
import { createClient as _createClient } from 'graphql-ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';

const serverURL = 'http://localhost:3000/graphql';
const wssURL = 'ws://localhost:3000/graphql';

const exampleIntrospectionDocument = parse(getIntrospectionQuery());

const createWebsocketsFetcherFromUrl = _createWebsocketsFetcherFromUrl as Mock<
typeof _createWebsocketsFetcherFromUrl
>;
const createMultipartFetcher = _createMultipartFetcher as Mock<
typeof _createMultipartFetcher
>;
const createSimpleFetcher = _createSimpleFetcher as Mock<
typeof _createSimpleFetcher
>;
const createClient = _createClient as Mock<typeof _createClient>;
const createWebsocketsFetcherFromClient =
_createWebsocketsFetcherFromClient as Mock<
typeof _createWebsocketsFetcherFromClient
>;
const createLegacyWebsocketsFetcher = _createLegacyWebsocketsFetcher as Mock<
typeof _createLegacyWebsocketsFetcher
>;

describe('createGraphiQLFetcher', () => {
afterEach(() => {
vi.resetAllMocks();
});
it('returns fetcher without websocket client by default', () => {
// @ts-expect-error
createWebsocketsFetcherFromUrl.mockReturnValue(true);
createGraphiQLFetcher({ url: serverURL });
expect(createWebsocketsFetcherFromUrl.mock.calls).toEqual([]);
Expand All @@ -39,6 +58,7 @@ describe('createGraphiQLFetcher', () => {
});

it('returns simple fetcher for introspection', async () => {
// @ts-expect-error
createSimpleFetcher.mockReturnValue(async () => 'hey!');
const fetcher = createGraphiQLFetcher({ url: serverURL });
expect(createWebsocketsFetcherFromUrl.mock.calls).toEqual([]);
Expand All @@ -55,6 +75,7 @@ describe('createGraphiQLFetcher', () => {
expect(res).toEqual('hey!');
});
it('returns fetcher without websocket client or multipart', () => {
// @ts-expect-error
createWebsocketsFetcherFromUrl.mockReturnValue(true);
createGraphiQLFetcher({ url: serverURL, enableIncrementalDelivery: false });
expect(createWebsocketsFetcherFromUrl.mock.calls).toEqual([]);
Expand All @@ -64,6 +85,7 @@ describe('createGraphiQLFetcher', () => {
]);
});
it('returns fetcher with websocket client', () => {
// @ts-expect-error
createWebsocketsFetcherFromUrl.mockReturnValue('Client1');

const args = {
Expand All @@ -78,7 +100,9 @@ describe('createGraphiQLFetcher', () => {
});

it('returns fetcher with custom wsClient', () => {
// @ts-expect-error
createClient.mockReturnValue('WSClient');
// @ts-expect-error
createWebsocketsFetcherFromUrl.mockReturnValue('CustomWSSFetcher');

const wsClient = createClient({ url: wssURL });
Expand All @@ -95,7 +119,9 @@ describe('createGraphiQLFetcher', () => {
});

it('returns fetcher with custom legacyClient', () => {
// @ts-expect-error
createClient.mockReturnValue('WSClient');
// @ts-expect-error
createLegacyWebsocketsFetcher.mockReturnValue('CustomWSSFetcher');

const legacyClient = new SubscriptionClient(wssURL);
Expand Down
19 changes: 12 additions & 7 deletions packages/graphiql-toolkit/src/create-fetcher/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { vi } from 'vitest';
import { Mock } from 'vitest';
import { parse } from 'graphql';
import {
isSubscriptionWithName,
Expand All @@ -12,9 +12,12 @@ vi.mock('graphql-ws');

vi.mock('subscriptions-transport-ws');

import { createClient } from 'graphql-ws';
import { createClient as _createClient } from 'graphql-ws';

import { SubscriptionClient } from 'subscriptions-transport-ws';
import { SubscriptionClient as _SubscriptionClient } from 'subscriptions-transport-ws';

const createClient = _createClient as Mock<typeof _createClient>;
const SubscriptionClient = _SubscriptionClient as Mock;

const exampleWithSubscription = parse(/* GraphQL */ `
subscription Example {
Expand Down Expand Up @@ -49,16 +52,16 @@ describe('createWebsocketsFetcherFromUrl', () => {
});

it('creates a websockets client using provided url', async () => {
// @ts-expect-error
createClient.mockReturnValue(true);
await createWebsocketsFetcherFromUrl('wss://example.com');
// @ts-ignore
expect(createClient.mock.calls[0][0]).toEqual({ url: 'wss://example.com' });
});

it('creates a websockets client using provided url that fails', async () => {
// @ts-expect-error
createClient.mockReturnValue(false);
expect(await createWebsocketsFetcherFromUrl('wss://example.com')).toThrow();
// @ts-ignore
expect(createClient.mock.calls[0][0]).toEqual({ url: 'wss://example.com' });
});
});
Expand All @@ -68,20 +71,22 @@ describe('getWsFetcher', () => {
vi.resetAllMocks();
});
it('provides an observable wsClient when custom wsClient option is provided', async () => {
// @ts-expect-error
createClient.mockReturnValue(true);
// @ts-expect-error
await getWsFetcher({ url: '', wsClient: true });
// @ts-ignore
expect(createClient.mock.calls).toHaveLength(0);
});
it('creates a subscriptions-transports-ws observable when custom legacyClient option is provided', async () => {
// @ts-expect-error
createClient.mockReturnValue(true);
await getWsFetcher({ url: '', legacyClient: true });
// @ts-ignore
expect(createClient.mock.calls).toHaveLength(0);
expect(SubscriptionClient.mock.calls).toHaveLength(0);
});

it('if subscriptionsUrl is provided, create a client on the fly', async () => {
// @ts-expect-error
createClient.mockReturnValue(true);
await getWsFetcher({ url: '', subscriptionUrl: 'wss://example' });
expect(createClient.mock.calls[0]).toEqual([
Expand Down
4 changes: 2 additions & 2 deletions packages/graphiql-toolkit/src/storage/__tests__/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('StorageAPI', () => {
});

it('returns any error while setting a value', () => {
// @ts-ignore
// @ts-expect-error
const throwingStorage = new StorageAPI({
setItem() {
throw new DOMException('Terrible Error');
Expand All @@ -84,7 +84,7 @@ describe('StorageAPI', () => {
});

it('returns isQuotaError to true if isQuotaError is thrown', () => {
// @ts-ignore
// @ts-expect-error
const throwingStorage = new StorageAPI({
setItem() {
throw new DOMException('Terrible Error', 'QuotaExceededError');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { QueryStore } from '../query';

class StorageMock {
shouldThrow: () => boolean;
// @ts-expect-error
count: number;
map = {};
// @ts-expect-error
storage: Storage;

constructor(shouldThrow: () => boolean) {
this.shouldThrow = shouldThrow;
}
Expand All @@ -19,7 +22,7 @@ class StorageMock {
isQuotaError: true,
};
}

// @ts-expect-error
this.map[key] = value;

return {
Expand All @@ -29,6 +32,7 @@ class StorageMock {
}

get(key: string) {
// @ts-expect-error
return this.map[key] || null;
}
}
Expand All @@ -47,6 +51,7 @@ describe('QueryStore', () => {

it('will fail silently on quota error', () => {
let i = 0;
// @ts-expect-error
const store = new QueryStore('normal', new StorageMock(() => i > 4));

for (; i < 10; i++) {
Expand Down Expand Up @@ -78,6 +83,7 @@ describe('QueryStore', () => {
let retryCounter = 0;
const store = new QueryStore(
'normal',
// @ts-expect-error
new StorageMock(() => {
retryCounter++;
return shouldThrow();
Expand Down Expand Up @@ -110,6 +116,7 @@ describe('QueryStore', () => {
let retryCounter = 0;
const store = new QueryStore(
'normal',
// @ts-expect-error
new StorageMock(() => {
retryCounter++;
return shouldThrow();
Expand Down
3 changes: 1 addition & 2 deletions packages/graphiql-toolkit/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
"moduleResolution": "node",
"types": ["vitest/globals"]
},
"include": ["src", "tsup.config.ts"],
"exclude": ["**/*.spec.ts"]
"include": ["src", "tsup.config.ts", "vitest.config.mts"]
}
Loading