From 5f4774fcc4ef05a7b32822ba1cad2d7979371e35 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 2 Oct 2023 14:26:17 +0200 Subject: [PATCH] fix(vitest): inline chai types instead of using @types/chai (#4209) --- package.json | 3 + packages/expect/index.d.ts | 3 + packages/expect/package.json | 8 ++- packages/expect/rollup.config.js | 6 ++ packages/expect/src/types.ts | 5 +- packages/vitest/package.json | 4 +- .../src/integrations/chai/chai-subset.d.ts | 4 ++ packages/vitest/src/types/global.ts | 12 ++++ patches/@types__chai@4.3.6.patch | 14 ++++ pnpm-lock.yaml | 68 +++++++++++++++---- 10 files changed, 103 insertions(+), 24 deletions(-) create mode 100644 packages/expect/index.d.ts create mode 100644 packages/vitest/src/integrations/chai/chai-subset.d.ts create mode 100644 patches/@types__chai@4.3.6.patch diff --git a/package.json b/package.json index fc90a709b7f0..8397f4038ada 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,9 @@ "ignoreMissing": [ "@algolia/client-search" ] + }, + "patchedDependencies": { + "@types/chai@4.3.6": "patches/@types__chai@4.3.6.patch" } }, "simple-git-hooks": { diff --git a/packages/expect/index.d.ts b/packages/expect/index.d.ts new file mode 100644 index 000000000000..1f129c72f135 --- /dev/null +++ b/packages/expect/index.d.ts @@ -0,0 +1,3 @@ +import './dist/chai' + +export * from './dist/index' diff --git a/packages/expect/package.json b/packages/expect/package.json index 9ba6b534488e..153b8b9ec86f 100644 --- a/packages/expect/package.json +++ b/packages/expect/package.json @@ -17,14 +17,14 @@ "sideEffects": false, "exports": { ".": { - "types": "./dist/index.d.ts", + "types": "./index.d.ts", "import": "./dist/index.js" }, "./*": "./*" }, "main": "./dist/index.js", "module": "./dist/index.js", - "types": "./dist/index.d.ts", + "types": "./index.d.ts", "files": [ "dist" ], @@ -39,7 +39,9 @@ "chai": "^4.3.10" }, "devDependencies": { + "@types/chai": "^4.3.6", "@vitest/runner": "workspace:*", - "picocolors": "^1.0.0" + "picocolors": "^1.0.0", + "rollup-plugin-copy": "^3.5.0" } } diff --git a/packages/expect/rollup.config.js b/packages/expect/rollup.config.js index 6ba38766cce2..bf8c7f56631b 100644 --- a/packages/expect/rollup.config.js +++ b/packages/expect/rollup.config.js @@ -2,6 +2,7 @@ import { builtinModules } from 'node:module' import esbuild from 'rollup-plugin-esbuild' import dts from 'rollup-plugin-dts' import { defineConfig } from 'rollup' +import copy from 'rollup-plugin-copy' import pkg from './package.json' assert { type: 'json' } const external = [ @@ -16,6 +17,11 @@ const plugins = [ esbuild({ target: 'node14', }), + copy({ + targets: [ + { src: 'node_modules/@types/chai/index.d.ts', dest: 'dist', rename: 'chai.d.ts' }, + ], + }), ] export default defineConfig([ diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index f530c8b80f8c..e3499fa9ec84 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -1,5 +1,3 @@ -import type { use as chaiUse } from 'chai' - /** * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. * @@ -12,8 +10,7 @@ import type { Formatter } from 'picocolors/types' import type { Constructable } from '@vitest/utils' import type { diff, getMatcherUtils, stringify } from './jest-matcher-utils' -export type FirstFunctionArgument = T extends (arg: infer A) => unknown ? A : never -export type ChaiPlugin = FirstFunctionArgument +export type ChaiPlugin = Chai.ChaiPlugin export type Tester = (a: any, b: any) => boolean | undefined diff --git a/packages/vitest/package.json b/packages/vitest/package.json index b3be327efd4c..573e83a76df0 100644 --- a/packages/vitest/package.json +++ b/packages/vitest/package.json @@ -106,8 +106,8 @@ "prepublishOnly": "pnpm build" }, "peerDependencies": { - "@types/node": ">= 14", "@edge-runtime/vm": "*", + "@types/node": ">= 14", "@vitest/browser": "*", "@vitest/ui": "*", "happy-dom": "*", @@ -146,8 +146,6 @@ } }, "dependencies": { - "@types/chai": "^4.3.5", - "@types/chai-subset": "^1.3.3", "@vitest/expect": "workspace:*", "@vitest/runner": "workspace:*", "@vitest/snapshot": "workspace:*", diff --git a/packages/vitest/src/integrations/chai/chai-subset.d.ts b/packages/vitest/src/integrations/chai/chai-subset.d.ts new file mode 100644 index 000000000000..a7d1b814e5a6 --- /dev/null +++ b/packages/vitest/src/integrations/chai/chai-subset.d.ts @@ -0,0 +1,4 @@ +declare module 'chai-subset' { + const chaiSubset: Chai.ChaiPlugin + export = chaiSubset +} diff --git a/packages/vitest/src/types/global.ts b/packages/vitest/src/types/global.ts index 664fbaef7004..9b943c43fcf6 100644 --- a/packages/vitest/src/types/global.ts +++ b/packages/vitest/src/types/global.ts @@ -5,6 +5,18 @@ import type { UserConsoleLog } from './general' import type { VitestEnvironment } from './config' import type { BenchmarkResult } from './benchmark' +declare global { + // eslint-disable-next-line @typescript-eslint/no-namespace + namespace Chai { + interface Assertion { + containSubset(expected: any): Assertion + } + interface Assert { + containSubset(val: any, exp: any, msg?: string): void + } + } +} + declare module '@vitest/expect' { interface MatcherState { environment: VitestEnvironment diff --git a/patches/@types__chai@4.3.6.patch b/patches/@types__chai@4.3.6.patch new file mode 100644 index 000000000000..6d8f1a0bcdc3 --- /dev/null +++ b/patches/@types__chai@4.3.6.patch @@ -0,0 +1,14 @@ +diff --git a/index.d.ts b/index.d.ts +index 9f5cb99cdc30c0ceee9c4bb2d866030cfe7b889e..7e231313ed85b703d70ec44778a96e7f3ff4f98e 100755 +--- a/index.d.ts ++++ b/index.d.ts +@@ -1963,6 +1963,6 @@ declare module "chai" { + export = chai; + } + +-interface Object { +- should: Chai.Assertion; +-} ++// interface Object { ++// should: Chai.Assertion; ++// } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d70c4329a6fc..6edac7859f37 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,11 @@ overrides: vite: ^4.4.9 vitest: workspace:* +patchedDependencies: + '@types/chai@4.3.6': + hash: s5kzatt2y2dzfxfynxzvzt5kbm + path: patches/@types__chai@4.3.6.patch + importers: .: @@ -1076,12 +1081,18 @@ importers: specifier: ^4.3.10 version: 4.3.10 devDependencies: + '@types/chai': + specifier: ^4.3.6 + version: 4.3.6(patch_hash=s5kzatt2y2dzfxfynxzvzt5kbm) '@vitest/runner': specifier: workspace:* version: link:../runner picocolors: specifier: ^1.0.0 version: 1.0.0 + rollup-plugin-copy: + specifier: ^3.5.0 + version: 3.5.0 packages/runner: dependencies: @@ -1278,12 +1289,6 @@ importers: packages/vitest: dependencies: - '@types/chai': - specifier: ^4.3.5 - version: 4.3.6 - '@types/chai-subset': - specifier: ^1.3.3 - version: 1.3.3 '@types/node': specifier: '>= 14' version: 18.16.19 @@ -10057,15 +10062,10 @@ packages: resolution: {integrity: sha512-+euflG6ygo4bn0JHtn4pYqcXwRtLvElQ7/nnjDu7iYG56H0+OhCd7d6Ug0IE3WcFpZozBKW2+80FUbv5QGk5AQ==} dev: true - /@types/chai-subset@1.3.3: - resolution: {integrity: sha512-frBecisrNGz+F4T6bcc+NLeolfiojh5FxW2klu669+8BARtyQv2C/GkNW6FUodVe4BroGMP/wER/YDGc7rEllw==} - dependencies: - '@types/chai': 4.3.6 - dev: false - - /@types/chai@4.3.6: + /@types/chai@4.3.6(patch_hash=s5kzatt2y2dzfxfynxzvzt5kbm): resolution: {integrity: sha512-VOVRLM1mBxIRxydiViqPcKn6MIxZytrbMpd6RJLIWKxUNr3zux8no0Oc7kJx0WAPIitgZ0gkrDS+btlqQpubpw==} - dev: false + dev: true + patched: true /@types/cheerio@0.22.31: resolution: {integrity: sha512-Kt7Cdjjdi2XWSfrZ53v4Of0wG3ZcmaegFXjMmz9tfNrZSkzzo36G0AL1YqSdcIA78Etjt6E609pt5h1xnQkPUw==} @@ -10144,6 +10144,12 @@ packages: '@types/node': 18.16.19 dev: true + /@types/fs-extra@8.1.3: + resolution: {integrity: sha512-7IdV01N0u/CaVO0fuY1YmEg14HQN3+EW8mpNgg6NEfxEl/lzCa5OxlBu3iFsCAdamnYOcTQ7oEi43Xc/67Rgzw==} + dependencies: + '@types/node': 18.16.19 + dev: true + /@types/fs-extra@9.0.13: resolution: {integrity: sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==} dependencies: @@ -13977,6 +13983,10 @@ packages: color-string: 1.9.1 dev: true + /colorette@1.4.0: + resolution: {integrity: sha512-Y2oEozpomLn7Q3HFP7dpww7AtMJplbM9lGZP6RDfHqmbeRjiwRg4n6VM6j4KLmRke85uWEI7JqF17f3pqdRA0g==} + dev: true + /colorette@2.0.20: resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==} dev: true @@ -17622,6 +17632,20 @@ packages: resolution: {integrity: sha512-40oNTM9UfG6aBmuKxk/giHn5nQ8RVz/SS4Ir6zgzOv9/qC3kKZ9v4etGTcJbEl/NyVQH7FGU7d+X1egr57Md2Q==} dev: true + /globby@10.0.1: + resolution: {integrity: sha512-sSs4inE1FB2YQiymcmTv6NWENryABjUNPeWhOvmn4SjtKybglsyPZxFB3U1/+L1bYi0rNZDqCLlHyLYDl1Pq5A==} + engines: {node: '>=8'} + dependencies: + '@types/glob': 7.2.0 + array-union: 2.1.0 + dir-glob: 3.0.1 + fast-glob: 3.3.0 + glob: 7.2.3 + ignore: 5.2.0 + merge2: 1.4.1 + slash: 3.0.0 + dev: true + /globby@11.1.0: resolution: {integrity: sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==} engines: {node: '>=10'} @@ -18765,6 +18789,11 @@ packages: isobject: 3.0.1 dev: true + /is-plain-object@3.0.1: + resolution: {integrity: sha512-Xnpx182SBMrr/aBik8y+GuR4U1L9FqMSojwDQwPMmxyC6bvEqly9UBCxhauBF5vNh2gwWJNX6oDV7O+OM4z34g==} + engines: {node: '>=0.10.0'} + dev: true + /is-plain-object@5.0.0: resolution: {integrity: sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==} engines: {node: '>=0.10.0'} @@ -23947,6 +23976,17 @@ packages: inherits: 2.0.4 dev: true + /rollup-plugin-copy@3.5.0: + resolution: {integrity: sha512-wI8D5dvYovRMx/YYKtUNt3Yxaw4ORC9xo6Gt9t22kveWz1enG9QrhVlagzwrxSC455xD1dHMKhIJkbsQ7d48BA==} + engines: {node: '>=8.3'} + dependencies: + '@types/fs-extra': 8.1.3 + colorette: 1.4.0 + fs-extra: 8.1.0 + globby: 10.0.1 + is-plain-object: 3.0.1 + dev: true + /rollup-plugin-dts@5.3.0(rollup@3.26.0)(typescript@5.1.6): resolution: {integrity: sha512-8FXp0ZkyZj1iU5klkIJYLjIq/YZSwBoERu33QBDxm/1yw5UU4txrEtcmMkrq+ZiKu3Q4qvPCNqc3ovX6rjqzbQ==} engines: {node: '>=v14'}