英文:
import https is undefined when using Jest with ts-jest (TypeScript)
问题
Issue: 在导入 Node.js 的本地模块 https
时出现 undefined
。
dummy.spec.file(我的测试文件)
import https from "https";
it("总是为真", () => {
console.log("https:");
console.log(https); // <- 这是 undefined
expect(true).toBe(true);
});
jest.config.js(Jest 配置文件)
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
Node.js 版本:v16.13.0
依赖项:
"dependencies": {
"jest": "^29.6.2",
"ts-jest": "^29.1.1"
},
"devDependencies": {
"@types/jest": "^29.5.3"
}
如何使 https
在测试文件(jest + TypeScript)中正常工作?
英文:
Issue: importing Nodejs native module https
is undefined
dummy.spec.file (my testing file)
import https from "https";
it("always true", () => {
console.log("https:");
console.log(https); // <- this is undefined
expect(true).toBe(true);
});
jest.config.js (Jest config file)
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
Nodejs version: v16.13.0
Dependencies:
"dependencies": {
"jest": "^29.6.2",
"ts-jest": "^29.1.1"
},
"devDependencies": {
"@types/jest": "^29.5.3"
}
How can I make https
work properly inside a testing file (jest + typescript) ?
答案1
得分: 0
可能的原因之一可能是您的TypeScript配置。检查是否与https模块导入存在任何问题。
在您的tsconfig.json文件中,请确保您有"module": "CommonJS"和"esModuleInterop": true
{
"compilerOptions": {
// ...
"module": "CommonJS",
"esModuleInterop": true,
// ...
}
}
英文:
One possible reason could be your Typescript configuration. Check that if it's causing any issues with the https module import.
In your tsconfig.json file, make sure you have "module": "CommonJS" and "esModuleInterop": true
{
"compilerOptions": {
// ...
"module": "CommonJS",
"esModuleInterop": true,
// ...
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论