当我尝试设置一个简单的测试文件时,根据jest documentation,我得到了一些linter错误:
连接:Type 'Promise<MongoClient> & void' is missing the following properties from type '{ db: (arg0: any) => any; close: () => any; }': db, closets(2739)
globalThis.MONGO_URI_:Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017)
使用新URL解析器:No overload matches this call. Overload 1 of 4, '(url: string, callback: Callback<MongoClient>): void', gave the following error. Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; }' is not assignable to parameter of type 'Callback<MongoClient>'....
我尝试使用以下声明设置global.d.ts
(as per these solutions)文件:
export interface globalThis {}
declare module globalThis {
var __MONGO_URI__: string;
var __MONGO_DB_NAME__: string;
}
export interface global {}
declare global {
var __MONGO_URI__: string;
var __MONGO_DB_NAME__: string;
}
我认为这解决了之前的globalThis.__MONGO_URI__
错误,但我重新打开VS Code,错误再次出现。
package.json依赖项:
"dependencies": {
"cross-env": "^7.0.3",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"node-cron": "^3.0.0",
"snoostorm": "^1.5.2",
"snoowrap": "^1.23.0",
"mongodb": "^4.6.0"
},
"devDependencies": {
"@babel/core": "^7.18.2",
"@babel/preset-env": "^7.18.2",
"@babel/preset-typescript": "^7.17.12",
"@shelf/jest-mongodb": "^3.0.1",
"@types/express": "^4.17.13",
"@types/jest": "^27.5.1",
"@types/mongodb": "^4.0.7",
"@types/node": "^17.0.36",
"@types/node-cron": "^3.0.1",
"@types/request": "^2.48.8",
"jest": "^28.1.0",
"jest-environment-node": "^28.1.0",
"ts-jest": "^28.0.3",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.2"
},
1条答案
按热度按时间qvk1mo1f1#
对我来说,使用
process.env.MONGO_URL
来替换globalThis._MONGO_URI_
是可行的:这样,您将使用lib @shelf/jest-mongodb中的测试数据库。