From b40f77cfd658d10a778eac8855359cc3b0452d11 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Fri, 23 Aug 2024 23:06:15 +0200 Subject: [PATCH] migrate graphiql-toolkit to vitest (#3748) --- .eslintrc.js | 2 +- package.json | 2 +- packages/graphiql-toolkit/jest.config.js | 5 -- packages/graphiql-toolkit/package.json | 3 +- .../__tests__/buildFetcher.spec.ts | 9 +- .../src/create-fetcher/__tests__/lib.spec.ts | 24 +++-- .../src/storage/__tests__/base.spec.ts | 4 +- packages/graphiql-toolkit/tsconfig.json | 3 +- packages/graphiql-toolkit/vitest.config.mts | 8 ++ packages/monaco-graphql/vitest.config.mts | 1 - .../vscode-graphql-syntax/vitest.config.mts | 1 - yarn.lock | 88 +++++++++---------- 12 files changed, 81 insertions(+), 69 deletions(-) delete mode 100644 packages/graphiql-toolkit/jest.config.js create mode 100644 packages/graphiql-toolkit/vitest.config.mts diff --git a/.eslintrc.js b/.eslintrc.js index 0a9cdf1f0ec..8fb38b34309 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -333,7 +333,7 @@ module.exports = { 'unicorn/prefer-node-protocol': 'error', 'import-x/no-unresolved': [ 'error', - { ignore: ['^node:', '\\.svg\\?react$'] }, + { ignore: ['^node:', '\\.svg\\?react$', 'vitest/config'] }, ], 'no-extra-boolean-cast': [ 'error', diff --git a/package.json b/package.json index 41f402e9534..aa272db9a91 100644 --- a/package.json +++ b/package.json @@ -137,7 +137,7 @@ "ts-jest": "^27.1.5", "typedoc": "^0.19.2", "typescript": "^4.6.3", - "vitest": "^2.0.4", + "vitest": "^2.0.5", "wgutils": "^0.1.7", "wsrun": "^5.2.4" }, diff --git a/packages/graphiql-toolkit/jest.config.js b/packages/graphiql-toolkit/jest.config.js deleted file mode 100644 index d5f2ae82f35..00000000000 --- a/packages/graphiql-toolkit/jest.config.js +++ /dev/null @@ -1,5 +0,0 @@ -const base = require('../../jest.config.base')(__dirname); - -module.exports = { - ...base, -}; diff --git a/packages/graphiql-toolkit/package.json b/packages/graphiql-toolkit/package.json index 7d5546709f3..77c9f4ced47 100644 --- a/packages/graphiql-toolkit/package.json +++ b/packages/graphiql-toolkit/package.json @@ -22,7 +22,8 @@ "build": "tsup", "dev": "tsup --watch", "prebuild": "yarn types:check", - "types:check": "tsc --noEmit" + "types:check": "tsc --noEmit", + "test": "vitest run" }, "dependencies": { "@n1ru4l/push-pull-async-iterable-iterator": "^3.1.0", diff --git a/packages/graphiql-toolkit/src/create-fetcher/__tests__/buildFetcher.spec.ts b/packages/graphiql-toolkit/src/create-fetcher/__tests__/buildFetcher.spec.ts index de2e2a4934d..d6a857370e6 100644 --- a/packages/graphiql-toolkit/src/create-fetcher/__tests__/buildFetcher.spec.ts +++ b/packages/graphiql-toolkit/src/create-fetcher/__tests__/buildFetcher.spec.ts @@ -1,13 +1,14 @@ +import { vi } from 'vitest'; import { parse, getIntrospectionQuery } from 'graphql'; import { createGraphiQLFetcher } from '../createFetcher'; import 'isomorphic-fetch'; -jest.mock('../lib'); +vi.mock('../lib'); -jest.mock('graphql-ws'); +vi.mock('graphql-ws'); -jest.mock('subscriptions-transport-ws'); +vi.mock('subscriptions-transport-ws'); import { createWebsocketsFetcherFromUrl, @@ -26,7 +27,7 @@ const exampleIntrospectionDocument = parse(getIntrospectionQuery()); describe('createGraphiQLFetcher', () => { afterEach(() => { - jest.resetAllMocks(); + vi.resetAllMocks(); }); it('returns fetcher without websocket client by default', () => { createWebsocketsFetcherFromUrl.mockReturnValue(true); diff --git a/packages/graphiql-toolkit/src/create-fetcher/__tests__/lib.spec.ts b/packages/graphiql-toolkit/src/create-fetcher/__tests__/lib.spec.ts index 921ffcaebf1..9a77f98a492 100644 --- a/packages/graphiql-toolkit/src/create-fetcher/__tests__/lib.spec.ts +++ b/packages/graphiql-toolkit/src/create-fetcher/__tests__/lib.spec.ts @@ -1,3 +1,4 @@ +import { vi } from 'vitest'; import { parse } from 'graphql'; import { isSubscriptionWithName, @@ -7,9 +8,9 @@ import { import 'isomorphic-fetch'; -jest.mock('graphql-ws'); +vi.mock('graphql-ws'); -jest.mock('subscriptions-transport-ws'); +vi.mock('subscriptions-transport-ws'); import { createClient } from 'graphql-ws'; @@ -44,7 +45,7 @@ describe('isSubscriptionWithName', () => { describe('createWebsocketsFetcherFromUrl', () => { afterEach(() => { - jest.resetAllMocks(); + vi.resetAllMocks(); }); it('creates a websockets client using provided url', async () => { @@ -64,7 +65,7 @@ describe('createWebsocketsFetcherFromUrl', () => { describe('getWsFetcher', () => { afterEach(() => { - jest.resetAllMocks(); + vi.resetAllMocks(); }); it('provides an observable wsClient when custom wsClient option is provided', async () => { createClient.mockReturnValue(true); @@ -92,10 +93,17 @@ describe('getWsFetcher', () => { describe('missing `graphql-ws` dependency', () => { it('should throw a nice error', async () => { - jest.resetModules(); - jest.doMock('graphql-ws', () => { - // eslint-disable-next-line no-throw-literal - throw { code: 'MODULE_NOT_FOUND' }; + vi.resetModules(); + vi.doMock('graphql-ws', () => { + // While throwing an error directly inside this callback `code` is attached in `cause` + // property e.g. `Error.cause.code`, so I throw an error on calling `createClient` instead + + return { + createClient: vi.fn().mockImplementation(() => { + // eslint-disable-next-line no-throw-literal + throw { code: 'MODULE_NOT_FOUND' }; + }), + }; }); await expect( diff --git a/packages/graphiql-toolkit/src/storage/__tests__/base.spec.ts b/packages/graphiql-toolkit/src/storage/__tests__/base.spec.ts index f47bb386f66..7f0851a76b9 100644 --- a/packages/graphiql-toolkit/src/storage/__tests__/base.spec.ts +++ b/packages/graphiql-toolkit/src/storage/__tests__/base.spec.ts @@ -79,7 +79,7 @@ describe('StorageAPI', () => { }); const result = throwingStorage.set('key', 'value'); - expect(result.error.message).toEqual('Terrible Error'); + expect(result.error!.message).toEqual('Error: Terrible Error'); expect(result.isQuotaError).toBe(false); }); @@ -93,7 +93,7 @@ describe('StorageAPI', () => { }); const result = throwingStorage.set('key', 'value'); - expect(result.error.message).toEqual('Terrible Error'); + expect(result.error!.message).toEqual('QuotaExceededError: Terrible Error'); expect(result.isQuotaError).toBe(true); }); }); diff --git a/packages/graphiql-toolkit/tsconfig.json b/packages/graphiql-toolkit/tsconfig.json index 9209de32b24..4fcb973a796 100644 --- a/packages/graphiql-toolkit/tsconfig.json +++ b/packages/graphiql-toolkit/tsconfig.json @@ -9,7 +9,8 @@ "skipLibCheck": true, "allowJs": true, "lib": ["es2022", "dom"], - "moduleResolution": "node" + "moduleResolution": "node", + "types": ["vitest/globals"] }, "include": ["src", "tsup.config.ts"], "exclude": ["**/*.spec.ts"] diff --git a/packages/graphiql-toolkit/vitest.config.mts b/packages/graphiql-toolkit/vitest.config.mts new file mode 100644 index 00000000000..527089358ab --- /dev/null +++ b/packages/graphiql-toolkit/vitest.config.mts @@ -0,0 +1,8 @@ +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + globals: true, + environment: 'jsdom', + }, +}); diff --git a/packages/monaco-graphql/vitest.config.mts b/packages/monaco-graphql/vitest.config.mts index 50d382d83f8..7382f40e7d2 100644 --- a/packages/monaco-graphql/vitest.config.mts +++ b/packages/monaco-graphql/vitest.config.mts @@ -1,4 +1,3 @@ -// eslint-disable-next-line import-x/no-unresolved -- todo: try to fix better rather ignoring here? import { defineConfig } from 'vitest/config'; export default defineConfig({ diff --git a/packages/vscode-graphql-syntax/vitest.config.mts b/packages/vscode-graphql-syntax/vitest.config.mts index 3444fdd8e6e..9c294ab69b6 100644 --- a/packages/vscode-graphql-syntax/vitest.config.mts +++ b/packages/vscode-graphql-syntax/vitest.config.mts @@ -1,4 +1,3 @@ -// eslint-disable-next-line import-x/no-unresolved -- fix later import { defineConfig } from 'vitest/config'; export default defineConfig({ diff --git a/yarn.lock b/yarn.lock index cec5bf193f4..b47771846d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5122,53 +5122,53 @@ "@types/babel__core" "^7.20.5" react-refresh "^0.14.2" -"@vitest/expect@2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-2.0.4.tgz#d365c106c84f2a3aae96000e95be21956acc099c" - integrity sha512-39jr5EguIoanChvBqe34I8m1hJFI4+jxvdOpD7gslZrVQBKhh8H9eD7J/LJX4zakrw23W+dITQTDqdt43xVcJw== +"@vitest/expect@2.0.5": + version "2.0.5" + resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-2.0.5.tgz#f3745a6a2c18acbea4d39f5935e913f40d26fa86" + integrity sha512-yHZtwuP7JZivj65Gxoi8upUN2OzHTi3zVfjwdpu2WrvCZPLwsJ2Ey5ILIPccoW23dd/zQBlJ4/dhi7DWNyXCpA== dependencies: - "@vitest/spy" "2.0.4" - "@vitest/utils" "2.0.4" + "@vitest/spy" "2.0.5" + "@vitest/utils" "2.0.5" chai "^5.1.1" tinyrainbow "^1.2.0" -"@vitest/pretty-format@2.0.4", "@vitest/pretty-format@^2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@vitest/pretty-format/-/pretty-format-2.0.4.tgz#9a3934932e7f8ddd836b38c34ddaeec91bd0f82e" - integrity sha512-RYZl31STbNGqf4l2eQM1nvKPXE0NhC6Eq0suTTePc4mtMQ1Fn8qZmjV4emZdEdG2NOWGKSCrHZjmTqDCDoeFBw== +"@vitest/pretty-format@2.0.5", "@vitest/pretty-format@^2.0.5": + version "2.0.5" + resolved "https://registry.yarnpkg.com/@vitest/pretty-format/-/pretty-format-2.0.5.tgz#91d2e6d3a7235c742e1a6cc50e7786e2f2979b1e" + integrity sha512-h8k+1oWHfwTkyTkb9egzwNMfJAEx4veaPSnMeKbVSjp4euqGSbQlm5+6VHwTr7u4FJslVVsUG5nopCaAYdOmSQ== dependencies: tinyrainbow "^1.2.0" -"@vitest/runner@2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@vitest/runner/-/runner-2.0.4.tgz#0b1edb8ab5f81a1c7dfd50090e5e7e971a117891" - integrity sha512-Gk+9Su/2H2zNfNdeJR124gZckd5st4YoSuhF1Rebi37qTXKnqYyFCd9KP4vl2cQHbtuVKjfEKrNJxHHCW8thbQ== +"@vitest/runner@2.0.5": + version "2.0.5" + resolved "https://registry.yarnpkg.com/@vitest/runner/-/runner-2.0.5.tgz#89197e712bb93513537d6876995a4843392b2a84" + integrity sha512-TfRfZa6Bkk9ky4tW0z20WKXFEwwvWhRY+84CnSEtq4+3ZvDlJyY32oNTJtM7AW9ihW90tX/1Q78cb6FjoAs+ig== dependencies: - "@vitest/utils" "2.0.4" + "@vitest/utils" "2.0.5" pathe "^1.1.2" -"@vitest/snapshot@2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@vitest/snapshot/-/snapshot-2.0.4.tgz#7d7dea9df17c5c13386f1a7a433b99dc0ffe3c14" - integrity sha512-or6Mzoz/pD7xTvuJMFYEtso1vJo1S5u6zBTinfl+7smGUhqybn6VjzCDMhmTyVOFWwkCMuNjmNNxnyXPgKDoPw== +"@vitest/snapshot@2.0.5": + version "2.0.5" + resolved "https://registry.yarnpkg.com/@vitest/snapshot/-/snapshot-2.0.5.tgz#a2346bc5013b73c44670c277c430e0334690a162" + integrity sha512-SgCPUeDFLaM0mIUHfaArq8fD2WbaXG/zVXjRupthYfYGzc8ztbFbu6dUNOblBG7XLMR1kEhS/DNnfCZ2IhdDew== dependencies: - "@vitest/pretty-format" "2.0.4" + "@vitest/pretty-format" "2.0.5" magic-string "^0.30.10" pathe "^1.1.2" -"@vitest/spy@2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@vitest/spy/-/spy-2.0.4.tgz#19083386a741a158c2f142beffe43be68b1375cf" - integrity sha512-uTXU56TNoYrTohb+6CseP8IqNwlNdtPwEO0AWl+5j7NelS6x0xZZtP0bDWaLvOfUbaYwhhWp1guzXUxkC7mW7Q== +"@vitest/spy@2.0.5": + version "2.0.5" + resolved "https://registry.yarnpkg.com/@vitest/spy/-/spy-2.0.5.tgz#590fc07df84a78b8e9dd976ec2090920084a2b9f" + integrity sha512-c/jdthAhvJdpfVuaexSrnawxZz6pywlTPe84LUB2m/4t3rl2fTo9NFGBG4oWgaD+FTgDDV8hJ/nibT7IfH3JfA== dependencies: tinyspy "^3.0.0" -"@vitest/utils@2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@vitest/utils/-/utils-2.0.4.tgz#2db1df35aaeb5caa932770a190df636a68d284d5" - integrity sha512-Zc75QuuoJhOBnlo99ZVUkJIuq4Oj0zAkrQ2VzCqNCx6wAwViHEh5Fnp4fiJTE9rA+sAoXRf00Z9xGgfEzV6fzQ== +"@vitest/utils@2.0.5": + version "2.0.5" + resolved "https://registry.yarnpkg.com/@vitest/utils/-/utils-2.0.5.tgz#6f8307a4b6bc6ceb9270007f73c67c915944e926" + integrity sha512-d8HKbqIcya+GR67mkZbrzhS5kKhtp8dQLcmRZLGTscGVg7yImT82cIrhtn2L8+VujWcy6KZweApgNmPsTAO/UQ== dependencies: - "@vitest/pretty-format" "2.0.4" + "@vitest/pretty-format" "2.0.5" estree-walker "^3.0.3" loupe "^3.1.1" tinyrainbow "^1.2.0" @@ -18411,10 +18411,10 @@ vinyl@^1.1.1: clone-stats "^0.0.1" replace-ext "0.0.1" -vite-node@2.0.4: - version "2.0.4" - resolved "https://registry.yarnpkg.com/vite-node/-/vite-node-2.0.4.tgz#5600cc9f0d9c3ff9a64050c6858e7e1b62fb3fcd" - integrity sha512-ZpJVkxcakYtig5iakNeL7N3trufe3M6vGuzYAr4GsbCTwobDeyPJpE4cjDhhPluv8OvQCFzu2LWp6GkoKRITXA== +vite-node@2.0.5: + version "2.0.5" + resolved "https://registry.yarnpkg.com/vite-node/-/vite-node-2.0.5.tgz#36d909188fc6e3aba3da5fc095b3637d0d18e27b" + integrity sha512-LdsW4pxj0Ot69FAoXZ1yTnA9bjGohr2yNBU7QKRxpz8ITSkhuDl6h3zS/tvgz4qrNjeRnvrWeXQ8ZF7Um4W00Q== dependencies: cac "^6.7.14" debug "^4.3.5" @@ -18447,18 +18447,18 @@ vite@^5.0.0, vite@^5.3.5: optionalDependencies: fsevents "~2.3.3" -vitest@^2.0.4: - version "2.0.4" - resolved "https://registry.yarnpkg.com/vitest/-/vitest-2.0.4.tgz#ac6bfbaee53e502cee864b07a5b2edf1fcba793e" - integrity sha512-luNLDpfsnxw5QSW4bISPe6tkxVvv5wn2BBs/PuDRkhXZ319doZyLOBr1sjfB5yCEpTiU7xCAdViM8TNVGPwoog== +vitest@^2.0.5: + version "2.0.5" + resolved "https://registry.yarnpkg.com/vitest/-/vitest-2.0.5.tgz#2f15a532704a7181528e399cc5b754c7f335fd62" + integrity sha512-8GUxONfauuIdeSl5f9GTgVEpg5BTOlplET4WEDaeY2QBiN8wSm68vxN/tb5z405OwppfoCavnwXafiaYBC/xOA== dependencies: "@ampproject/remapping" "^2.3.0" - "@vitest/expect" "2.0.4" - "@vitest/pretty-format" "^2.0.4" - "@vitest/runner" "2.0.4" - "@vitest/snapshot" "2.0.4" - "@vitest/spy" "2.0.4" - "@vitest/utils" "2.0.4" + "@vitest/expect" "2.0.5" + "@vitest/pretty-format" "^2.0.5" + "@vitest/runner" "2.0.5" + "@vitest/snapshot" "2.0.5" + "@vitest/spy" "2.0.5" + "@vitest/utils" "2.0.5" chai "^5.1.1" debug "^4.3.5" execa "^8.0.1" @@ -18469,7 +18469,7 @@ vitest@^2.0.4: tinypool "^1.0.0" tinyrainbow "^1.2.0" vite "^5.0.0" - vite-node "2.0.4" + vite-node "2.0.5" why-is-node-running "^2.3.0" vsce@^2.6.3: