diff --git a/e2e/tsconfig.json b/e2e/tsconfig.json index c3053b6da00f..dba558ebe941 100644 --- a/e2e/tsconfig.json +++ b/e2e/tsconfig.json @@ -3,5 +3,6 @@ "compilerOptions": { "rootDir": "./" }, - "include": ["./**/*"] + "include": ["./**/*"], + "references": [{"path": "../packages/test-utils"}] } diff --git a/packages/babel-jest/src/__tests__/tsconfig.json b/packages/babel-jest/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/babel-jest/src/__tests__/tsconfig.json +++ b/packages/babel-jest/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/babel-jest/tsconfig.json b/packages/babel-jest/tsconfig.json index a58a0880e8aa..3535b54c1d86 100644 --- a/packages/babel-jest/tsconfig.json +++ b/packages/babel-jest/tsconfig.json @@ -7,5 +7,5 @@ "include": ["./src/**/*"], "exclude": ["./**/__tests__/**/*"], // TODO: include `babel-preset-jest` if it's ever in TS even though we don't care about its types - "references": [{"path": "../jest-transform"}, {"path": "../test-utils"}] + "references": [{"path": "../jest-transform"}] } diff --git a/packages/expect/src/__tests__/tsconfig.json b/packages/expect/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/expect/src/__tests__/tsconfig.json +++ b/packages/expect/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/expect/tsconfig.json b/packages/expect/tsconfig.json index 330ff26d00c9..8d7a33c00c62 100644 --- a/packages/expect/tsconfig.json +++ b/packages/expect/tsconfig.json @@ -12,7 +12,6 @@ {"path": "../jest-get-type"}, {"path": "../jest-matcher-utils"}, {"path": "../jest-message-util"}, - {"path": "../jest-util"}, - {"path": "../test-utils"} + {"path": "../jest-util"} ] } diff --git a/packages/jest-console/src/__tests__/tsconfig.json b/packages/jest-console/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-console/src/__tests__/tsconfig.json +++ b/packages/jest-console/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-console/tsconfig.json b/packages/jest-console/tsconfig.json index 308e84ff26ee..735c691024a6 100644 --- a/packages/jest-console/tsconfig.json +++ b/packages/jest-console/tsconfig.json @@ -9,7 +9,6 @@ "references": [ {"path": "../jest-message-util"}, {"path": "../jest-types"}, - {"path": "../jest-util"}, - {"path": "../test-utils"} + {"path": "../jest-util"} ] } diff --git a/packages/jest-core/src/__tests__/tsconfig.json b/packages/jest-core/src/__tests__/tsconfig.json index 8ccc3083ffb0..d603a17f8dfe 100644 --- a/packages/jest-core/src/__tests__/tsconfig.json +++ b/packages/jest-core/src/__tests__/tsconfig.json @@ -1,7 +1,5 @@ { "extends": "../../../../tsconfig.test.json", - "compilerOptions": { - "rootDir": "../" - }, - "include": ["../**/*"] + "include": ["./**/*"], + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-core/src/lib/__tests__/tsconfig.json b/packages/jest-core/src/lib/__tests__/tsconfig.json index facea41430cf..1bc42294775a 100644 --- a/packages/jest-core/src/lib/__tests__/tsconfig.json +++ b/packages/jest-core/src/lib/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../../"}] + "references": [{"path": "../../../"}, {"path": "../../../../test-utils"}] } diff --git a/packages/jest-core/tsconfig.json b/packages/jest-core/tsconfig.json index 0a01ce3297dd..ac309da620f3 100644 --- a/packages/jest-core/tsconfig.json +++ b/packages/jest-core/tsconfig.json @@ -26,7 +26,6 @@ {"path": "../jest-util"}, {"path": "../jest-validate"}, {"path": "../jest-watcher"}, - {"path": "../pretty-format"}, - {"path": "../test-utils"} + {"path": "../pretty-format"} ] } diff --git a/packages/jest-diff/src/__tests__/tsconfig.json b/packages/jest-diff/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-diff/src/__tests__/tsconfig.json +++ b/packages/jest-diff/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-diff/tsconfig.json b/packages/jest-diff/tsconfig.json index e816b873f5ba..b286bcd0da96 100644 --- a/packages/jest-diff/tsconfig.json +++ b/packages/jest-diff/tsconfig.json @@ -9,7 +9,6 @@ "references": [ {"path": "../diff-sequences"}, {"path": "../jest-get-type"}, - {"path": "../pretty-format"}, - {"path": "../test-utils"} + {"path": "../pretty-format"} ] } diff --git a/packages/jest-environment-jsdom/src/__tests__/tsconfig.json b/packages/jest-environment-jsdom/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-environment-jsdom/src/__tests__/tsconfig.json +++ b/packages/jest-environment-jsdom/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-environment-jsdom/tsconfig.json b/packages/jest-environment-jsdom/tsconfig.json index 240880c13e8f..82e5be22db29 100644 --- a/packages/jest-environment-jsdom/tsconfig.json +++ b/packages/jest-environment-jsdom/tsconfig.json @@ -11,7 +11,6 @@ {"path": "../jest-fake-timers"}, {"path": "../jest-mock"}, {"path": "../jest-types"}, - {"path": "../jest-util"}, - {"path": "../test-utils"} + {"path": "../jest-util"} ] } diff --git a/packages/jest-environment-node/src/__tests__/tsconfig.json b/packages/jest-environment-node/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-environment-node/src/__tests__/tsconfig.json +++ b/packages/jest-environment-node/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-environment-node/tsconfig.json b/packages/jest-environment-node/tsconfig.json index 12bd3f484bcd..13971fdd663d 100644 --- a/packages/jest-environment-node/tsconfig.json +++ b/packages/jest-environment-node/tsconfig.json @@ -11,7 +11,6 @@ {"path": "../jest-fake-timers"}, {"path": "../jest-mock"}, {"path": "../jest-types"}, - {"path": "../jest-util"}, - {"path": "../test-utils"} + {"path": "../jest-util"} ] } diff --git a/packages/jest-fake-timers/src/__tests__/tsconfig.json b/packages/jest-fake-timers/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-fake-timers/src/__tests__/tsconfig.json +++ b/packages/jest-fake-timers/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-fake-timers/tsconfig.json b/packages/jest-fake-timers/tsconfig.json index 3c20004c2488..483142698671 100644 --- a/packages/jest-fake-timers/tsconfig.json +++ b/packages/jest-fake-timers/tsconfig.json @@ -11,7 +11,6 @@ {"path": "../jest-message-util"}, {"path": "../jest-mock"}, {"path": "../jest-types"}, - {"path": "../jest-util"}, - {"path": "../test-utils"} + {"path": "../jest-util"} ] } diff --git a/packages/jest-haste-map/package.json b/packages/jest-haste-map/package.json index d06359213809..fa4f5b8a7747 100644 --- a/packages/jest-haste-map/package.json +++ b/packages/jest-haste-map/package.json @@ -30,7 +30,6 @@ "walker": "^1.0.8" }, "devDependencies": { - "@jest/test-utils": "workspace:^", "@types/fb-watchman": "^2.0.0", "@types/micromatch": "^4.0.1", "slash": "^3.0.0" diff --git a/packages/jest-haste-map/tsconfig.json b/packages/jest-haste-map/tsconfig.json index 5fcbf9a63cd9..22b31f6d8eb1 100644 --- a/packages/jest-haste-map/tsconfig.json +++ b/packages/jest-haste-map/tsconfig.json @@ -10,7 +10,6 @@ {"path": "../jest-regex-util"}, {"path": "../jest-types"}, {"path": "../jest-util"}, - {"path": "../jest-worker"}, - {"path": "../test-utils"} + {"path": "../jest-worker"} ] } diff --git a/packages/jest-matcher-utils/src/__tests__/tsconfig.json b/packages/jest-matcher-utils/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-matcher-utils/src/__tests__/tsconfig.json +++ b/packages/jest-matcher-utils/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-matcher-utils/tsconfig.json b/packages/jest-matcher-utils/tsconfig.json index 3905821cea60..e59af10a490f 100644 --- a/packages/jest-matcher-utils/tsconfig.json +++ b/packages/jest-matcher-utils/tsconfig.json @@ -10,7 +10,6 @@ "references": [ {"path": "../jest-diff"}, {"path": "../jest-get-type"}, - {"path": "../pretty-format"}, - {"path": "../test-utils"} + {"path": "../pretty-format"} ] } diff --git a/packages/jest-reporters/src/__tests__/tsconfig.json b/packages/jest-reporters/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-reporters/src/__tests__/tsconfig.json +++ b/packages/jest-reporters/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-reporters/tsconfig.json b/packages/jest-reporters/tsconfig.json index 813ba257479e..86a51036fda6 100644 --- a/packages/jest-reporters/tsconfig.json +++ b/packages/jest-reporters/tsconfig.json @@ -14,7 +14,6 @@ {"path": "../jest-transform"}, {"path": "../jest-types"}, {"path": "../jest-util"}, - {"path": "../jest-worker"}, - {"path": "../test-utils"} + {"path": "../jest-worker"} ] } diff --git a/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json b/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json +++ b/packages/jest-resolve-dependencies/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-resolve-dependencies/tsconfig.json b/packages/jest-resolve-dependencies/tsconfig.json index a1307345772a..503f05242ff2 100644 --- a/packages/jest-resolve-dependencies/tsconfig.json +++ b/packages/jest-resolve-dependencies/tsconfig.json @@ -12,7 +12,6 @@ {"path": "../jest-resolve"}, {"path": "../jest-runtime"}, {"path": "../jest-snapshot"}, - {"path": "../jest-types"}, - {"path": "../test-utils"} + {"path": "../jest-types"} ] } diff --git a/packages/jest-runner/package.json b/packages/jest-runner/package.json index c44fe957ced6..becf440e57f3 100644 --- a/packages/jest-runner/package.json +++ b/packages/jest-runner/package.json @@ -40,6 +40,7 @@ "source-map-support": "0.5.13" }, "devDependencies": { + "@jest/test-utils": "workspace:^", "@tsd/typescript": "^4.9.0", "@types/exit": "^0.1.30", "@types/graceful-fs": "^4.1.3", diff --git a/packages/jest-runner/src/__tests__/tsconfig.json b/packages/jest-runner/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-runner/src/__tests__/tsconfig.json +++ b/packages/jest-runner/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-runtime/src/__tests__/tsconfig.json b/packages/jest-runtime/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-runtime/src/__tests__/tsconfig.json +++ b/packages/jest-runtime/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-runtime/tsconfig.json b/packages/jest-runtime/tsconfig.json index 90a3eb84bddd..e62786852942 100644 --- a/packages/jest-runtime/tsconfig.json +++ b/packages/jest-runtime/tsconfig.json @@ -23,7 +23,6 @@ {"path": "../jest-test-result"}, {"path": "../jest-transform"}, {"path": "../jest-types"}, - {"path": "../jest-util"}, - {"path": "../test-utils"} + {"path": "../jest-util"} ] } diff --git a/packages/jest-snapshot/src/__tests__/tsconfig.json b/packages/jest-snapshot/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-snapshot/src/__tests__/tsconfig.json +++ b/packages/jest-snapshot/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-snapshot/tsconfig.json b/packages/jest-snapshot/tsconfig.json index 411a0f5a3a83..c9d79155bd00 100644 --- a/packages/jest-snapshot/tsconfig.json +++ b/packages/jest-snapshot/tsconfig.json @@ -17,7 +17,6 @@ {"path": "../jest-transform"}, {"path": "../jest-types"}, {"path": "../jest-util"}, - {"path": "../pretty-format"}, - {"path": "../test-utils"} + {"path": "../pretty-format"} ] } diff --git a/packages/jest-test-sequencer/src/__tests__/tsconfig.json b/packages/jest-test-sequencer/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-test-sequencer/src/__tests__/tsconfig.json +++ b/packages/jest-test-sequencer/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-test-sequencer/tsconfig.json b/packages/jest-test-sequencer/tsconfig.json index 7d3736031d3b..cdf9b3d35d19 100644 --- a/packages/jest-test-sequencer/tsconfig.json +++ b/packages/jest-test-sequencer/tsconfig.json @@ -6,9 +6,5 @@ }, "include": ["./src/**/*"], "exclude": ["./**/__tests__/**/*"], - "references": [ - {"path": "../jest-haste-map"}, - {"path": "../jest-test-result"}, - {"path": "../test-utils"} - ] + "references": [{"path": "../jest-haste-map"}, {"path": "../jest-test-result"}] } diff --git a/packages/jest-transform/src/__tests__/tsconfig.json b/packages/jest-transform/src/__tests__/tsconfig.json index dd1bca103251..d603a17f8dfe 100644 --- a/packages/jest-transform/src/__tests__/tsconfig.json +++ b/packages/jest-transform/src/__tests__/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "../../../../tsconfig.test.json", "include": ["./**/*"], - "references": [{"path": "../../"}] + "references": [{"path": "../../"}, {"path": "../../../test-utils"}] } diff --git a/packages/jest-transform/tsconfig.json b/packages/jest-transform/tsconfig.json index b663b58b48db..cd4148bc6954 100644 --- a/packages/jest-transform/tsconfig.json +++ b/packages/jest-transform/tsconfig.json @@ -10,7 +10,6 @@ {"path": "../jest-haste-map"}, {"path": "../jest-regex-util"}, {"path": "../jest-types"}, - {"path": "../jest-util"}, - {"path": "../test-utils"} + {"path": "../jest-util"} ] } diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index b064dd1ef7c8..4c090092b54e 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 badcb0c10ada..879c95bf06fd 100644 --- a/packages/test-utils/src/ConditionalTest.ts +++ b/packages/test-utils/src/ConditionalTest.ts @@ -5,11 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -import semver = require('semver'); -import type {Global} from '@jest/types'; +/* eslint-disable jest/no-focused-tests */ -declare const describe: Global.TestFrameworkGlobals['describe']; -declare const test: Global.TestFrameworkGlobals['test']; +import semver = require('semver'); +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/scripts/buildTs.mjs b/scripts/buildTs.mjs index 72dcdab405d9..29f50731bd6b 100644 --- a/scripts/buildTs.mjs +++ b/scripts/buildTs.mjs @@ -57,6 +57,12 @@ packagesWithTs.forEach(({packageDir, pkg}) => { } } + // only test files depend on '@jest/test-utils', i.e. it is always a dev dependency + // see additional checks below + if (dep === '@jest/test-utils') { + return false; + } + return true; }) .map(dep => @@ -84,6 +90,60 @@ packagesWithTs.forEach(({packageDir, pkg}) => { )}\nExpected:\n\n${jestDependenciesOfPackage.join('\n')}`, ); } + + let hasJestTestUtils = Object.keys(pkg.dependencies || {}).includes( + '@jest/test-utils', + ); + + if (hasJestTestUtils) { + throw new Error( + chalk.red( + `Package '${pkg.name}' declares '@jest/test-utils' as dependency, but it must be declared as dev dependency`, + ), + ); + } + + hasJestTestUtils = Object.keys(pkg.devDependencies || {}).includes( + '@jest/test-utils', + ); + + const tsConfigPaths = glob.sync('**/__tests__/tsconfig.json', { + absolute: true, + cwd: packageDir, + }); + + const testUtilsReferences = tsConfigPaths.filter(tsConfigPath => { + const tsConfig = JSON.parse( + stripJsonComments(fs.readFileSync(tsConfigPath, 'utf8')), + ); + const references = tsConfig.references.map(({path}) => path); + + return references.some(reference => /test-utils$/.test(reference)); + }); + + if (hasJestTestUtils && testUtilsReferences.length === 0) { + throw new Error( + chalk.red( + `Package '${ + pkg.name + }' declares '@jest/test-utils' as dev dependency, but it is not referenced in:\n\n${tsConfigPaths.join( + '\n', + )}`, + ), + ); + } + + if (!hasJestTestUtils && testUtilsReferences.length > 0) { + throw new Error( + chalk.red( + `Package '${ + pkg.name + }' does not declare '@jest/test-utils' as dev dependency, but it is referenced in:\n\n${testUtilsReferences.join( + '\n', + )}`, + ), + ); + } }); const args = [ diff --git a/yarn.lock b/yarn.lock index 6621ce616bd1..b25ad34476ff 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2970,6 +2970,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 @@ -12768,7 +12769,6 @@ __metadata: version: 0.0.0-use.local resolution: "jest-haste-map@workspace:packages/jest-haste-map" dependencies: - "@jest/test-utils": "workspace:^" "@jest/types": "workspace:^" "@types/fb-watchman": ^2.0.0 "@types/graceful-fs": ^4.1.3 @@ -12996,6 +12996,7 @@ __metadata: "@jest/console": "workspace:^" "@jest/environment": "workspace:^" "@jest/test-result": "workspace:^" + "@jest/test-utils": "workspace:^" "@jest/transform": "workspace:^" "@jest/types": "workspace:^" "@tsd/typescript": ^4.9.0