文件:jest.config.js
preset: "@shelf/jest-mongodb",
error TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no
jest-mongodb-config.js
module.exports = {
mongodbMemoryServerOptions: {
instance: {
dbName: 'jest',
},
binary: {
version: '4.0.3',
skipMD5: true,
},
autoStart: false,
},
};
jest.config.js
module.exports = {
roots: ["<rootDir>/src", "<rootDir>/tests"],
collectCoverageFrom: [
'<rootDir>/src/**/*.ts',
],
testEnvironment: 'node',
preset: "@shelf/jest-mongodb",
coverageDirectory: "coverage",
testEnvironment: "node",
transform: {
".+\\.ts$": "ts-jest",
},
moduleNameMapper: {
'@/tests/(.*)': '<rootDir>/tests/$1',
'@/(.*)': '<rootDir>/src/$1'
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$",
};
用法
beforeAll(async () => {
client = await MongoClient.connect(global.__MONGO_URI__, {
useNewUrlParser: true,
useUnifiedTopology: true,
} as ConnectOptions)
db = client.db()
})
我已经尝试过:
npm --save-dev @shelf/jest-mongodb
yarn add @shelf/jest-mongodb --dev
解决了! 我从 process.env 获取内容并找到 MONGO_URL。 所以我改变了:
global.__MONGO_URI__
至
process.env.MONGO_URL
请勿与下划线一起使用!不行的
process.env.__MONGO_URL__