From ed0b797eacd41d5118f9ff2df75d04a541f8c729 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 17 May 2022 14:32:39 +0300 Subject: [PATCH 01/18] feat: jest global types --- e2e/tsconfig.json | 4 +- examples/angular/package.json | 1 - examples/typescript/package.json | 1 - examples/typescript/tsconfig.json | 3 +- package.json | 1 - .../babel-jest/src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../expect-utils/src/__tests__/tsconfig.json | 8 +++ packages/expect/src/__tests__/tsconfig.json | 8 +++ .../jest-circus/src/__tests__/tsconfig.json | 8 +++ packages/jest-circus/tsconfig.json | 4 +- packages/jest-cli/src/__tests__/tsconfig.json | 8 +++ .../jest-cli/src/init/__tests__/tsconfig.json | 8 +++ .../jest-config/src/__tests__/tsconfig.json | 8 +++ .../jest-console/src/__tests__/tsconfig.json | 8 +++ .../jest-core/src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../jest-diff/src/__tests__/tsconfig.json | 8 +++ .../jest-docblock/src/__tests__/tsconfig.json | 8 +++ .../jest-each/src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ packages/jest-environment/tsconfig.json | 2 - .../src/__tests__/tsconfig.json | 8 +++ .../jest-get-type/src/__tests__/tsconfig.json | 8 +++ .../jest-globals/src/__tests__/tsconfig.json | 8 +++ packages/jest-globals/tsconfig.json | 2 - .../src/__tests__/tsconfig.json | 8 +++ .../jest-jasmine2/src/__tests__/tsconfig.json | 8 +++ packages/jest-jasmine2/tsconfig.json | 4 +- .../src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../jest-mock/src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../jest-repl/src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../jest-resolve/src/__tests__/tsconfig.json | 8 +++ .../jest-runner/src/__tests__/tsconfig.json | 8 +++ .../jest-runtime/src/__tests__/tsconfig.json | 8 +++ .../jest-snapshot/src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../src/__tests__/tsconfig.json | 8 +++ .../jest-util/src/__tests__/tsconfig.json | 8 +++ .../jest-validate/src/__tests__/tsconfig.json | 8 +++ .../src/lib/__tests__/tsconfig.json | 8 +++ .../jest-worker/src/__tests__/tsconfig.json | 8 +++ packages/jest/__typetests__/jest.test.ts | 34 ++++++++++++ packages/jest/__typetests__/tsconfig.json | 12 ++++ packages/jest/package.json | 4 ++ packages/jest/src/index.ts | 23 ++++++++ .../pretty-format/src/__tests__/tsconfig.json | 8 +++ packages/test-utils/src/ConditionalTest.ts | 4 ++ scripts/bundleTs.mjs | 2 +- yarn.lock | 55 +------------------ 59 files changed, 432 insertions(+), 68 deletions(-) create mode 100644 packages/babel-jest/src/__tests__/tsconfig.json create mode 100644 packages/babel-plugin-jest-hoist/src/__tests__/tsconfig.json create mode 100644 packages/diff-sequences/src/__tests__/tsconfig.json create mode 100644 packages/expect-utils/src/__tests__/tsconfig.json create mode 100644 packages/expect/src/__tests__/tsconfig.json create mode 100644 packages/jest-circus/src/__tests__/tsconfig.json create mode 100644 packages/jest-cli/src/__tests__/tsconfig.json create mode 100644 packages/jest-cli/src/init/__tests__/tsconfig.json create mode 100644 packages/jest-config/src/__tests__/tsconfig.json create mode 100644 packages/jest-console/src/__tests__/tsconfig.json create mode 100644 packages/jest-core/src/__tests__/tsconfig.json create mode 100644 packages/jest-create-cache-key-function/src/__tests__/tsconfig.json create mode 100644 packages/jest-diff/src/__tests__/tsconfig.json create mode 100644 packages/jest-docblock/src/__tests__/tsconfig.json create mode 100644 packages/jest-each/src/__tests__/tsconfig.json create mode 100644 packages/jest-environment-jsdom/src/__tests__/tsconfig.json create mode 100644 packages/jest-environment-node/src/__tests__/tsconfig.json create mode 100644 packages/jest-fake-timers/src/__tests__/tsconfig.json create mode 100644 packages/jest-get-type/src/__tests__/tsconfig.json create mode 100644 packages/jest-globals/src/__tests__/tsconfig.json create mode 100644 packages/jest-haste-map/src/__tests__/tsconfig.json create mode 100644 packages/jest-jasmine2/src/__tests__/tsconfig.json create mode 100644 packages/jest-leak-detector/src/__tests__/tsconfig.json create mode 100644 packages/jest-matcher-utils/src/__tests__/tsconfig.json create mode 100644 packages/jest-message-util/src/__tests__/tsconfig.json create mode 100644 packages/jest-mock/src/__tests__/tsconfig.json create mode 100644 packages/jest-regex-util/src/__tests__/tsconfig.json create mode 100644 packages/jest-repl/src/__tests__/tsconfig.json create mode 100644 packages/jest-reporters/src/__tests__/tsconfig.json create mode 100644 packages/jest-resolve-dependencies/src/__tests__/tsconfig.json create mode 100644 packages/jest-resolve/src/__tests__/tsconfig.json create mode 100644 packages/jest-runner/src/__tests__/tsconfig.json create mode 100644 packages/jest-runtime/src/__tests__/tsconfig.json create mode 100644 packages/jest-snapshot/src/__tests__/tsconfig.json create mode 100644 packages/jest-source-map/src/__tests__/tsconfig.json create mode 100644 packages/jest-test-result/src/__tests__/tsconfig.json create mode 100644 packages/jest-test-sequencer/src/__tests__/tsconfig.json create mode 100644 packages/jest-transform/src/__tests__/tsconfig.json create mode 100644 packages/jest-util/src/__tests__/tsconfig.json create mode 100644 packages/jest-validate/src/__tests__/tsconfig.json create mode 100644 packages/jest-watcher/src/lib/__tests__/tsconfig.json create mode 100644 packages/jest-worker/src/__tests__/tsconfig.json create mode 100644 packages/jest/__typetests__/jest.test.ts create mode 100644 packages/jest/__typetests__/tsconfig.json create mode 100644 packages/pretty-format/src/__tests__/tsconfig.json diff --git a/e2e/tsconfig.json b/e2e/tsconfig.json index a25747da2a2d..ad2a8a75c8f7 100644 --- a/e2e/tsconfig.json +++ b/e2e/tsconfig.json @@ -17,6 +17,8 @@ "moduleResolution": "node", "isolatedModules": true, "importsNotUsedAsValues": "error", - "resolveJsonModule": true + "resolveJsonModule": true, + + "types": ["jest"] } } diff --git a/examples/angular/package.json b/examples/angular/package.json index 08a4887857c3..e0d59cd59e39 100644 --- a/examples/angular/package.json +++ b/examples/angular/package.json @@ -23,7 +23,6 @@ "@babel/plugin-proposal-decorators": "*", "@babel/preset-env": "^7.1.0", "@babel/preset-typescript": "^7.0.0", - "@types/jest": "^27.4.0", "babel-jest": "workspace:*", "babel-plugin-transform-typescript-metadata": "*", "jest": "workspace:*", diff --git a/examples/typescript/package.json b/examples/typescript/package.json index 7effcd539ec6..0044e106b5bd 100644 --- a/examples/typescript/package.json +++ b/examples/typescript/package.json @@ -12,7 +12,6 @@ "@babel/preset-env": "^7.1.0", "@babel/preset-react": "^7.12.1", "@babel/preset-typescript": "^7.0.0", - "@types/jest": "^27.4.0", "babel-jest": "workspace:*", "jest": "workspace:*" }, diff --git a/examples/typescript/tsconfig.json b/examples/typescript/tsconfig.json index 2d2d665710bc..56cfce46578a 100644 --- a/examples/typescript/tsconfig.json +++ b/examples/typescript/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "module": "commonjs", - "jsx": "react-jsx" + "jsx": "react-jsx", + "types": ["jest"] } } diff --git a/package.json b/package.json index 06ae2a4449b1..a2ed83def1f4 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,6 @@ "@types/babel__generator": "^7.0.0", "@types/babel__template": "^7.0.2", "@types/dedent": "^0.7.0", - "@types/jest": "^27.4.0", "@types/node": "~12.12.0", "@types/which": "^2.0.0", "@typescript-eslint/eslint-plugin": "^5.14.0", diff --git a/packages/babel-jest/src/__tests__/tsconfig.json b/packages/babel-jest/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/babel-jest/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/babel-plugin-jest-hoist/src/__tests__/tsconfig.json b/packages/babel-plugin-jest-hoist/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/babel-plugin-jest-hoist/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/diff-sequences/src/__tests__/tsconfig.json b/packages/diff-sequences/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/diff-sequences/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/expect-utils/src/__tests__/tsconfig.json b/packages/expect-utils/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/expect-utils/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/expect/src/__tests__/tsconfig.json b/packages/expect/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/expect/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-circus/src/__tests__/tsconfig.json b/packages/jest-circus/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-circus/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-circus/tsconfig.json b/packages/jest-circus/tsconfig.json index 67941a37790e..5722ca1d21ac 100644 --- a/packages/jest-circus/tsconfig.json +++ b/packages/jest-circus/tsconfig.json @@ -2,9 +2,7 @@ "extends": "../../tsconfig", "compilerOptions": { "outDir": "build", - "rootDir": "src", - // we don't want `@types/jest` to be referenced - "types": [] + "rootDir": "src" }, "include": ["./src/**/*"], "exclude": ["./**/__mocks__/**/*", "./**/__tests__/**/*"], diff --git a/packages/jest-cli/src/__tests__/tsconfig.json b/packages/jest-cli/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-cli/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-cli/src/init/__tests__/tsconfig.json b/packages/jest-cli/src/init/__tests__/tsconfig.json new file mode 100644 index 000000000000..24cf52090d68 --- /dev/null +++ b/packages/jest-cli/src/init/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-config/src/__tests__/tsconfig.json b/packages/jest-config/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-config/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-console/src/__tests__/tsconfig.json b/packages/jest-console/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-console/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-core/src/__tests__/tsconfig.json b/packages/jest-core/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-core/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-create-cache-key-function/src/__tests__/tsconfig.json b/packages/jest-create-cache-key-function/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-create-cache-key-function/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-diff/src/__tests__/tsconfig.json b/packages/jest-diff/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-diff/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-docblock/src/__tests__/tsconfig.json b/packages/jest-docblock/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-docblock/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-each/src/__tests__/tsconfig.json b/packages/jest-each/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-each/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-environment-jsdom/src/__tests__/tsconfig.json b/packages/jest-environment-jsdom/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-environment-jsdom/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-environment-node/src/__tests__/tsconfig.json b/packages/jest-environment-node/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-environment-node/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-environment/tsconfig.json b/packages/jest-environment/tsconfig.json index 3b0f77d1c9c5..4ac228a90ddf 100644 --- a/packages/jest-environment/tsconfig.json +++ b/packages/jest-environment/tsconfig.json @@ -1,8 +1,6 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - // we don't want `@types/jest` to be referenced - "types": ["node"], "rootDir": "src", "outDir": "build" }, diff --git a/packages/jest-fake-timers/src/__tests__/tsconfig.json b/packages/jest-fake-timers/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-fake-timers/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-get-type/src/__tests__/tsconfig.json b/packages/jest-get-type/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-get-type/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-globals/src/__tests__/tsconfig.json b/packages/jest-globals/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-globals/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-globals/tsconfig.json b/packages/jest-globals/tsconfig.json index 16a7adbc92db..da44298e3423 100644 --- a/packages/jest-globals/tsconfig.json +++ b/packages/jest-globals/tsconfig.json @@ -1,8 +1,6 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - // we don't want `@types/jest` to be referenced - "types": [], "rootDir": "src", "outDir": "build" }, diff --git a/packages/jest-haste-map/src/__tests__/tsconfig.json b/packages/jest-haste-map/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-haste-map/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-jasmine2/src/__tests__/tsconfig.json b/packages/jest-jasmine2/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-jasmine2/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-jasmine2/tsconfig.json b/packages/jest-jasmine2/tsconfig.json index 3d2268f04774..ecd8d01247bf 100644 --- a/packages/jest-jasmine2/tsconfig.json +++ b/packages/jest-jasmine2/tsconfig.json @@ -2,9 +2,7 @@ "extends": "../../tsconfig", "compilerOptions": { "rootDir": "src", - "outDir": "build", - // we don't want `@types/jest` to be referenced - "types": [] + "outDir": "build" }, "include": ["./src/**/*"], "exclude": ["./**/__tests__/**/*"], diff --git a/packages/jest-leak-detector/src/__tests__/tsconfig.json b/packages/jest-leak-detector/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-leak-detector/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-matcher-utils/src/__tests__/tsconfig.json b/packages/jest-matcher-utils/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-matcher-utils/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-message-util/src/__tests__/tsconfig.json b/packages/jest-message-util/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-message-util/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-mock/src/__tests__/tsconfig.json b/packages/jest-mock/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-mock/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-regex-util/src/__tests__/tsconfig.json b/packages/jest-regex-util/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-regex-util/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-repl/src/__tests__/tsconfig.json b/packages/jest-repl/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-repl/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-reporters/src/__tests__/tsconfig.json b/packages/jest-reporters/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-reporters/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json b/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-resolve/src/__tests__/tsconfig.json b/packages/jest-resolve/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-resolve/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-runner/src/__tests__/tsconfig.json b/packages/jest-runner/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-runner/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-runtime/src/__tests__/tsconfig.json b/packages/jest-runtime/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-runtime/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-snapshot/src/__tests__/tsconfig.json b/packages/jest-snapshot/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-snapshot/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-source-map/src/__tests__/tsconfig.json b/packages/jest-source-map/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-source-map/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-test-result/src/__tests__/tsconfig.json b/packages/jest-test-result/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-test-result/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-test-sequencer/src/__tests__/tsconfig.json b/packages/jest-test-sequencer/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-test-sequencer/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-transform/src/__tests__/tsconfig.json b/packages/jest-transform/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-transform/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-util/src/__tests__/tsconfig.json b/packages/jest-util/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-util/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-validate/src/__tests__/tsconfig.json b/packages/jest-validate/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-validate/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-watcher/src/lib/__tests__/tsconfig.json b/packages/jest-watcher/src/lib/__tests__/tsconfig.json new file mode 100644 index 000000000000..1871983fe3e1 --- /dev/null +++ b/packages/jest-watcher/src/lib/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../../", + "types": ["jest"] + }, + "include": ["../../**/*"] +} diff --git a/packages/jest-worker/src/__tests__/tsconfig.json b/packages/jest-worker/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/jest-worker/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest/__typetests__/jest.test.ts b/packages/jest/__typetests__/jest.test.ts new file mode 100644 index 000000000000..91c2b3025c88 --- /dev/null +++ b/packages/jest/__typetests__/jest.test.ts @@ -0,0 +1,34 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {expectType} from 'tsd-lite'; +import type {Jest} from '@jest/environment'; +import type {JestExpect} from '@jest/expect'; +import type {Global} from '@jest/types'; + +// globals enable through "types": ["jest"] + +expectType(beforeEach); +expectType(beforeAll); + +expectType(afterEach); +expectType(afterAll); + +expectType(describe); +expectType(fdescribe); +expectType(xdescribe); + +expectType(test); +expectType(xtest); + +expectType(it); +expectType(fit); +expectType(xit); + +expectType(expect); + +expectType(jest); diff --git a/packages/jest/__typetests__/tsconfig.json b/packages/jest/__typetests__/tsconfig.json new file mode 100644 index 000000000000..bb887a2b4cf7 --- /dev/null +++ b/packages/jest/__typetests__/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../../tsconfig.json", + "compilerOptions": { + "composite": false, + "noUnusedLocals": false, + "noUnusedParameters": false, + "skipLibCheck": true, + + "types": ["jest"] + }, + "include": ["./**/*"] +} diff --git a/packages/jest/package.json b/packages/jest/package.json index 0d7b32ecd020..46889788e92f 100644 --- a/packages/jest/package.json +++ b/packages/jest/package.json @@ -17,6 +17,10 @@ "import-local": "^3.0.2", "jest-cli": "^28.1.0" }, + "devDependencies": { + "@tsd/typescript": "~4.6.2", + "tsd-lite": "^0.5.1" + }, "peerDependencies": { "node-notifier": "^8.0.1 || ^9.0.0 || ^10.0.0" }, diff --git a/packages/jest/src/index.ts b/packages/jest/src/index.ts index d101fd921a01..51642a999366 100644 --- a/packages/jest/src/index.ts +++ b/packages/jest/src/index.ts @@ -13,3 +13,26 @@ export { } from '@jest/core'; export {run} from 'jest-cli'; + +declare global { + const beforeEach: typeof import('@jest/globals')['beforeEach']; + const beforeAll: typeof import('@jest/globals')['beforeAll']; + + const afterEach: typeof import('@jest/globals')['afterEach']; + const afterAll: typeof import('@jest/globals')['afterAll']; + + const describe: typeof import('@jest/globals')['describe']; + const fdescribe: typeof import('@jest/globals')['fdescribe']; + const xdescribe: typeof import('@jest/globals')['xdescribe']; + + const test: typeof import('@jest/globals')['test']; + const xtest: typeof import('@jest/globals')['xtest']; + + const it: typeof import('@jest/globals')['it']; + const fit: typeof import('@jest/globals')['fit']; + const xit: typeof import('@jest/globals')['xit']; + + const expect: typeof import('@jest/globals')['expect']; + + const jest: typeof import('@jest/globals')['jest']; +} diff --git a/packages/pretty-format/src/__tests__/tsconfig.json b/packages/pretty-format/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..37960a77de06 --- /dev/null +++ b/packages/pretty-format/src/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/test-utils/src/ConditionalTest.ts b/packages/test-utils/src/ConditionalTest.ts index 53e1f965e969..fb99aa44fd93 100644 --- a/packages/test-utils/src/ConditionalTest.ts +++ b/packages/test-utils/src/ConditionalTest.ts @@ -8,6 +8,10 @@ /* eslint-disable jest/no-focused-tests */ import semver = require('semver'); +import type {Global} from '@jest/types'; + +declare const describe: Global.TestFrameworkGlobals['describe']; +declare const test: Global.TestFrameworkGlobals['test']; export function isJestJasmineRun(): boolean { return process.env.JEST_JASMINE === '1'; diff --git a/scripts/bundleTs.mjs b/scripts/bundleTs.mjs index 4961d41ad2c6..30f26220f286 100644 --- a/scripts/bundleTs.mjs +++ b/scripts/bundleTs.mjs @@ -38,7 +38,7 @@ const copyrightSnippet = ` const typesNodeReferenceDirective = '/// '; -const excludedPackages = new Set(['@jest/globals']); +const excludedPackages = new Set(['@jest/globals', 'jest']); (async () => { const packages = getPackages(); diff --git a/yarn.lock b/yarn.lock index 050470a7b40c..23b19226e3d8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2668,7 +2668,6 @@ __metadata: "@types/babel__generator": ^7.0.0 "@types/babel__template": ^7.0.2 "@types/dedent": ^0.7.0 - "@types/jest": ^27.4.0 "@types/node": ~12.12.0 "@types/which": ^2.0.0 "@typescript-eslint/eslint-plugin": ^5.14.0 @@ -5076,16 +5075,6 @@ __metadata: languageName: node linkType: hard -"@types/jest@npm:^27.4.0": - version: 27.4.1 - resolution: "@types/jest@npm:27.4.1" - dependencies: - jest-matcher-utils: ^27.0.0 - pretty-format: ^27.0.0 - checksum: 5184f3eef4832d01ee8f59bed15eec45ccc8e29c724a5e6ce37bf74396b37bdf04f557000f45ba4fc38ae6075cf9cfcce3d7a75abc981023c61ceb27230a93e4 - languageName: node - linkType: hard - "@types/jsdom@npm:^16.2.4": version: 16.2.14 resolution: "@types/jsdom@npm:16.2.14" @@ -6150,7 +6139,6 @@ __metadata: "@babel/plugin-proposal-decorators": "*" "@babel/preset-env": ^7.1.0 "@babel/preset-typescript": ^7.0.0 - "@types/jest": ^27.4.0 babel-jest: "workspace:*" babel-plugin-transform-typescript-metadata: "*" core-js: ^3.2.1 @@ -9057,13 +9045,6 @@ __metadata: languageName: unknown linkType: soft -"diff-sequences@npm:^27.5.1": - version: 27.5.1 - resolution: "diff-sequences@npm:27.5.1" - checksum: a00db5554c9da7da225db2d2638d85f8e41124eccbd56cbaefb3b276dcbb1c1c2ad851c32defe2055a54a4806f030656cbf6638105fd6ce97bb87b90b32a33ca - languageName: node - linkType: hard - "diff@npm:^4.0.1": version: 4.0.2 resolution: "diff@npm:4.0.2" @@ -10199,7 +10180,6 @@ __metadata: "@babel/preset-env": ^7.1.0 "@babel/preset-react": ^7.12.1 "@babel/preset-typescript": ^7.0.0 - "@types/jest": ^27.4.0 babel-jest: "workspace:*" jest: "workspace:*" react: 17.0.2 @@ -13124,18 +13104,6 @@ __metadata: languageName: unknown linkType: soft -"jest-diff@npm:^27.5.1": - version: 27.5.1 - resolution: "jest-diff@npm:27.5.1" - dependencies: - chalk: ^4.0.0 - diff-sequences: ^27.5.1 - jest-get-type: ^27.5.1 - pretty-format: ^27.5.1 - checksum: 8be27c1e1ee57b2bb2bef9c0b233c19621b4c43d53a3c26e2c00a4e805eb4ea11fe1694a06a9fb0e80ffdcfdc0d2b1cb0b85920b3f5c892327ecd1e7bd96b865 - languageName: node - linkType: hard - "jest-docblock@^28.0.2, jest-docblock@workspace:packages/jest-docblock": version: 0.0.0-use.local resolution: "jest-docblock@workspace:packages/jest-docblock" @@ -13200,13 +13168,6 @@ __metadata: languageName: node linkType: hard -"jest-get-type@npm:^27.5.1": - version: 27.5.1 - resolution: "jest-get-type@npm:27.5.1" - checksum: 63064ab70195c21007d897c1157bf88ff94a790824a10f8c890392e7d17eda9c3900513cb291ca1c8d5722cad79169764e9a1279f7c8a9c4cd6e9109ff04bbc0 - languageName: node - linkType: hard - "jest-haste-map@^28.1.0, jest-haste-map@workspace:packages/jest-haste-map": version: 0.0.0-use.local resolution: "jest-haste-map@workspace:packages/jest-haste-map" @@ -13318,18 +13279,6 @@ __metadata: languageName: unknown linkType: soft -"jest-matcher-utils@npm:^27.0.0": - version: 27.5.1 - resolution: "jest-matcher-utils@npm:27.5.1" - dependencies: - chalk: ^4.0.0 - jest-diff: ^27.5.1 - jest-get-type: ^27.5.1 - pretty-format: ^27.5.1 - checksum: bb2135fc48889ff3fe73888f6cc7168ddab9de28b51b3148f820c89fdfd2effdcad005f18be67d0b9be80eda208ad47290f62f03d0a33f848db2dd0273c8217a - languageName: node - linkType: hard - "jest-message-util@^28.1.0, jest-message-util@workspace:packages/jest-message-util": version: 0.0.0-use.local resolution: "jest-message-util@workspace:packages/jest-message-util" @@ -13777,8 +13726,10 @@ __metadata: resolution: "jest@workspace:packages/jest" dependencies: "@jest/core": ^28.1.0 + "@tsd/typescript": ~4.6.2 import-local: ^3.0.2 jest-cli: ^28.1.0 + tsd-lite: ^0.5.1 peerDependencies: node-notifier: ^8.0.1 || ^9.0.0 || ^10.0.0 peerDependenciesMeta: @@ -17946,7 +17897,7 @@ __metadata: languageName: node linkType: hard -"pretty-format@npm:^27.0.0, pretty-format@npm:^27.0.2, pretty-format@npm:^27.5.1": +"pretty-format@npm:^27.0.2": version: 27.5.1 resolution: "pretty-format@npm:27.5.1" dependencies: From ed47a17adf53d7faa7f6263a8311b642ddf44379 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 17 May 2022 15:56:45 +0300 Subject: [PATCH 02/18] add docs --- docs/GettingStarted.md | 29 +++++++++++++++++++++++++---- docs/MockFunctionAPI.md | 6 +----- docs/UpgradingToJest28.md | 19 ++++++++++++++----- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/docs/GettingStarted.md b/docs/GettingStarted.md index 870942cac091..870e7ccb08f0 100644 --- a/docs/GettingStarted.md +++ b/docs/GettingStarted.md @@ -156,10 +156,31 @@ npm install --save-dev ts-jest #### Type definitions -You may also want to install the [`@types/jest`](https://www.npmjs.com/package/@types/jest) module for the version of Jest you're using. This will help provide full typing when writing your tests with TypeScript. +To enable type definitions of [Jest globals](GlobalAPI.md) add `"jest"` to the `"types"` list of your `tsconfig.json`: -> For `@types/*` modules it's recommended to try to match the version of the associated module. For example, if you are using `26.4.0` of `jest` then using `26.4.x` of `@types/jest` is ideal. In general, try to match the major (`26`) and minor (`4`) version as closely as possible. +```json title="tsconfig.json" +{ + "compilerOptions": { + "types": ["jest"] + } +} +``` -```bash npm2yarn -npm install --save-dev @types/jest +Alternatively you may use explicit imports from `@jest/globals` package: + +```ts title="sum.test.ts" +import {describe, expect, test} from '@jest/globals'; +import {sum} from './sum'; + +describe('sum module', () => { + test('adds 1 + 2 to equal 3', () => { + expect(sum(1, 2)).toBe(3); + }); +}); ``` + +:::info + +If you had `@types/jest` installed in your project before, remember to remove it. + +::: diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index 0ac4de121f65..b3dc66f90e9a 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -7,11 +7,7 @@ Mock functions are also known as "spies", because they let you spy on the behavi :::info -The TypeScript examples from this page will only work as document if you import `jest` from `'@jest/globals'`: - -```ts -import {jest} from '@jest/globals'; -``` +The TypeScript examples from this page assume you are using type definitions from [`jest`](GettingStarted.md/#type-definitions). ::: diff --git a/docs/UpgradingToJest28.md b/docs/UpgradingToJest28.md index 485cc51d9d31..942f11cf352f 100644 --- a/docs/UpgradingToJest28.md +++ b/docs/UpgradingToJest28.md @@ -189,16 +189,25 @@ Known examples of packages that fails in Jest 28 are [`uuid`](https://npmjs.com/ ## TypeScript -:::info +From Jest 28.2 you can replace `@types/jest` package with native type definitions. Simply add `"jest"` to the `"types"` list of your `tsconfig.json` and enjoy typed testing! + +```diff title="tsconfig.json" + { + "compilerOptions": { +- "types": ["@types/jest"] ++ "types": ["jest"] + } + } +``` -The TypeScript examples from this page will only work as document if you import `jest` from `'@jest/globals'`: +:::info -```ts -import {jest} from '@jest/globals'; -``` +If you had `@types/jest` installed in your project before, remember to remove it. ::: +The TypeScript examples bellow assume you are using type definitions from `jest`. + ### `jest.fn()` `jest.fn()` now takes only one generic type argument. See [Mock Functions API](MockFunctionAPI.md) page for more usage examples. From 075268f8f5f257a4ae328140797753991c4fd75f Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 17 May 2022 16:15:02 +0300 Subject: [PATCH 03/18] more docs --- docs/ExpectAPI.md | 124 +++++++++++++++++----- docs/MockFunctionAPI.md | 2 +- examples/expect-extend/toBeWithinRange.ts | 1 - examples/expect-extend/tsconfig.json | 3 +- 4 files changed, 102 insertions(+), 28 deletions(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index aa25136d861b..fb2730ae86c7 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -7,6 +7,12 @@ When you're writing tests, you often need to check that values meet certain cond For additional Jest matchers maintained by the Jest Community check out [`jest-extended`](https://github.com/jest-community/jest-extended). +:::info + +The TypeScript examples from this page assume you are using type definitions from [`jest`](GettingStarted.md/#type-definitions). + +::: + ## Methods import TOCInline from '@theme/TOCInline'; @@ -17,6 +23,9 @@ import TOCInline from '@theme/TOCInline'; ## Reference +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + ### `expect(value)` The `expect` function is used every time you want to test a value. You will rarely call `expect` by itself. Instead, you will use `expect` along with a "matcher" function to assert something about a value. @@ -37,29 +46,114 @@ The argument to `expect` should be the value that your code produces, and any ar You can use `expect.extend` to add your own matchers to Jest. For example, let's say that you're testing a number utility library and you're frequently asserting that numbers appear within particular ranges of other numbers. You could abstract that into a `toBeWithinRange` matcher: + + + ```js +const toBeWithinRange = function (actual, floor, ceiling) { + if ( + typeof actual !== 'number' || + typeof floor !== 'number' || + typeof ceiling !== 'number' + ) { + throw new Error('These must be of type number!'); + } + + const pass = actual >= floor && actual <= ceiling; + if (pass) { + return { + message: () => + `expected ${this.utils.printReceived( + actual, + )} not to be within range ${this.utils.printExpected( + `${floor} - ${ceiling}`, + )}`, + pass: true, + }; + } else { + return { + message: () => + `expected ${this.utils.printReceived( + actual, + )} to be within range ${this.utils.printExpected( + `${floor} - ${ceiling}`, + )}`, + pass: false, + }; + } +}; + expect.extend({ - toBeWithinRange(received, floor, ceiling) { - const pass = received >= floor && received <= ceiling; + toBeWithinRange, +}); +``` + + + + + +```ts +import type {MatcherFunction} from 'expect'; + +const toBeWithinRange: MatcherFunction<[floor: number, ceiling: number]> = + function (actual: unknown, floor: unknown, ceiling: unknown) { + if ( + typeof actual !== 'number' || + typeof floor !== 'number' || + typeof ceiling !== 'number' + ) { + throw new Error('These must be of type number!'); + } + + const pass = actual >= floor && actual <= ceiling; if (pass) { return { message: () => - `expected ${received} not to be within range ${floor} - ${ceiling}`, + `expected ${this.utils.printReceived( + actual, + )} not to be within range ${this.utils.printExpected( + `${floor} - ${ceiling}`, + )}`, pass: true, }; } else { return { message: () => - `expected ${received} to be within range ${floor} - ${ceiling}`, + `expected ${this.utils.printReceived( + actual, + )} to be within range ${this.utils.printExpected( + `${floor} - ${ceiling}`, + )}`, pass: false, }; } - }, + }; + +expect.extend({ + toBeWithinRange, }); +declare module 'expect' { + interface AsymmetricMatchers { + toBeWithinRange(floor: number, ceiling: number): void; + } + interface Matchers { + toBeWithinRange(floor: number, ceiling: number): R; + } +} +``` + + + + +And use it in a test: + +```js test('numeric ranges', () => { expect(100).toBeWithinRange(90, 110); + expect(101).not.toBeWithinRange(0, 100); + expect({apples: 6, bananas: 3}).toEqual({ apples: expect.toBeWithinRange(1, 10), bananas: expect.not.toBeWithinRange(11, 20), @@ -67,26 +161,6 @@ test('numeric ranges', () => { }); ``` -:::note - -In TypeScript, when using `@types/jest` for example, you can declare the new `toBeWithinRange` matcher in the imported module like this: - -```ts -interface CustomMatchers { - toBeWithinRange(floor: number, ceiling: number): R; -} - -declare global { - namespace jest { - interface Expect extends CustomMatchers {} - interface Matchers extends CustomMatchers {} - interface InverseAsymmetricMatchers extends CustomMatchers {} - } -} -``` - -::: - #### Async Matchers `expect.extend` also supports async matchers. Async matchers return a Promise so you will need to await the returned value. Let's use an example matcher to illustrate the usage of them. We are going to implement a matcher called `toBeDivisibleByExternalValue`, where the divisible number is going to be pulled from an external source. diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index b3dc66f90e9a..1345b4973685 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -174,7 +174,7 @@ mockFn(3); // 39 -```js +```ts const mockFn = jest.fn((scalar: number) => 42 + scalar); mockFn(0); // 42 diff --git a/examples/expect-extend/toBeWithinRange.ts b/examples/expect-extend/toBeWithinRange.ts index 43be8cfe431a..feb5e42c093e 100644 --- a/examples/expect-extend/toBeWithinRange.ts +++ b/examples/expect-extend/toBeWithinRange.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import {expect} from '@jest/globals'; import type {MatcherFunction} from 'expect'; const toBeWithinRange: MatcherFunction<[floor: number, ceiling: number]> = diff --git a/examples/expect-extend/tsconfig.json b/examples/expect-extend/tsconfig.json index 3b5a9204b389..a1e0c2ef1d68 100644 --- a/examples/expect-extend/tsconfig.json +++ b/examples/expect-extend/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { - "strict": true + "strict": true, + "types": ["jest"] }, "include": ["./**/*"] } From fc87c17f564b585e333e9a25b9ee2fdf7b5fdaca Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 17 May 2022 16:18:56 +0300 Subject: [PATCH 04/18] add change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b0e4a8b3327..6f2a997f8757 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[jest]` Allow enabling Jest global types through `"types": ["jest"]` in `tsconfig.json` ([#12856](https://github.com/facebook/jest/pull/12856)) - `[@jest/reporters]` Improve `GitHubActionsReporter`s annotation format ([#12826](https://github.com/facebook/jest/pull/12826)) ### Fixes From a37c2cd5f5a512e68539a7dc2eba0688499f4340 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 17 May 2022 16:25:51 +0300 Subject: [PATCH 05/18] clean up --- examples/expect-extend/__tests__/ranges.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/expect-extend/__tests__/ranges.test.ts b/examples/expect-extend/__tests__/ranges.test.ts index 77f80e8a5ef8..1faaac9ba8ec 100644 --- a/examples/expect-extend/__tests__/ranges.test.ts +++ b/examples/expect-extend/__tests__/ranges.test.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import {expect, test} from '@jest/globals'; import '../toBeWithinRange'; test('is within range', () => expect(100).toBeWithinRange(90, 110)); From c642ac668429c5897ee5a6f54201384d803570d6 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 17 May 2022 20:30:44 +0300 Subject: [PATCH 06/18] add missing references --- packages/jest/package.json | 1 + packages/jest/tsconfig.json | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/jest/package.json b/packages/jest/package.json index 46889788e92f..0f5c4498d7e7 100644 --- a/packages/jest/package.json +++ b/packages/jest/package.json @@ -14,6 +14,7 @@ }, "dependencies": { "@jest/core": "^28.1.0", + "@jest/globals": "^28.1.0", "import-local": "^3.0.2", "jest-cli": "^28.1.0" }, diff --git a/packages/jest/tsconfig.json b/packages/jest/tsconfig.json index 21169670d305..4ac64c22b5e1 100644 --- a/packages/jest/tsconfig.json +++ b/packages/jest/tsconfig.json @@ -5,5 +5,9 @@ "outDir": "build" }, "include": ["./src/**/*"], - "references": [{"path": "../jest-cli"}, {"path": "../jest-core"}] + "references": [ + {"path": "../jest-cli"}, + {"path": "../jest-core"}, + {"path": "../jest-globals"} + ] } From 00c245285abdb2dcce0f1d0c5a6033fefd38178b Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Wed, 18 May 2022 07:01:14 +0300 Subject: [PATCH 07/18] add export --- packages/jest/src/index.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/jest/src/index.ts b/packages/jest/src/index.ts index 51642a999366..280170d66e29 100644 --- a/packages/jest/src/index.ts +++ b/packages/jest/src/index.ts @@ -15,24 +15,24 @@ export { export {run} from 'jest-cli'; declare global { - const beforeEach: typeof import('@jest/globals')['beforeEach']; - const beforeAll: typeof import('@jest/globals')['beforeAll']; + export const beforeEach: typeof import('@jest/globals')['beforeEach']; + export const beforeAll: typeof import('@jest/globals')['beforeAll']; - const afterEach: typeof import('@jest/globals')['afterEach']; - const afterAll: typeof import('@jest/globals')['afterAll']; + export const afterEach: typeof import('@jest/globals')['afterEach']; + export const afterAll: typeof import('@jest/globals')['afterAll']; - const describe: typeof import('@jest/globals')['describe']; - const fdescribe: typeof import('@jest/globals')['fdescribe']; - const xdescribe: typeof import('@jest/globals')['xdescribe']; + export const describe: typeof import('@jest/globals')['describe']; + export const fdescribe: typeof import('@jest/globals')['fdescribe']; + export const xdescribe: typeof import('@jest/globals')['xdescribe']; - const test: typeof import('@jest/globals')['test']; - const xtest: typeof import('@jest/globals')['xtest']; + export const test: typeof import('@jest/globals')['test']; + export const xtest: typeof import('@jest/globals')['xtest']; - const it: typeof import('@jest/globals')['it']; - const fit: typeof import('@jest/globals')['fit']; - const xit: typeof import('@jest/globals')['xit']; + export const it: typeof import('@jest/globals')['it']; + export const fit: typeof import('@jest/globals')['fit']; + export const xit: typeof import('@jest/globals')['xit']; - const expect: typeof import('@jest/globals')['expect']; + export const expect: typeof import('@jest/globals')['expect']; - const jest: typeof import('@jest/globals')['jest']; + export const jest: typeof import('@jest/globals')['jest']; } From 973955c3e77e1dfe1b62c4faf34e00927515ab6b Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Wed, 18 May 2022 07:26:45 +0300 Subject: [PATCH 08/18] tweak tsconfigs --- packages/babel-jest/src/__tests__/tsconfig.json | 2 ++ .../src/__tests__/tsconfig.json | 2 ++ packages/diff-sequences/src/__tests__/tsconfig.json | 2 ++ packages/expect-utils/src/__tests__/tsconfig.json | 2 ++ packages/expect/src/__tests__/tsconfig.json | 2 ++ packages/jest-circus/src/__tests__/tsconfig.json | 2 ++ packages/jest-cli/src/__tests__/tsconfig.json | 2 ++ packages/jest-cli/src/init/__tests__/tsconfig.json | 2 ++ packages/jest-config/src/__tests__/tsconfig.json | 2 ++ packages/jest-console/src/__tests__/tsconfig.json | 2 ++ packages/jest-core/src/__tests__/tsconfig.json | 2 ++ packages/jest-core/src/lib/__tests__/tsconfig.json | 10 ++++++++++ packages/jest-core/src/plugins/__tests__/tsconfig.json | 10 ++++++++++ .../src/__tests__/tsconfig.json | 2 ++ packages/jest-diff/src/__tests__/tsconfig.json | 2 ++ packages/jest-docblock/src/__tests__/tsconfig.json | 2 ++ packages/jest-each/src/__tests__/tsconfig.json | 2 ++ .../jest-environment-jsdom/src/__tests__/tsconfig.json | 2 ++ .../jest-environment-node/src/__tests__/tsconfig.json | 2 ++ packages/jest-fake-timers/src/__tests__/tsconfig.json | 2 ++ packages/jest-get-type/src/__tests__/tsconfig.json | 2 ++ packages/jest-globals/src/__tests__/tsconfig.json | 2 ++ packages/jest-haste-map/src/__tests__/tsconfig.json | 2 ++ .../src/crawlers/__tests__/tsconfig.json | 10 ++++++++++ .../jest-haste-map/src/lib/__tests__/tsconfig.json | 10 ++++++++++ packages/jest-jasmine2/src/__tests__/tsconfig.json | 2 ++ .../jest-leak-detector/src/__tests__/tsconfig.json | 2 ++ .../jest-matcher-utils/src/__tests__/tsconfig.json | 2 ++ packages/jest-message-util/src/__tests__/tsconfig.json | 2 ++ packages/jest-mock/src/__tests__/tsconfig.json | 2 ++ packages/jest-regex-util/src/__tests__/tsconfig.json | 2 ++ packages/jest-repl/src/__tests__/tsconfig.json | 2 ++ packages/jest-reporters/src/__tests__/tsconfig.json | 2 ++ .../src/__tests__/tsconfig.json | 2 ++ packages/jest-resolve/src/__tests__/tsconfig.json | 2 ++ packages/jest-runner/src/__tests__/tsconfig.json | 2 ++ packages/jest-runtime/src/__tests__/tsconfig.json | 2 ++ packages/jest-snapshot/src/__tests__/tsconfig.json | 2 ++ packages/jest-source-map/src/__tests__/tsconfig.json | 2 ++ packages/jest-test-result/src/__tests__/tsconfig.json | 2 ++ .../jest-test-sequencer/src/__tests__/tsconfig.json | 2 ++ packages/jest-transform/src/__tests__/tsconfig.json | 2 ++ packages/jest-util/src/__tests__/tsconfig.json | 2 ++ packages/jest-validate/src/__tests__/tsconfig.json | 2 ++ packages/jest-watcher/src/lib/__tests__/tsconfig.json | 2 ++ packages/jest-worker/src/__tests__/tsconfig.json | 2 ++ packages/jest-worker/src/base/__tests__/tsconfig.json | 10 ++++++++++ .../jest-worker/src/workers/__tests__/tsconfig.json | 10 ++++++++++ packages/pretty-format/src/__tests__/setPrettyPrint.ts | 8 +++----- packages/pretty-format/src/__tests__/tsconfig.json | 2 ++ 50 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 packages/jest-core/src/lib/__tests__/tsconfig.json create mode 100644 packages/jest-core/src/plugins/__tests__/tsconfig.json create mode 100644 packages/jest-haste-map/src/crawlers/__tests__/tsconfig.json create mode 100644 packages/jest-haste-map/src/lib/__tests__/tsconfig.json create mode 100644 packages/jest-worker/src/base/__tests__/tsconfig.json create mode 100644 packages/jest-worker/src/workers/__tests__/tsconfig.json diff --git a/packages/babel-jest/src/__tests__/tsconfig.json b/packages/babel-jest/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/babel-jest/src/__tests__/tsconfig.json +++ b/packages/babel-jest/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/babel-plugin-jest-hoist/src/__tests__/tsconfig.json b/packages/babel-plugin-jest-hoist/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/babel-plugin-jest-hoist/src/__tests__/tsconfig.json +++ b/packages/babel-plugin-jest-hoist/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/diff-sequences/src/__tests__/tsconfig.json b/packages/diff-sequences/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/diff-sequences/src/__tests__/tsconfig.json +++ b/packages/diff-sequences/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/expect-utils/src/__tests__/tsconfig.json b/packages/expect-utils/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/expect-utils/src/__tests__/tsconfig.json +++ b/packages/expect-utils/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/expect/src/__tests__/tsconfig.json b/packages/expect/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/expect/src/__tests__/tsconfig.json +++ b/packages/expect/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-circus/src/__tests__/tsconfig.json b/packages/jest-circus/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-circus/src/__tests__/tsconfig.json +++ b/packages/jest-circus/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-cli/src/__tests__/tsconfig.json b/packages/jest-cli/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-cli/src/__tests__/tsconfig.json +++ b/packages/jest-cli/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-cli/src/init/__tests__/tsconfig.json b/packages/jest-cli/src/init/__tests__/tsconfig.json index 24cf52090d68..e2776c3e444f 100644 --- a/packages/jest-cli/src/init/__tests__/tsconfig.json +++ b/packages/jest-cli/src/init/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-config/src/__tests__/tsconfig.json b/packages/jest-config/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-config/src/__tests__/tsconfig.json +++ b/packages/jest-config/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-console/src/__tests__/tsconfig.json b/packages/jest-console/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-console/src/__tests__/tsconfig.json +++ b/packages/jest-console/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-core/src/__tests__/tsconfig.json b/packages/jest-core/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-core/src/__tests__/tsconfig.json +++ b/packages/jest-core/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-core/src/lib/__tests__/tsconfig.json b/packages/jest-core/src/lib/__tests__/tsconfig.json new file mode 100644 index 000000000000..aa4d0b76cee8 --- /dev/null +++ b/packages/jest-core/src/lib/__tests__/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../../../tsconfig.json", + "compilerOptions": { + "composite": false, + "esModuleInterop": true, + "rootDir": "../../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-core/src/plugins/__tests__/tsconfig.json b/packages/jest-core/src/plugins/__tests__/tsconfig.json new file mode 100644 index 000000000000..aa4d0b76cee8 --- /dev/null +++ b/packages/jest-core/src/plugins/__tests__/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../../../tsconfig.json", + "compilerOptions": { + "composite": false, + "esModuleInterop": true, + "rootDir": "../../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-create-cache-key-function/src/__tests__/tsconfig.json b/packages/jest-create-cache-key-function/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-create-cache-key-function/src/__tests__/tsconfig.json +++ b/packages/jest-create-cache-key-function/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-diff/src/__tests__/tsconfig.json b/packages/jest-diff/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-diff/src/__tests__/tsconfig.json +++ b/packages/jest-diff/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-docblock/src/__tests__/tsconfig.json b/packages/jest-docblock/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-docblock/src/__tests__/tsconfig.json +++ b/packages/jest-docblock/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-each/src/__tests__/tsconfig.json b/packages/jest-each/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-each/src/__tests__/tsconfig.json +++ b/packages/jest-each/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-environment-jsdom/src/__tests__/tsconfig.json b/packages/jest-environment-jsdom/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-environment-jsdom/src/__tests__/tsconfig.json +++ b/packages/jest-environment-jsdom/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-environment-node/src/__tests__/tsconfig.json b/packages/jest-environment-node/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-environment-node/src/__tests__/tsconfig.json +++ b/packages/jest-environment-node/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-fake-timers/src/__tests__/tsconfig.json b/packages/jest-fake-timers/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-fake-timers/src/__tests__/tsconfig.json +++ b/packages/jest-fake-timers/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-get-type/src/__tests__/tsconfig.json b/packages/jest-get-type/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-get-type/src/__tests__/tsconfig.json +++ b/packages/jest-get-type/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-globals/src/__tests__/tsconfig.json b/packages/jest-globals/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-globals/src/__tests__/tsconfig.json +++ b/packages/jest-globals/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-haste-map/src/__tests__/tsconfig.json b/packages/jest-haste-map/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-haste-map/src/__tests__/tsconfig.json +++ b/packages/jest-haste-map/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-haste-map/src/crawlers/__tests__/tsconfig.json b/packages/jest-haste-map/src/crawlers/__tests__/tsconfig.json new file mode 100644 index 000000000000..aa4d0b76cee8 --- /dev/null +++ b/packages/jest-haste-map/src/crawlers/__tests__/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../../../tsconfig.json", + "compilerOptions": { + "composite": false, + "esModuleInterop": true, + "rootDir": "../../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-haste-map/src/lib/__tests__/tsconfig.json b/packages/jest-haste-map/src/lib/__tests__/tsconfig.json new file mode 100644 index 000000000000..aa4d0b76cee8 --- /dev/null +++ b/packages/jest-haste-map/src/lib/__tests__/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../../../tsconfig.json", + "compilerOptions": { + "composite": false, + "esModuleInterop": true, + "rootDir": "../../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-jasmine2/src/__tests__/tsconfig.json b/packages/jest-jasmine2/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-jasmine2/src/__tests__/tsconfig.json +++ b/packages/jest-jasmine2/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-leak-detector/src/__tests__/tsconfig.json b/packages/jest-leak-detector/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-leak-detector/src/__tests__/tsconfig.json +++ b/packages/jest-leak-detector/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-matcher-utils/src/__tests__/tsconfig.json b/packages/jest-matcher-utils/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-matcher-utils/src/__tests__/tsconfig.json +++ b/packages/jest-matcher-utils/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-message-util/src/__tests__/tsconfig.json b/packages/jest-message-util/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-message-util/src/__tests__/tsconfig.json +++ b/packages/jest-message-util/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-mock/src/__tests__/tsconfig.json b/packages/jest-mock/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-mock/src/__tests__/tsconfig.json +++ b/packages/jest-mock/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-regex-util/src/__tests__/tsconfig.json b/packages/jest-regex-util/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-regex-util/src/__tests__/tsconfig.json +++ b/packages/jest-regex-util/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-repl/src/__tests__/tsconfig.json b/packages/jest-repl/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-repl/src/__tests__/tsconfig.json +++ b/packages/jest-repl/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-reporters/src/__tests__/tsconfig.json b/packages/jest-reporters/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-reporters/src/__tests__/tsconfig.json +++ b/packages/jest-reporters/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json b/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json +++ b/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-resolve/src/__tests__/tsconfig.json b/packages/jest-resolve/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-resolve/src/__tests__/tsconfig.json +++ b/packages/jest-resolve/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-runner/src/__tests__/tsconfig.json b/packages/jest-runner/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-runner/src/__tests__/tsconfig.json +++ b/packages/jest-runner/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-runtime/src/__tests__/tsconfig.json b/packages/jest-runtime/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-runtime/src/__tests__/tsconfig.json +++ b/packages/jest-runtime/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-snapshot/src/__tests__/tsconfig.json b/packages/jest-snapshot/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-snapshot/src/__tests__/tsconfig.json +++ b/packages/jest-snapshot/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-source-map/src/__tests__/tsconfig.json b/packages/jest-source-map/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-source-map/src/__tests__/tsconfig.json +++ b/packages/jest-source-map/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-test-result/src/__tests__/tsconfig.json b/packages/jest-test-result/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-test-result/src/__tests__/tsconfig.json +++ b/packages/jest-test-result/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-test-sequencer/src/__tests__/tsconfig.json b/packages/jest-test-sequencer/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-test-sequencer/src/__tests__/tsconfig.json +++ b/packages/jest-test-sequencer/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-transform/src/__tests__/tsconfig.json b/packages/jest-transform/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-transform/src/__tests__/tsconfig.json +++ b/packages/jest-transform/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-util/src/__tests__/tsconfig.json b/packages/jest-util/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-util/src/__tests__/tsconfig.json +++ b/packages/jest-util/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-validate/src/__tests__/tsconfig.json b/packages/jest-validate/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-validate/src/__tests__/tsconfig.json +++ b/packages/jest-validate/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-watcher/src/lib/__tests__/tsconfig.json b/packages/jest-watcher/src/lib/__tests__/tsconfig.json index 1871983fe3e1..9efa3a46dfe2 100644 --- a/packages/jest-watcher/src/lib/__tests__/tsconfig.json +++ b/packages/jest-watcher/src/lib/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../../", "types": ["jest"] }, diff --git a/packages/jest-worker/src/__tests__/tsconfig.json b/packages/jest-worker/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/jest-worker/src/__tests__/tsconfig.json +++ b/packages/jest-worker/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, diff --git a/packages/jest-worker/src/base/__tests__/tsconfig.json b/packages/jest-worker/src/base/__tests__/tsconfig.json new file mode 100644 index 000000000000..aa4d0b76cee8 --- /dev/null +++ b/packages/jest-worker/src/base/__tests__/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../../../tsconfig.json", + "compilerOptions": { + "composite": false, + "esModuleInterop": true, + "rootDir": "../../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/jest-worker/src/workers/__tests__/tsconfig.json b/packages/jest-worker/src/workers/__tests__/tsconfig.json new file mode 100644 index 000000000000..aa4d0b76cee8 --- /dev/null +++ b/packages/jest-worker/src/workers/__tests__/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../../../tsconfig.json", + "compilerOptions": { + "composite": false, + "esModuleInterop": true, + "rootDir": "../../", + "types": ["jest"] + }, + "include": ["../**/*"] +} diff --git a/packages/pretty-format/src/__tests__/setPrettyPrint.ts b/packages/pretty-format/src/__tests__/setPrettyPrint.ts index b6bc35e1ffc5..e82ce4f9b87e 100644 --- a/packages/pretty-format/src/__tests__/setPrettyPrint.ts +++ b/packages/pretty-format/src/__tests__/setPrettyPrint.ts @@ -8,11 +8,9 @@ import prettyFormat from '../'; import type {OptionsReceived, Plugins} from '../types'; -declare global { - namespace jest { - interface Matchers { - toPrettyPrintTo(expected: unknown, options?: OptionsReceived): R; - } +declare module 'expect' { + interface Matchers { + toPrettyPrintTo(expected: unknown, options?: OptionsReceived): R; } } diff --git a/packages/pretty-format/src/__tests__/tsconfig.json b/packages/pretty-format/src/__tests__/tsconfig.json index 37960a77de06..21a4fe83bc20 100644 --- a/packages/pretty-format/src/__tests__/tsconfig.json +++ b/packages/pretty-format/src/__tests__/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../../../../tsconfig.json", "compilerOptions": { + "composite": false, + "esModuleInterop": true, "rootDir": "../", "types": ["jest"] }, From a0fa6ed885c6894bfe2157b323069b399eaf1c63 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Wed, 18 May 2022 07:29:30 +0300 Subject: [PATCH 09/18] fix lock --- yarn.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/yarn.lock b/yarn.lock index 23b19226e3d8..56670a326ecb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13726,6 +13726,7 @@ __metadata: resolution: "jest@workspace:packages/jest" dependencies: "@jest/core": ^28.1.0 + "@jest/globals": ^28.1.0 "@tsd/typescript": ~4.6.2 import-local: ^3.0.2 jest-cli: ^28.1.0 From 048c18ccb1b60914f587c56a0c5eaf3b4171ebe9 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 3 Jun 2022 07:40:00 +0300 Subject: [PATCH 10/18] tabs --- docs/ExpectAPI.md | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index fb2730ae86c7..8674c26c3aab 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -23,9 +23,6 @@ import TOCInline from '@theme/TOCInline'; ## Reference -import Tabs from '@theme/Tabs'; -import TabItem from '@theme/TabItem'; - ### `expect(value)` The `expect` function is used every time you want to test a value. You will rarely call `expect` by itself. Instead, you will use `expect` along with a "matcher" function to assert something about a value. @@ -46,10 +43,7 @@ The argument to `expect` should be the value that your code produces, and any ar You can use `expect.extend` to add your own matchers to Jest. For example, let's say that you're testing a number utility library and you're frequently asserting that numbers appear within particular ranges of other numbers. You could abstract that into a `toBeWithinRange` matcher: - - - -```js +```js tab const toBeWithinRange = function (actual, floor, ceiling) { if ( typeof actual !== 'number' || @@ -88,11 +82,7 @@ expect.extend({ }); ``` - - - - -```ts +```ts tab import type {MatcherFunction} from 'expect'; const toBeWithinRange: MatcherFunction<[floor: number, ceiling: number]> = @@ -143,9 +133,6 @@ declare module 'expect' { } ``` - - - And use it in a test: ```js From ea84f93595098dc6f92bc3babaf34b578cb5915c Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 15:01:34 +0300 Subject: [PATCH 11/18] import from @jest/globals --- packages/test-utils/package.json | 1 + packages/test-utils/src/ConditionalTest.ts | 5 +---- packages/test-utils/tsconfig.json | 6 +++++- yarn.lock | 1 + 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index 9946d2c3da1b..45eced453a01 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -12,6 +12,7 @@ "./package.json": "./package.json" }, "dependencies": { + "@jest/globals": "workspace:^", "@jest/types": "workspace:^", "@types/node": "*", "ansi-regex": "^5.0.1", diff --git a/packages/test-utils/src/ConditionalTest.ts b/packages/test-utils/src/ConditionalTest.ts index fb99aa44fd93..879c95bf06fd 100644 --- a/packages/test-utils/src/ConditionalTest.ts +++ b/packages/test-utils/src/ConditionalTest.ts @@ -8,10 +8,7 @@ /* eslint-disable jest/no-focused-tests */ import semver = require('semver'); -import type {Global} from '@jest/types'; - -declare const describe: Global.TestFrameworkGlobals['describe']; -declare const test: Global.TestFrameworkGlobals['test']; +import {describe, test} from '@jest/globals'; export function isJestJasmineRun(): boolean { return process.env.JEST_JASMINE === '1'; diff --git a/packages/test-utils/tsconfig.json b/packages/test-utils/tsconfig.json index 110097eb7fef..bcc1e9b530ae 100644 --- a/packages/test-utils/tsconfig.json +++ b/packages/test-utils/tsconfig.json @@ -5,5 +5,9 @@ "outDir": "build" }, "include": ["./src/**/*"], - "references": [{"path": "../jest-types"}, {"path": "../pretty-format"}] + "references": [ + {"path": "../jest-globals"}, + {"path": "../jest-types"}, + {"path": "../pretty-format"} + ] } diff --git a/yarn.lock b/yarn.lock index 0c46bb756bd5..0b1208d06e1e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2927,6 +2927,7 @@ __metadata: version: 0.0.0-use.local resolution: "@jest/test-utils@workspace:packages/test-utils" dependencies: + "@jest/globals": "workspace:^" "@jest/types": "workspace:^" "@types/node": "*" "@types/semver": ^7.1.0 From 998e02a1397b2b678bea6edee24db36e9986f4ce Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 15:03:27 +0300 Subject: [PATCH 12/18] fix upgrading docs --- docs/UpgradingToJest29.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/UpgradingToJest29.md b/docs/UpgradingToJest29.md index 747cbab86adf..b0970cf951dd 100644 --- a/docs/UpgradingToJest29.md +++ b/docs/UpgradingToJest29.md @@ -63,7 +63,7 @@ From Jest 29 you can replace `@types/jest` package with native type definitions. :::info -The TypeScript examples bellow assume you are using type definitions from `jest`. +The TypeScript examples below assume you are using type definitions from `jest` and not `@types/jest`. ```ts import {jest} from '@jest/globals'; From dd87ffcd4113d69f35992254612bbc96b0b39252 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 15:08:55 +0300 Subject: [PATCH 13/18] tweak e2e tsconfig.json --- e2e/tsconfig.json | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/e2e/tsconfig.json b/e2e/tsconfig.json index ad2a8a75c8f7..b4c805fbba53 100644 --- a/e2e/tsconfig.json +++ b/e2e/tsconfig.json @@ -1,24 +1,8 @@ { + "extends": "../tsconfig.json", "compilerOptions": { - "noEmit": true, - - "target": "es2017", - "module": "commonjs", - "lib": ["dom", "es2017"], - "strict": true, - - /* Additional Checks */ - "noUnusedLocals": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true, - - /* Module Resolution Options */ - "moduleResolution": "node", - "isolatedModules": true, - "importsNotUsedAsValues": "error", - "resolveJsonModule": true, - + "composite": false, "types": ["jest"] - } + }, + "include": ["../**/*"] } From c734ec5902dec76979c85377a137d56cb3796570 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 15:26:57 +0300 Subject: [PATCH 14/18] revert imports --- packages/test-utils/package.json | 1 - packages/test-utils/src/ConditionalTest.ts | 7 ++++--- packages/test-utils/tsconfig.json | 6 +----- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index 45eced453a01..9946d2c3da1b 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -12,7 +12,6 @@ "./package.json": "./package.json" }, "dependencies": { - "@jest/globals": "workspace:^", "@jest/types": "workspace:^", "@types/node": "*", "ansi-regex": "^5.0.1", diff --git a/packages/test-utils/src/ConditionalTest.ts b/packages/test-utils/src/ConditionalTest.ts index 879c95bf06fd..badcb0c10ada 100644 --- a/packages/test-utils/src/ConditionalTest.ts +++ b/packages/test-utils/src/ConditionalTest.ts @@ -5,10 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -/* eslint-disable jest/no-focused-tests */ - import semver = require('semver'); -import {describe, test} from '@jest/globals'; +import type {Global} from '@jest/types'; + +declare const describe: Global.TestFrameworkGlobals['describe']; +declare const test: Global.TestFrameworkGlobals['test']; export function isJestJasmineRun(): boolean { return process.env.JEST_JASMINE === '1'; diff --git a/packages/test-utils/tsconfig.json b/packages/test-utils/tsconfig.json index bcc1e9b530ae..110097eb7fef 100644 --- a/packages/test-utils/tsconfig.json +++ b/packages/test-utils/tsconfig.json @@ -5,9 +5,5 @@ "outDir": "build" }, "include": ["./src/**/*"], - "references": [ - {"path": "../jest-globals"}, - {"path": "../jest-types"}, - {"path": "../pretty-format"} - ] + "references": [{"path": "../jest-types"}, {"path": "../pretty-format"}] } From 7a9c0542990d6cdeb04b223f846f5481ed9e003b Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 13 Aug 2022 15:30:38 +0300 Subject: [PATCH 15/18] fix lock --- yarn.lock | 1 - 1 file changed, 1 deletion(-) diff --git a/yarn.lock b/yarn.lock index 0b1208d06e1e..0c46bb756bd5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2927,7 +2927,6 @@ __metadata: version: 0.0.0-use.local resolution: "@jest/test-utils@workspace:packages/test-utils" dependencies: - "@jest/globals": "workspace:^" "@jest/types": "workspace:^" "@types/node": "*" "@types/semver": ^7.1.0 From 8475aef33f81eac41efc5d8d8fe5ac3cb86c1468 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sun, 14 Aug 2022 10:17:31 +0300 Subject: [PATCH 16/18] fix namespace --- packages/jest/src/index.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/jest/src/index.ts b/packages/jest/src/index.ts index 162afa2e4dad..3c9d18fdfe7a 100644 --- a/packages/jest/src/index.ts +++ b/packages/jest/src/index.ts @@ -6,6 +6,14 @@ */ import type {Config as ConfigTypes} from '@jest/types'; +import type { + ClassLike, + FunctionLike, + Mocked as JestMocked, + MockedClass as JestMockedClass, + MockedFunction as JestMockedFunction, + MockedObject as JestMockedObject, +} from 'jest-mock'; export { SearchSource, @@ -39,4 +47,24 @@ declare global { export const expect: typeof import('@jest/globals')['expect']; export const jest: typeof import('@jest/globals')['jest']; + + // eslint-disable-next-line @typescript-eslint/no-namespace + namespace jest { + /** + * Wraps a class, function or object type with Jest mock type definitions. + */ + export type Mocked = JestMocked; + /** + * Wraps a class type with Jest mock type definitions. + */ + export type MockedClass = JestMockedClass; + /** + * Wraps a function type with Jest mock type definitions. + */ + export type MockedFunction = JestMockedFunction; + /** + * Wraps an object type with Jest mock type definitions. + */ + export type MockedObject = JestMockedObject; + } } From a8d883dca439063c6c76e4562bd1d18ac73bb0ab Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sun, 14 Aug 2022 11:53:24 +0300 Subject: [PATCH 17/18] include deps --- packages/jest/package.json | 3 ++- packages/jest/tsconfig.json | 1 + yarn.lock | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/jest/package.json b/packages/jest/package.json index d0297e847d36..e4b03b31dd9e 100644 --- a/packages/jest/package.json +++ b/packages/jest/package.json @@ -17,7 +17,8 @@ "@jest/globals": "workspace:^", "@jest/types": "workspace:^", "import-local": "^3.0.2", - "jest-cli": "workspace:^" + "jest-cli": "workspace:^", + "jest-mock": "workspace:^" }, "devDependencies": { "@tsd/typescript": "~4.7.4", diff --git a/packages/jest/tsconfig.json b/packages/jest/tsconfig.json index c0cb56c68865..9c5eebd3c8cf 100644 --- a/packages/jest/tsconfig.json +++ b/packages/jest/tsconfig.json @@ -9,6 +9,7 @@ {"path": "../jest-cli"}, {"path": "../jest-core"}, {"path": "../jest-globals"}, + {"path": "../jest-mock"}, {"path": "../jest-types"} ] } diff --git a/yarn.lock b/yarn.lock index 0c46bb756bd5..f33cb61df42c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13052,6 +13052,7 @@ __metadata: "@tsd/typescript": ~4.7.4 import-local: ^3.0.2 jest-cli: "workspace:^" + jest-mock: "workspace:^" tsd-lite: ^0.5.6 peerDependencies: node-notifier: ^8.0.1 || ^9.0.0 || ^10.0.0 From a65cfee5af1aa17e70556e1ad27ee38dca1ce48c Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sun, 14 Aug 2022 11:57:56 +0300 Subject: [PATCH 18/18] test namespace --- packages/jest/__typetests__/jest.test.ts | 64 +++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/packages/jest/__typetests__/jest.test.ts b/packages/jest/__typetests__/jest.test.ts index 50ef1bdb37ab..9819c40bb545 100644 --- a/packages/jest/__typetests__/jest.test.ts +++ b/packages/jest/__typetests__/jest.test.ts @@ -10,6 +10,12 @@ import type {Jest} from '@jest/environment'; import type {JestExpect} from '@jest/expect'; import type {Config as ConfigTypes, Global} from '@jest/types'; import type {Config} from 'jest'; +import type { + Mocked, + MockedClass, + MockedFunction, + MockedObject, +} from 'jest-mock'; // Config @@ -17,7 +23,47 @@ declare const config: Config; expectType(config); -// globals enable through "types": ["jest"] +// globals enabled through "types": ["jest"] + +class SomeClass { + constructor(one: string, two?: boolean) {} + + methodA() { + return true; + } + methodB(a: string, b?: number) { + return; + } +} + +function someFunction(a: string, b?: number): boolean { + return true; +} + +const someObject = { + SomeClass, + + methodA() { + return; + }, + methodB(b: string) { + return true; + }, + methodC: (c: number) => true, + + one: { + more: { + time: (t: number) => { + return; + }, + }, + }, + + propertyA: 123, + propertyB: 'value', + + someClassInstance: new SomeClass('value'), +}; expectType(beforeEach); expectType(beforeAll); @@ -39,3 +85,19 @@ expectType(xit); expectType(expect); expectType(jest); + +expectType>( + someObject as jest.Mocked, +); + +expectType>( + SomeClass as jest.MockedClass, +); + +expectType>( + someFunction as jest.MockedFunction, +); + +expectType>( + someObject as jest.MockedObject, +);