typescript - Jest 无法导入包,而我的其他打字稿代码导入它
问题描述
在 Jest 测试文件中,如果我这样做:
import _ from 'lodash';
然后_
评估为undefined
。
如果,OTOH,我这样做:
import * as _ from 'lodash';
然后_
得到评估就好了。
我的问题是这怎么可能,因为在我的 Typescript 代码库的其他部分我正在做import _ from lodash
没有任何问题。我在假设 Jest 使用与tsconfig.js
我的捆绑器相同的情况下进行操作babel.config.js
,那么为什么我会得到这种不同的结果呢?
我jest.config.js
从这里获得了一个非常简单的配置:
module.exports = {
"roots": [
"<rootDir>/src"
],
"testMatch": [
"**/__tests__/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
}
这是我的babel.config.js
:
module.exports = {
"presets": ["@babel/preset-env", "@babel/react"],
"compact" : false,
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
最后我tsconfig.json
的是:
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist/",
"sourceMap": true,
"module": "es6",
"target": "es5",
"jsx": "react",
"allowJs": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/typings/*",
"node_modules/@siedlerchr/types-ol-ext/@types/*"
]
},
"resolveJsonModule": true
}
}
解决方案
推荐阅读
- c++ - 在控制台上输出我从文件中读取的整数,使程序陷入无限循环?
- php - Phpspreadsheet - set Hyperlink for cell
- angular - 角度动态字符串绑定
- azure-cloud-services - Errors when trying to use StackExchange REDIS in Azure Cloud Service Worker Role
- node.js - 如何使用分子.js 从 bodyParser 访问 post 参数
- python-3.x - Class inheritance with constructors results in NoneType
- ios - 通过覆盖 TabbarViewController 中的 shouldSelect 方法推送 View Controller
- c# - 在 Winforms 中的窗体之间切换
- google-apps-script - Apps Script query not copying over working row
- node.js - 在学习 Node 之前我应该知道什么?(先决条件)