[英]How do you update a yarn workspace monorepo to node 18 with yarn typescript and jest?
This year (2023) support for Node 14 and 16 ends.今年(2023 年)对 Node 14 和 16 的支持结束。 So we chose to migrate to 18. This migration issue is from a yarn 2 workspace monorepo running node 14 to a yarn 3 workspace monorepo running node 18.
所以我们选择迁移到18。这个迁移问题是从运行节点14的yarn 2 workspace monorepo到运行节点18的yarn 3 workspace monorepo。
The first issue we ran into is this:我们遇到的第一个问题是:
TypeError: BigInt.prototype.toString requires that 'this' be a BigInt
You can't run node 18 and yarn 2 with workspaces, you need to go to yarn 3. Getting to yarn 3 is not that hard, but getting jest to play nice is where your issues will be.您不能使用工作区运行节点 18 和 yarn 2,您需要 go 到 yarn 3。进入 yarn 3 并不难,但是开玩笑玩得开心是您的问题所在。 Here is a path forward.
这是一条前进的道路。
Start with your types.从你的类型开始。 Update to the latest version, and fix all your type errors.
更新到最新版本,并修复所有类型错误。 Node 18 typescript is not as forgiving as Node 14. Things will outright fail without helpful errors.
节点 18 typescript 不像节点 14 那样宽容。如果没有有用的错误,事情将彻底失败。 This will probably improve with future versions.
这可能会在未来的版本中得到改善。 But right now things will fail for no apparent reason, and it will be some type error buried.
但是现在事情会无缘无故地失败,并且会埋下一些类型错误。 Best to fix all your type warnings before you even start.
最好在开始之前修复所有类型警告。 This includes in your test files.
这包括在您的测试文件中。
If you are not on Babel 7 you might want to get there.如果你不在 Babel 7 上,你可能想去那里。 You will probably need babel to get jest working.
你可能需要 babel 来开玩笑。 https://babeljs.io/docs/v7-migration
https://babeljs.io/docs/v7-migration
Also a good resource is the monorepo instructions here: https://babeljs.io/docs/config-files#file-relative-configuration这里的 monorepo 说明也是一个很好的资源: https://babeljs.io/docs/config-files#file-relative-configuration
If you are like me and your project has some experimental stuff in it you may need some extra stuff in your babel config.如果你像我一样并且你的项目中有一些实验性的东西,你可能需要在你的 babel 配置中添加一些额外的东西。 But it depends on the scope of your project.
但是要看你项目的scope。 Mine ended up looking about like this:
我的最终看起来像这样:
{
"presets": [
["@babel/preset-env", { "targets": { "node": "current" } }],
"@babel/preset-typescript"
],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }]
]
}
As of this writing @types/jest does not play nice with yarn 3 and node 18. @types/jest is convenient, but it will throw errors and warnings.在撰写本文时,@types/jest 不能很好地与 yarn 3 和节点 18 配合使用。@types/jest 很方便,但它会抛出错误和警告。 It is actually not supported by Jest, it is a 3rd party library.
它实际上不受 Jest 支持,它是一个 3rd 方库。 The solution is to use "@jest/globals".
解决方案是使用“@jest/globals”。 It gives warnings if you attempt to implement it globally.
如果您尝试在全球范围内实施它,它会发出警告。 It is probably possible to implement globally, but it might just be easier to grab a JR dev and tell them to import the jest variables into each test file.
全球实施可能是可能的,但抓住 JR 开发人员并告诉他们将笑话变量导入每个测试文件可能会更容易。 So go ahead and remove "@types/jest".
所以 go 提前删除“@types/jest”。
import { jest, describe, it, expect, beforeEach ... whatever else } from '@jest/globals"
Likewise "ts-jest" does not play nice as well.同样,“ts-jest”也玩得不好。 I could not get all the tests working until I went with the jest babel implementation.
在我使用 jest babel 实现之前,我无法让所有测试工作。 I recommend removing ts-jest as well.
我建议也删除 ts-jest。
Use the "Via babel" instructions from here: https://jestjs.io/docs/getting-started#using-typescript使用此处的“Via babel”说明: https://jestjs.io/docs/getting-started#using-typescript
Also a good read is the "Type definitions" section on the same page.同样值得一读的是同一页上的“类型定义”部分。
Yarn 2 is not supported on node 18 for yarn workspaces. Yarn 2 在 yarn 工作区的节点 18 上不受支持。 To be able to continue to use yarn, you probably will have to update to yarn 3. You can use "yarn set version" to update to yarn 3. https://yarnpkg.com/cli/set/version
为了能够继续使用 yarn,您可能必须更新到 yarn 3。您可以使用“yarn set version”来更新到 yarn 3。https://yarnpkg.com/cli/set/version
Depending on your project you may need to be sure that corepack is enabled.根据您的项目,您可能需要确保已启用 corepack。 If you set your yarn version to 3, and you are running node 18, and yarn still says version 1, it is probably missing corepack.
如果您将 yarn 版本设置为 3,并且您正在运行节点 18,而 yarn 仍然显示版本 1,则它可能缺少 corepack。 https://yarnpkg.com/getting-started/install
https://yarnpkg.com/getting-started/install
As of April 2023: These commands should work to update to yarn 3:截至 2023 年 4 月:这些命令应该可以更新到 yarn 3:
// Update to latest // 更新到最新
yarn set version stable
// Update workspace tools. // 更新工作区工具。
yarn plugin import @yarnpkg/plugin-workspace-tools
// Clean the cache out of all your workspaces // 清除所有工作区的缓存
yarn workspaces focus --all --production && rm -rf "$(yarn cache clean)"
From here you should be past the major obstacles and you will probably just need to clean up some tests.从这里开始,您应该越过了主要障碍,您可能只需要清理一些测试。
Hope this helps you spend less time digging through docs, and get your project up and working.希望这可以帮助您减少花在挖掘文档上的时间,并让您的项目启动并运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.