Skip to content

Commit

Permalink
fix(vitest): inline chai types instead of using @types/chai (#4209)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Oct 2, 2023
1 parent 4da7caa commit 5f4774f
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 24 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
"ignoreMissing": [
"@algolia/client-search"
]
},
"patchedDependencies": {
"@types/[email protected]": "patches/@[email protected]"
}
},
"simple-git-hooks": {
Expand Down
3 changes: 3 additions & 0 deletions packages/expect/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './dist/chai'

export * from './dist/index'
8 changes: 5 additions & 3 deletions packages/expect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand All @@ -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"
}
}
6 changes: 6 additions & 0 deletions packages/expect/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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([
Expand Down
5 changes: 1 addition & 4 deletions packages/expect/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { use as chaiUse } from 'chai'

/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
Expand All @@ -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> = T extends (arg: infer A) => unknown ? A : never
export type ChaiPlugin = FirstFunctionArgument<typeof chaiUse>
export type ChaiPlugin = Chai.ChaiPlugin

export type Tester = (a: any, b: any) => boolean | undefined

Expand Down
4 changes: 1 addition & 3 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@
"prepublishOnly": "pnpm build"
},
"peerDependencies": {
"@types/node": ">= 14",
"@edge-runtime/vm": "*",
"@types/node": ">= 14",
"@vitest/browser": "*",
"@vitest/ui": "*",
"happy-dom": "*",
Expand Down Expand Up @@ -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:*",
Expand Down
4 changes: 4 additions & 0 deletions packages/vitest/src/integrations/chai/chai-subset.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'chai-subset' {
const chaiSubset: Chai.ChaiPlugin
export = chaiSubset
}
12 changes: 12 additions & 0 deletions packages/vitest/src/types/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions patches/@[email protected]
Original file line number Diff line number Diff line change
@@ -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;
+// }
68 changes: 54 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5f4774f

Please sign in to comment.