简体   繁体   English

Jest 无法从“src/tests/X.test.ts”中找到打字稿模块“../X.js”

[英]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 都会对我大喊大叫。

This is my project directory这是我的项目目录
.
├── jest.config.js
├── package.json
├── src
│   ├── main.ts
│   └── tests
│       └── main.test.ts
└── tsconfig.json
jest.config.js jest.config.js
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
  preset: 'ts-jest',
  testEnvironment: 'node',
};
package.json包.json
{
  "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"
  }
}
tsconfig.json tsconfig.json文件
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "rootDir": "./src",
    "outDir": "./dist",
    "strict": true,
    "skipLibCheck": true
  }
}

./src/main.ts ./src/main.ts

import chalk from "chalk";

function dumyFn(num: number, num2: number) {
  return num + num2;
}

export { dumyFn };

./src/tests/main.test.ts ./src/tests/main.test.ts

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 编译器对我大喊大叫: TypeScript 编译器错误

How can get rid of both of the problems?如何摆脱这两个问题?

I decided to use vitest instead of jest and it works easily out of the box with TypeScript.我决定使用vitest而不是jest ,它可以轻松地与 TypeScript 一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JsFiddle:任何使用单独的 x.js 来测试 isJsFileLoaded() 的方法 - JsFiddle: Any way to use a separate x.js to test isJsFileLoaded() 当我构建时,VS找不到“x.js”但使用“x.min.js” - When I build, VS can't find “x.js” but uses “x.min.js” Jest 测试错误 - 无法从“jest_expect.js”中找到模块“expect” - Jest Tests Error - Cannot find module 'expect' from 'jest_expect.js' 汇总别名和 Jest:无法从 'src/index.js' 中找到模块 'global/http' - Rollup aliases and Jest: Cannot find module 'global/http' from 'src/index.js' 开玩笑测试:在 typescript 组件导入中找不到模块 - Jest test: Cannot find module, in typescript component import 如何正确使用“ x.js”库格式化货币? - How to properly use “x.js” library to format currency? React&Jest:无法从测试文件中找到模块 - React & Jest: Cannot find module from test file ts-jest 无法运行 tsx 测试文件,因为从模块的 js 文件“导入” - ts-jest fails to run a tsx test file because of “Import” from a module's js file 尝试将 jest 与 create-react-app 和 typescript 一起使用。 获取错误:开玩笑:无法解析 TypeScript 配置文件...找不到模块“ts-node” - Trying to use jest with create-react-app and typescript. Get error: Jest: Failed to parse the TypeScript config file... Cannot find module 'ts-node' Jest 测试运行 - 找不到模块错误 - Jest test runs - Cannot find module error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM