Skip to content

Commit

Permalink
chore: verify TS project references are correct (#10941)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Dec 11, 2020
1 parent 39fe984 commit 15ca29c
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"stealthy-require": "^1.1.1",
"string-length": "^4.0.1",
"strip-ansi": "^6.0.0",
"strip-json-comments": "^3.1.1",
"tempy": "^1.0.0",
"throat": "^5.0.0",
"ts-node": "^9.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/jest-circus/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"rootDir": "src"
},
"references": [
{"path": "../expect"},
{"path": "../jest-each"},
{"path": "../jest-environment"},
{"path": "../jest-matcher-utils"},
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"outDir": "build"
},
"references": [
{"path": "../jest-core"},
{"path": "../jest-config"},
{"path": "../jest-core"},
{"path": "../jest-test-result"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-validate"}
{"path": "../jest-validate"},
{"path": "../test-utils"}
]
}
5 changes: 3 additions & 2 deletions packages/jest-config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"rootDir": "src",
"outDir": "build"
},
// TODO: This is missing `jest-jasmine2`, but that is just
// `require.resolve`d, so no real use for its types
// TODO: This is missing `babel-jest`, `jest-jasmine2`, `jest-circus` and
// jest-test-sequencer, but that is just `require.resolve`d, so no real use
// for their types
"references": [
{"path": "../jest-environment-jsdom"},
{"path": "../jest-environment-node"},
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
{"path": "../jest-message-util"},
{"path": "../jest-regex-util"},
{"path": "../jest-reporters"},
{"path": "../jest-resolve"},
{"path": "../jest-resolve-dependencies"},
{"path": "../jest-runner"},
{"path": "../jest-runtime"},
{"path": "../jest-snapshot"},
{"path": "../jest-test-result"},
{"path": "../jest-test-sequencer"},
{"path": "../jest-types"},
{"path": "../jest-transform"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-validate"},
{"path": "../jest-watcher"},
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-environment/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"references": [
{"path": "../jest-fake-timers"},
{"path": "../jest-types"},
{"path": "../jest-util"}
{"path": "../jest-mock"},
{"path": "../jest-types"}
]
}
1 change: 1 addition & 0 deletions packages/jest-fake-timers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"references": [
{"path": "../jest-message-util"},
{"path": "../jest-mock"},
{"path": "../jest-types"},
{"path": "../jest-util"}
]
}
3 changes: 2 additions & 1 deletion packages/jest-haste-map/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{"path": "../jest-serializer"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-worker"}
{"path": "../jest-worker"},
{"path": "../test-utils"}
]
}
1 change: 1 addition & 0 deletions packages/jest-leak-detector/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"outDir": "build"
},
"references": [
{"path": "../jest-get-type"},
{"path": "../pretty-format"}
]
}
1 change: 1 addition & 0 deletions packages/jest-reporters/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{"path": "../jest-haste-map"},
{"path": "../jest-resolve"},
{"path": "../jest-test-result"},
{"path": "../jest-transform"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-worker"},
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-resolve-dependencies/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"outDir": "build"
},
"references": [
{"path": "../jest-haste-map"},
{"path": "../jest-regex-util"},
{"path": "../jest-resolve"},
{"path": "../jest-runtime"},
{"path": "../jest-snapshot"},
{"path": "../jest-types"},
{"path": "../test-utils"}
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-resolve/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
"rootDir": "src",
"outDir": "build"
},
"references": [{"path": "../jest-types"}, {"path": "../jest-haste-map"}]
"references": [
{"path": "../jest-haste-map"},
{"path": "../jest-types"},
{"path": "../jest-util"}
]
}
4 changes: 2 additions & 2 deletions packages/jest-runner/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{"path": "../jest-test-result"},
{"path": "../jest-transform"},
{"path": "../jest-types"},
{"path": "../jest-worker"},
{"path": "../jest-util"}
{"path": "../jest-util"},
{"path": "../jest-worker"}
]
}
1 change: 0 additions & 1 deletion packages/jest-runtime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-validate"},
{"path": "../pretty-format"},
{"path": "../test-utils"}
]
}
4 changes: 2 additions & 2 deletions packages/jest-test-sequencer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"references": [
{"path": "../jest-haste-map"},
{"path": "../jest-runner"},
{"path": "../jest-test-result"},
{"path": "../jest-types"}
{"path": "../jest-runtime"},
{"path": "../jest-test-result"}
]
}
2 changes: 1 addition & 1 deletion packages/pretty-format/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"rootDir": "src",
"outDir": "build"
},
"references": [{"path": "../jest-types"}]
"references": [{"path": "../jest-types"}, {"path": "../jest-util"}]
}
58 changes: 57 additions & 1 deletion scripts/buildTs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const util = require('util');
const chalk = require('chalk');
const execa = require('execa');
const globby = require('globby');
const stripJsonComments = require('strip-json-comments');
const throat = require('throat');
const {getPackages} = require('./buildUtils');

Expand All @@ -27,8 +28,22 @@ const readFilePromise = util.promisify(fs.readFile);
fs.existsSync(path.resolve(p, 'tsconfig.json')),
);

const {stdout: allWorkspacesString} = await execa('yarn', [
'workspaces',
'list',
'--json',
]);

const workspacesWithTs = new Map(
JSON.parse(`[${allWorkspacesString.split('\n').join(',')}]`)
.filter(({location}) =>
packagesWithTs.some(pkg => pkg.endsWith(location)),
)
.map(({location, name}) => [name, location]),
);

packagesWithTs.forEach(pkgDir => {
const pkg = require(pkgDir + '/package.json');
const pkg = require(`${pkgDir}/package.json`);

assert.ok(pkg.types, `Package ${pkg.name} is missing \`types\` field`);

Expand All @@ -37,6 +52,47 @@ const readFilePromise = util.promisify(fs.readFile);
pkg.main.replace(/\.js$/, '.d.ts'),
`\`main\` and \`types\` field of ${pkg.name} does not match`,
);

const jestDependenciesOfPackage = Object.keys(pkg.dependencies || {})
.concat(Object.keys(pkg.devDependencies || {}))
.filter(dep => workspacesWithTs.has(dep))
.filter(dep => {
// nothing should depend on these
if (dep === 'jest-circus' || dep === 'jest-jasmine2') {
return false;
}

// these are just `require.resolve`-ed
if (pkg.name === 'jest-config') {
if (dep === '@jest/test-sequencer' || dep === 'babel-jest') {
return false;
}
}

return true;
})
.map(dep =>
path.relative(pkgDir, `${pkgDir}/../../${workspacesWithTs.get(dep)}`),
)
.sort();

if (jestDependenciesOfPackage.length > 0) {
const tsConfig = JSON.parse(
stripJsonComments(fs.readFileSync(`${pkgDir}/tsconfig.json`, 'utf8')),
);

const references = tsConfig.references.map(({path}) => path);

assert.deepStrictEqual(
references,
jestDependenciesOfPackage,
`Expected declared references to match dependencies in packages ${
pkg.name
}. Got:\n\n${references.join(
'\n',
)}\nExpected:\n\n${jestDependenciesOfPackage.join('\n')}`,
);
}
});

const args = ['tsc', '-b', ...packagesWithTs, ...process.argv.slice(2)];
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,7 @@ __metadata:
stealthy-require: ^1.1.1
string-length: ^4.0.1
strip-ansi: ^6.0.0
strip-json-comments: ^3.1.1
tempy: ^1.0.0
throat: ^5.0.0
ts-node: ^9.0.0
Expand Down

0 comments on commit 15ca29c

Please sign in to comment.