Skip to content

Commit

Permalink
chore: move @jest/test-utils reference (#13468)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Feb 4, 2023
1 parent 48ddbf4 commit ffe2352
Show file tree
Hide file tree
Showing 41 changed files with 108 additions and 62 deletions.
3 changes: 2 additions & 1 deletion e2e/tsconfig.json
Expand Up @@ -3,5 +3,6 @@
"compilerOptions": {
"rootDir": "./"
},
"include": ["./**/*"]
"include": ["./**/*"],
"references": [{"path": "../packages/test-utils"}]
}
2 changes: 1 addition & 1 deletion packages/babel-jest/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
2 changes: 1 addition & 1 deletion packages/babel-jest/tsconfig.json
Expand Up @@ -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"}]
}
2 changes: 1 addition & 1 deletion packages/expect/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/expect/tsconfig.json
Expand Up @@ -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"}
]
}
2 changes: 1 addition & 1 deletion packages/jest-console/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-console/tsconfig.json
Expand Up @@ -9,7 +9,6 @@
"references": [
{"path": "../jest-message-util"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../test-utils"}
{"path": "../jest-util"}
]
}
6 changes: 2 additions & 4 deletions packages/jest-core/src/__tests__/tsconfig.json
@@ -1,7 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"compilerOptions": {
"rootDir": "../"
},
"include": ["../**/*"]
"include": ["./**/*"],
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
2 changes: 1 addition & 1 deletion packages/jest-core/src/lib/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../../"}]
"references": [{"path": "../../../"}, {"path": "../../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-core/tsconfig.json
Expand Up @@ -26,7 +26,6 @@
{"path": "../jest-util"},
{"path": "../jest-validate"},
{"path": "../jest-watcher"},
{"path": "../pretty-format"},
{"path": "../test-utils"}
{"path": "../pretty-format"}
]
}
2 changes: 1 addition & 1 deletion packages/jest-diff/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-diff/tsconfig.json
Expand Up @@ -9,7 +9,6 @@
"references": [
{"path": "../diff-sequences"},
{"path": "../jest-get-type"},
{"path": "../pretty-format"},
{"path": "../test-utils"}
{"path": "../pretty-format"}
]
}
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-environment-jsdom/tsconfig.json
Expand Up @@ -11,7 +11,6 @@
{"path": "../jest-fake-timers"},
{"path": "../jest-mock"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../test-utils"}
{"path": "../jest-util"}
]
}
2 changes: 1 addition & 1 deletion packages/jest-environment-node/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-environment-node/tsconfig.json
Expand Up @@ -11,7 +11,6 @@
{"path": "../jest-fake-timers"},
{"path": "../jest-mock"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../test-utils"}
{"path": "../jest-util"}
]
}
2 changes: 1 addition & 1 deletion packages/jest-fake-timers/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-fake-timers/tsconfig.json
Expand Up @@ -11,7 +11,6 @@
{"path": "../jest-message-util"},
{"path": "../jest-mock"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../test-utils"}
{"path": "../jest-util"}
]
}
1 change: 0 additions & 1 deletion packages/jest-haste-map/package.json
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-haste-map/tsconfig.json
Expand Up @@ -10,7 +10,6 @@
{"path": "../jest-regex-util"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-worker"},
{"path": "../test-utils"}
{"path": "../jest-worker"}
]
}
2 changes: 1 addition & 1 deletion packages/jest-matcher-utils/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-matcher-utils/tsconfig.json
Expand Up @@ -10,7 +10,6 @@
"references": [
{"path": "../jest-diff"},
{"path": "../jest-get-type"},
{"path": "../pretty-format"},
{"path": "../test-utils"}
{"path": "../pretty-format"}
]
}
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-reporters/tsconfig.json
Expand Up @@ -14,7 +14,6 @@
{"path": "../jest-transform"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-worker"},
{"path": "../test-utils"}
{"path": "../jest-worker"}
]
}
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-resolve-dependencies/tsconfig.json
Expand Up @@ -12,7 +12,6 @@
{"path": "../jest-resolve"},
{"path": "../jest-runtime"},
{"path": "../jest-snapshot"},
{"path": "../jest-types"},
{"path": "../test-utils"}
{"path": "../jest-types"}
]
}
1 change: 1 addition & 0 deletions packages/jest-runner/package.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runner/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-runtime/tsconfig.json
Expand Up @@ -23,7 +23,6 @@
{"path": "../jest-test-result"},
{"path": "../jest-transform"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../test-utils"}
{"path": "../jest-util"}
]
}
2 changes: 1 addition & 1 deletion packages/jest-snapshot/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-snapshot/tsconfig.json
Expand Up @@ -17,7 +17,6 @@
{"path": "../jest-transform"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../pretty-format"},
{"path": "../test-utils"}
{"path": "../pretty-format"}
]
}
2 changes: 1 addition & 1 deletion packages/jest-test-sequencer/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
6 changes: 1 addition & 5 deletions packages/jest-test-sequencer/tsconfig.json
Expand Up @@ -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"}]
}
2 changes: 1 addition & 1 deletion packages/jest-transform/src/__tests__/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.test.json",
"include": ["./**/*"],
"references": [{"path": "../../"}]
"references": [{"path": "../../"}, {"path": "../../../test-utils"}]
}
3 changes: 1 addition & 2 deletions packages/jest-transform/tsconfig.json
Expand Up @@ -10,7 +10,6 @@
{"path": "../jest-haste-map"},
{"path": "../jest-regex-util"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../test-utils"}
{"path": "../jest-util"}
]
}
1 change: 1 addition & 0 deletions packages/test-utils/package.json
Expand Up @@ -12,6 +12,7 @@
"./package.json": "./package.json"
},
"dependencies": {
"@jest/globals": "workspace:^",
"@jest/types": "workspace:^",
"@types/node": "*",
"ansi-regex": "^5.0.1",
Expand Down
7 changes: 3 additions & 4 deletions packages/test-utils/src/ConditionalTest.ts
Expand Up @@ -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';
Expand Down
6 changes: 5 additions & 1 deletion packages/test-utils/tsconfig.json
Expand Up @@ -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"}
]
}
60 changes: 60 additions & 0 deletions scripts/buildTs.mjs
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit ffe2352

Please sign in to comment.