[英]Jest cannot find typescript module '../X.js' from 'src/tests/X.test.ts'
I've been trying to find a way to test my TypeScript project but with every try Jest or typescript have yelled at me.我一直在尝试寻找一种方法来测试我的TypeScript项目,但每次尝试 Jest 或 TypeScript 都会对我大喊大叫。
.
├── jest.config.js
├── package.json
├── src
│ ├── main.ts
│ └── tests
│ └── main.test.ts
└── tsconfig.json
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'node',
};
{
"type": "module",
"scripts": {
"test": "jest"
},
"devDependencies": {
"@types/jest": "^29.2.4",
"jest": "^29.3.1",
"ts-jest": "^29.0.3"
},
"dependencies": {
"chalk": "^5.1.2"
}
}
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"skipLibCheck": true
}
}
import chalk from "chalk";
function dumyFn(num: number, num2: number) {
return num + num2;
}
export { dumyFn };
import { dumyFn } from "../main.js"; // Problem is here
test("simple dummy test", () => {
expect(dumyFn(2, 3)).toBe(5);
});
Whenever I try to run this test it's going to fail and the error is.每当我尝试运行此测试时,它都会失败并且出现错误。
FAIL src/tests/main.test.ts
● Test suite failed to run
Cannot find module '../main.js' from 'tests/main.test.ts'
> 1 | import { dumyFn } from "../main.js"; // problem is here
| ^
2 | test("simple dummy test", () => {
3 | expect(dumyFn(2, 3)).toBe(5);
4 | });
at Resolver._throwModNotFoundError (../node_modules/jest-resolve/build/resolver.js:425:11)
at Object.<anonymous> (tests/main.test.ts:1:1)
I Found out that if I change the first line of src/tests/main.test.ts
something like below, this problem fades away but a new problem comes to me.我发现如果我像下面这样更改
src/tests/main.test.ts
的第一行,这个问题就会消失,但一个新问题出现了。
// import { dumyFn } from "../main.js"; // problem is here
import { dumyFn } from "../main";
TypeScript compiler yells at me: TypeScript 编译器对我大喊大叫:
How can get rid of both of the problems?如何摆脱这两个问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.