Skip to content
/ jest Public
forked from jestjs/jest

Commit

Permalink
Enforce same version of a dependency (jestjs#12668)
Browse files Browse the repository at this point in the history
  • Loading branch information
F3n67u authored Apr 19, 2022
1 parent 7f484cb commit ccecdae
Show file tree
Hide file tree
Showing 50 changed files with 464 additions and 310 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
run: yarn verify-old-ts

lint:
name: Running ESLint
name: Running Lint
runs-on: ubuntu-latest
needs: prepare-yarn-cache

Expand All @@ -82,6 +82,20 @@ jobs:
- name: check copyright headers
run: yarn check-copyright-headers

yarn-validate:
name: Validate Yarn constraints
runs-on: ubuntu-latest
needs: prepare-yarn-cache
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: yarn
- name: 'Check for unmet constraints (fix w/ "yarn constraints --fix")'
run: |
yarn constraints
test:
name: Node v${{ matrix.node-version }} on ${{ matrix.os }} (${{ matrix.shard }})
strategy:
Expand Down
52 changes: 52 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-constraints.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ packageExtensions:
"@babel/preset-env": ^7.1.6

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-constraints.cjs
spec: "@yarnpkg/plugin-constraints"
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
Expand Down
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ Time: 0.232 s, estimated 1 s
Ran all test suites.
```

## Checking Constraints

We use [Yarn Constraints](https://yarnpkg.com/features/constraints) to enforce various rules across the repository. They are declared inside the [`constraints.pro` file](https://github.com/facebook/jest/blob/main/constraints.pro) and their purposes are documented with comments.

Constraints can be checked with `yarn constraints`, and fixed with `yarn constraints --fix`. Generally speaking:

- Workspaces must not depend on conflicting ranges of dependencies. Use the `-i,--interactive` flag and select "Reuse" when installing dependencies and you shouldn't ever have to deal with this rule.

- A dependency doesn't appear in both `dependencies` and `devDependencies` of the same workspace.

- Workspaces must point our repository through the `repository` field.

##### Using jest-circus

There may be cases where you want to run jest using `jest-circus` instead of `jest-jasmine2` (which is the default runner) for integration testing. In situations like this, set the environment variable `JEST_CIRCUS` to 1. That will configure jest to use `jest-circus`. So something like this.
Expand Down
82 changes: 82 additions & 0 deletions constraints.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
constraints_min_version(1).

% This file is written in Prolog
% It contains rules that the project must respect.
% Check with "yarn constraints" (fix w/ "yarn constraints --fix")
% Yarn Constraints https://yarnpkg.com/features/constraints
% Reference for other constraints:
% https://github.com/babel/babel/blob/main/constraints.pro
% https://github.com/yarnpkg/berry/blob/master/constraints.pro

% This rule will enforce that a workspace MUST depend on the same version of a dependency as the one used by the other workspaces
gen_enforced_dependency(WorkspaceCwd, DependencyIdent, DependencyRange2, DependencyType) :-
% Iterates over all dependencies from all workspaces
workspace_has_dependency(WorkspaceCwd, DependencyIdent, DependencyRange, DependencyType),
% Iterates over similarly-named dependencies from all workspaces (again)
workspace_has_dependency(OtherWorkspaceCwd, DependencyIdent, DependencyRange2, DependencyType2),
% Ignore peer dependencies
DependencyType \= 'peerDependencies',
DependencyType2 \= 'peerDependencies',
% Ignore workspace:*: we use both `workspace:*` and real version such as `^28.0.0-alpha.8` to reference package in monorepo
% TODO: in the future we should make it consistent and remove this ignore
DependencyRange \= 'workspace:*',
DependencyRange2 \= 'workspace:*',
% Get the workspace name
workspace_ident(WorkspaceCwd, WorkspaceIdent),
workspace_ident(OtherWorkspaceCwd, OtherWorkspaceIdent),
% @types/node in the root need to stay on ~12.12.0
(
(WorkspaceIdent = '@jest/monorepo'; OtherWorkspaceIdent = '@jest/monorepo') ->
DependencyIdent \= '@types/node'
;
true
),
% Allow enzyme example workspace use a older version react and react-dom, because enzyme don't support react 17
(
(WorkspaceIdent = 'example-enzyme'; OtherWorkspaceIdent = 'example-enzyme') ->
\+ member(DependencyIdent, ['react', 'react-dom'])
;
true
).

% Enforces that a dependency doesn't appear in both `dependencies` and `devDependencies`
gen_enforced_dependency(WorkspaceCwd, DependencyIdent, null, 'devDependencies') :-
workspace_has_dependency(WorkspaceCwd, DependencyIdent, _, 'devDependencies'),
workspace_has_dependency(WorkspaceCwd, DependencyIdent, _, 'dependencies').

% Enforces the license in all public workspaces while removing it from private workspaces
gen_enforced_field(WorkspaceCwd, 'license', 'MIT') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
gen_enforced_field(WorkspaceCwd, 'license', null) :-
workspace_field(WorkspaceCwd, 'private', true).

% Enforces the repository field for all public workspaces while removing it from private workspaces
gen_enforced_field(WorkspaceCwd, 'repository.type', 'git') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
gen_enforced_field(WorkspaceCwd, 'repository.url', 'https://github.com/facebook/jest.git') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
gen_enforced_field(WorkspaceCwd, 'repository.directory', WorkspaceCwd) :-
\+ workspace_field(WorkspaceCwd, 'private', true).
gen_enforced_field(WorkspaceCwd, 'repository', null) :-
workspace_field(WorkspaceCwd, 'private', true).

% Enforces 'publishConfig.access' is set to public for public workspaces while removing it from private workspaces
gen_enforced_field(WorkspaceCwd, 'publishConfig.access', 'public') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
gen_enforced_field(WorkspaceCwd, 'publishConfig.access', null) :-
workspace_field(WorkspaceCwd, 'private', true).

% Enforces the engines.node field for public workspace
gen_enforced_field(WorkspaceCwd, 'engines.node', '^12.13.0 || ^14.15.0 || ^16.13.0 || >=17.0.0') :-
\+ workspace_field(WorkspaceCwd, 'private', true).

% Enforces the main and types field to start with ./
gen_enforced_field(WorkspaceCwd, FieldName, ExpectedValue) :-
% Fields the rule applies to
member(FieldName, ['main', 'types']),
% Get current value
workspace_field(WorkspaceCwd, FieldName, CurrentValue),
% Must not start with ./ already
\+ atom_concat('./', _, CurrentValue),
% Store './' + CurrentValue in ExpectedValue
atom_concat('./', CurrentValue, ExpectedValue).
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ exports[`processes stack traces and code frames with source maps with coverage 1
15 | }
16 |
at Object.error (lib.ts:14:9)
at Object.<anonymous> (__tests__/fails.ts:10:3)"
at error (lib.ts:14:9)
at Object.<anonymous> (__tests__/fails.ts:10:8)"
`;
2 changes: 1 addition & 1 deletion e2e/babel-plugin-jest-hoist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"@babel/preset-env": "^7.0.0",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-typescript": "^7.0.0",
"react": "*"
"react": "17.0.2"
},
"jest": {
"automock": true,
Expand Down
4 changes: 2 additions & 2 deletions e2e/babel-plugin-jest-hoist/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ __metadata:
languageName: node
linkType: hard

"react@npm:*":
"react@npm:17.0.2":
version: 17.0.2
resolution: "react@npm:17.0.2"
dependencies:
Expand Down Expand Up @@ -1703,7 +1703,7 @@ __metadata:
"@babel/preset-env": ^7.0.0
"@babel/preset-flow": ^7.0.0
"@babel/preset-typescript": ^7.0.0
react: "*"
react: 17.0.2
languageName: unknown
linkType: soft

Expand Down
2 changes: 1 addition & 1 deletion e2e/coverage-remapping/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"testEnvironment": "node"
},
"dependencies": {
"typescript": "^3.7.4"
"typescript": "^4.6.2"
}
}
18 changes: 9 additions & 9 deletions e2e/coverage-remapping/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ __metadata:
version: 0.0.0-use.local
resolution: "root-workspace-0b6124@workspace:."
dependencies:
typescript: ^3.7.4
typescript: ^4.6.2
languageName: unknown
linkType: soft

"typescript@npm:^3.7.4":
version: 3.9.10
resolution: "typescript@npm:3.9.10"
"typescript@npm:^4.6.2":
version: 4.6.3
resolution: "typescript@npm:4.6.3"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 46c842e2cd4797b88b66ef06c9c41dd21da48b95787072ccf39d5f2aa3124361bc4c966aa1c7f709fae0509614d76751455b5231b12dbb72eb97a31369e1ff92
checksum: 255bb26c8cb846ca689dd1c3a56587af4f69055907aa2c154796ea28ee0dea871535b1c78f85a6212c77f2657843a269c3a742d09d81495b97b914bf7920415b
languageName: node
linkType: hard

"typescript@patch:typescript@^3.7.4#~builtin<compat/typescript>":
version: 3.9.10
resolution: "typescript@patch:typescript@npm%3A3.9.10#~builtin<compat/typescript>::version=3.9.10&hash=bda367"
"typescript@patch:typescript@^4.6.2#~builtin<compat/typescript>":
version: 4.6.3
resolution: "typescript@patch:typescript@npm%3A4.6.3#~builtin<compat/typescript>::version=4.6.3&hash=bda367"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: dc7141ab555b23a8650a6787f98845fc11692063d02b75ff49433091b3af2fe3d773650dea18389d7c21f47d620fb3b110ea363dab4ab039417a6ccbbaf96fc2
checksum: 6bf45caf847062420592e711bc9c28bf5f9a9a7fa8245343b81493e4ededae33f1774009d1234d911422d1646a2c839f44e1a23ecb111b40a60ac2ea4c1482a8
languageName: node
linkType: hard
2 changes: 1 addition & 1 deletion e2e/stack-trace-source-maps-with-coverage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"testRegex": "fails"
},
"dependencies": {
"typescript": "^3.7.4"
"typescript": "^4.6.2"
}
}
18 changes: 9 additions & 9 deletions e2e/stack-trace-source-maps-with-coverage/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ __metadata:
version: 0.0.0-use.local
resolution: "root-workspace-0b6124@workspace:."
dependencies:
typescript: ^3.7.4
typescript: ^4.6.2
languageName: unknown
linkType: soft

"typescript@npm:^3.7.4":
version: 3.9.10
resolution: "typescript@npm:3.9.10"
"typescript@npm:^4.6.2":
version: 4.6.3
resolution: "typescript@npm:4.6.3"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 46c842e2cd4797b88b66ef06c9c41dd21da48b95787072ccf39d5f2aa3124361bc4c966aa1c7f709fae0509614d76751455b5231b12dbb72eb97a31369e1ff92
checksum: 255bb26c8cb846ca689dd1c3a56587af4f69055907aa2c154796ea28ee0dea871535b1c78f85a6212c77f2657843a269c3a742d09d81495b97b914bf7920415b
languageName: node
linkType: hard

"typescript@patch:typescript@^3.7.4#~builtin<compat/typescript>":
version: 3.9.10
resolution: "typescript@patch:typescript@npm%3A3.9.10#~builtin<compat/typescript>::version=3.9.10&hash=bda367"
"typescript@patch:typescript@^4.6.2#~builtin<compat/typescript>":
version: 4.6.3
resolution: "typescript@patch:typescript@npm%3A4.6.3#~builtin<compat/typescript>::version=4.6.3&hash=bda367"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: dc7141ab555b23a8650a6787f98845fc11692063d02b75ff49433091b3af2fe3d773650dea18389d7c21f47d620fb3b110ea363dab4ab039417a6ccbbaf96fc2
checksum: 6bf45caf847062420592e711bc9c28bf5f9a9a7fa8245343b81493e4ededae33f1774009d1234d911422d1646a2c839f44e1a23ecb111b40a60ac2ea4c1482a8
languageName: node
linkType: hard
2 changes: 1 addition & 1 deletion e2e/stack-trace-source-maps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"testRegex": "fails"
},
"dependencies": {
"typescript": "^3.7.4"
"typescript": "^4.6.2"
}
}
18 changes: 9 additions & 9 deletions e2e/stack-trace-source-maps/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ __metadata:
version: 0.0.0-use.local
resolution: "root-workspace-0b6124@workspace:."
dependencies:
typescript: ^3.7.4
typescript: ^4.6.2
languageName: unknown
linkType: soft

"typescript@npm:^3.7.4":
version: 3.9.10
resolution: "typescript@npm:3.9.10"
"typescript@npm:^4.6.2":
version: 4.6.3
resolution: "typescript@npm:4.6.3"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 46c842e2cd4797b88b66ef06c9c41dd21da48b95787072ccf39d5f2aa3124361bc4c966aa1c7f709fae0509614d76751455b5231b12dbb72eb97a31369e1ff92
checksum: 255bb26c8cb846ca689dd1c3a56587af4f69055907aa2c154796ea28ee0dea871535b1c78f85a6212c77f2657843a269c3a742d09d81495b97b914bf7920415b
languageName: node
linkType: hard

"typescript@patch:typescript@^3.7.4#~builtin<compat/typescript>":
version: 3.9.10
resolution: "typescript@patch:typescript@npm%3A3.9.10#~builtin<compat/typescript>::version=3.9.10&hash=bda367"
"typescript@patch:typescript@^4.6.2#~builtin<compat/typescript>":
version: 4.6.3
resolution: "typescript@patch:typescript@npm%3A4.6.3#~builtin<compat/typescript>::version=4.6.3&hash=bda367"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: dc7141ab555b23a8650a6787f98845fc11692063d02b75ff49433091b3af2fe3d773650dea18389d7c21f47d620fb3b110ea363dab4ab039417a6ccbbaf96fc2
checksum: 6bf45caf847062420592e711bc9c28bf5f9a9a7fa8245343b81493e4ededae33f1774009d1234d911422d1646a2c839f44e1a23ecb111b40a60ac2ea4c1482a8
languageName: node
linkType: hard
6 changes: 3 additions & 3 deletions e2e/transform/multiple-transformers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"react": "*",
"react-dom": "*",
"react-test-renderer": "*"
"react": "17.0.2",
"react-dom": "^17.0.1",
"react-test-renderer": "17.0.2"
}
}
12 changes: 6 additions & 6 deletions e2e/transform/multiple-transformers/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,7 @@ __metadata:
languageName: node
linkType: hard

"react-dom@npm:*":
"react-dom@npm:^17.0.1":
version: 17.0.2
resolution: "react-dom@npm:17.0.2"
dependencies:
Expand Down Expand Up @@ -1732,7 +1732,7 @@ __metadata:
languageName: node
linkType: hard

"react-test-renderer@npm:*":
"react-test-renderer@npm:17.0.2":
version: 17.0.2
resolution: "react-test-renderer@npm:17.0.2"
dependencies:
Expand All @@ -1746,7 +1746,7 @@ __metadata:
languageName: node
linkType: hard

"react@npm:*":
"react@npm:17.0.2":
version: 17.0.2
resolution: "react@npm:17.0.2"
dependencies:
Expand Down Expand Up @@ -1853,9 +1853,9 @@ __metadata:
"@babel/core": ^7.0.0
"@babel/preset-env": ^7.0.0
"@babel/preset-react": ^7.0.0
react: "*"
react-dom: "*"
react-test-renderer: "*"
react: 17.0.2
react-dom: ^17.0.1
react-test-renderer: 17.0.2
languageName: unknown
linkType: soft

Expand Down
2 changes: 1 addition & 1 deletion e2e/typescript-coverage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"testEnvironment": "node"
},
"dependencies": {
"typescript": "^3.3.1"
"typescript": "^4.6.2"
}
}
Loading

0 comments on commit ccecdae

Please sign in to comment.