Skip to content

Commit b40f77c

Browse files
authored
migrate graphiql-toolkit to vitest (#3748)
1 parent 21c4409 commit b40f77c

File tree

12 files changed

+81
-69
lines changed

12 files changed

+81
-69
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ module.exports = {
333333
'unicorn/prefer-node-protocol': 'error',
334334
'import-x/no-unresolved': [
335335
'error',
336-
{ ignore: ['^node:', '\\.svg\\?react$'] },
336+
{ ignore: ['^node:', '\\.svg\\?react$', 'vitest/config'] },
337337
],
338338
'no-extra-boolean-cast': [
339339
'error',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
"ts-jest": "^27.1.5",
138138
"typedoc": "^0.19.2",
139139
"typescript": "^4.6.3",
140-
"vitest": "^2.0.4",
140+
"vitest": "^2.0.5",
141141
"wgutils": "^0.1.7",
142142
"wsrun": "^5.2.4"
143143
},

packages/graphiql-toolkit/jest.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/graphiql-toolkit/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"build": "tsup",
2323
"dev": "tsup --watch",
2424
"prebuild": "yarn types:check",
25-
"types:check": "tsc --noEmit"
25+
"types:check": "tsc --noEmit",
26+
"test": "vitest run"
2627
},
2728
"dependencies": {
2829
"@n1ru4l/push-pull-async-iterable-iterator": "^3.1.0",

packages/graphiql-toolkit/src/create-fetcher/__tests__/buildFetcher.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import { vi } from 'vitest';
12
import { parse, getIntrospectionQuery } from 'graphql';
23
import { createGraphiQLFetcher } from '../createFetcher';
34

45
import 'isomorphic-fetch';
56

6-
jest.mock('../lib');
7+
vi.mock('../lib');
78

8-
jest.mock('graphql-ws');
9+
vi.mock('graphql-ws');
910

10-
jest.mock('subscriptions-transport-ws');
11+
vi.mock('subscriptions-transport-ws');
1112

1213
import {
1314
createWebsocketsFetcherFromUrl,
@@ -26,7 +27,7 @@ const exampleIntrospectionDocument = parse(getIntrospectionQuery());
2627

2728
describe('createGraphiQLFetcher', () => {
2829
afterEach(() => {
29-
jest.resetAllMocks();
30+
vi.resetAllMocks();
3031
});
3132
it('returns fetcher without websocket client by default', () => {
3233
createWebsocketsFetcherFromUrl.mockReturnValue(true);

packages/graphiql-toolkit/src/create-fetcher/__tests__/lib.spec.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { vi } from 'vitest';
12
import { parse } from 'graphql';
23
import {
34
isSubscriptionWithName,
@@ -7,9 +8,9 @@ import {
78

89
import 'isomorphic-fetch';
910

10-
jest.mock('graphql-ws');
11+
vi.mock('graphql-ws');
1112

12-
jest.mock('subscriptions-transport-ws');
13+
vi.mock('subscriptions-transport-ws');
1314

1415
import { createClient } from 'graphql-ws';
1516

@@ -44,7 +45,7 @@ describe('isSubscriptionWithName', () => {
4445

4546
describe('createWebsocketsFetcherFromUrl', () => {
4647
afterEach(() => {
47-
jest.resetAllMocks();
48+
vi.resetAllMocks();
4849
});
4950

5051
it('creates a websockets client using provided url', async () => {
@@ -64,7 +65,7 @@ describe('createWebsocketsFetcherFromUrl', () => {
6465

6566
describe('getWsFetcher', () => {
6667
afterEach(() => {
67-
jest.resetAllMocks();
68+
vi.resetAllMocks();
6869
});
6970
it('provides an observable wsClient when custom wsClient option is provided', async () => {
7071
createClient.mockReturnValue(true);
@@ -92,10 +93,17 @@ describe('getWsFetcher', () => {
9293

9394
describe('missing `graphql-ws` dependency', () => {
9495
it('should throw a nice error', async () => {
95-
jest.resetModules();
96-
jest.doMock('graphql-ws', () => {
97-
// eslint-disable-next-line no-throw-literal
98-
throw { code: 'MODULE_NOT_FOUND' };
96+
vi.resetModules();
97+
vi.doMock('graphql-ws', () => {
98+
// While throwing an error directly inside this callback `code` is attached in `cause`
99+
// property e.g. `Error.cause.code`, so I throw an error on calling `createClient` instead
100+
101+
return {
102+
createClient: vi.fn().mockImplementation(() => {
103+
// eslint-disable-next-line no-throw-literal
104+
throw { code: 'MODULE_NOT_FOUND' };
105+
}),
106+
};
99107
});
100108

101109
await expect(

packages/graphiql-toolkit/src/storage/__tests__/base.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe('StorageAPI', () => {
7979
});
8080
const result = throwingStorage.set('key', 'value');
8181

82-
expect(result.error.message).toEqual('Terrible Error');
82+
expect(result.error!.message).toEqual('Error: Terrible Error');
8383
expect(result.isQuotaError).toBe(false);
8484
});
8585

@@ -93,7 +93,7 @@ describe('StorageAPI', () => {
9393
});
9494
const result = throwingStorage.set('key', 'value');
9595

96-
expect(result.error.message).toEqual('Terrible Error');
96+
expect(result.error!.message).toEqual('QuotaExceededError: Terrible Error');
9797
expect(result.isQuotaError).toBe(true);
9898
});
9999
});

packages/graphiql-toolkit/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"skipLibCheck": true,
1010
"allowJs": true,
1111
"lib": ["es2022", "dom"],
12-
"moduleResolution": "node"
12+
"moduleResolution": "node",
13+
"types": ["vitest/globals"]
1314
},
1415
"include": ["src", "tsup.config.ts"],
1516
"exclude": ["**/*.spec.ts"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'vitest/config';
2+
3+
export default defineConfig({
4+
test: {
5+
globals: true,
6+
environment: 'jsdom',
7+
},
8+
});

packages/monaco-graphql/vitest.config.mts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// eslint-disable-next-line import-x/no-unresolved -- todo: try to fix better rather ignoring here?
21
import { defineConfig } from 'vitest/config';
32

43
export default defineConfig({

0 commit comments

Comments
 (0)