我试图获得使用Mongoose(在Mongodb中拖动)与Jest一起工作的Node Express/Typescript应用程序。我知道错误是什么,我只是找不到正确的配置,让jest和mongodb很好地发挥。
无论我做什么,我都得到:
chart-review-server2.0/node_modules/mongodb/src/bson.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import type {
^^^^^^
SyntaxError: Cannot use import statement outside a module
或者如果我试图在我的jest.config.js配置中使用transformIgnorePatterns将其排除在配置中,像这样:transformIgnorePatterns:['node_modules/(?!(mongodb))'],
我得到:
RangeError: Maximum call stack size exceeded
at Object.get [as ObjectId] (node_modules/mongodb/src/bson.ts:38:3)
at Object.get [as ObjectId] (node_modules/mongodb/src/bson.ts:38:3)...
我的jest.config.js文件看起来像这样:
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
transformIgnorePatterns: [`node_modules/(?!(mongodb))`],
transform: {
'^.+\.ts?$': 'ts-jest'
},
moduleDirectories: [ "node_modules", "src", "test"],
verbose: true,
testMatch: ["<rootDir>/test/*(*.)+(test).+(ts)"],
setupFiles: [
'dotenv/config'
],
};
我tsconfig。Json是这样的:
{
"compilerOptions": {
"lib": [
"es6"
],
"module": "commonjs",
"target": "es5",
"rootDirs": [
"./src"
],
"outDir": "./dist",
"baseUrl": "./src",
"moduleResolution": "node",
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"allowJs": true,
"downlevelIteration": true,
"isolatedModules": false,
"noEmit": true
},
"include": [
"src/**/*.ts",
"test/**/*.ts"
],
"exclude": [
"node_modules"
],
"types": [
"node"
],
"typeRoots": [
"node_modules/@types"
]
}
提前感谢任何回复,并阅读这篇文章。
在jest.config.js上注释moduleDirectories
解决了我的问题。所有功劳都归功于@jimmythecode,看看他的答案。