[英]TypeORM RepositoryNotFoundError when searching for entities using the class in Jest
我在这里遇到了一个难以调试的问题。 我升级了所有项目依赖项,突然我的所有测试(Jest 25.5.4 或 26.x)开始失败,并出现“RepositoryNotFoundError”。
奇怪的行为是所有实体都加载到元数据存储中:
import { Connection, getMetadataArgsStorage } from 'typeorm';
let connection = await createConnection(); //<- The connection is creating according to my config
console.log(getMetadataArgsStorage()); //<- All the entities are here
console.log(getRepository('User')); //<- This works
console.log(getRepository(User)); //<- But this will raise the error
经过一段时间的调试,我注意到错误出现在https://github.com/typeorm/typeorm/blob/0.2.24/src/connection/Connection.ts#L482并且我为您创建了一个存储库来复制问题。
比较(metadata.target === target)
总是返回false
。 目标来自相同的 class,但它们有些不同。 使用toString()
返回 class 的不同版本,一个带注释,另一个不带注释(如果我在我的 tsc 配置中使用removeComments: true
):
const targetMetadata = connection.entityMetadatas[7].target; // 7 is the index in my debug, it can be anything else in the array
console.log(targetMetadata === User); // prints false
我仍然没有弄清楚升级后导致问题的原因。 不幸的是,我无法分享该项目的代码,但如果您需要,我可以提供更多信息。 你能帮我找出问题所在吗?
我的笑话配置(在 package.json 中):
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
我还没有尝试修改您提供的示例存储库,但我看到您的 TypeORM 配置文件中的路径以dist/
开头。 如果您使用的是ts-jest
并且还设置了src
作为您的rootDir
,我认为这可能是您遇到麻烦的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.