使用 ES 模块与 TS,以及 Jest 测试(不能在模块外使用 import 语句)

问题描述 投票:0回答:1

自从我开始使用 Express 和 TS 之旅以来,这是我第一次尝试测试。我决定用 Jest 对一个新项目进行一些测试,但我想使用 import/exports 而不是 require...但现在 jest 抱怨我不能在模块之外使用 import 语句。我在这里查看了不同的解决方案,但我之前可能搞砸了一些东西,这就是为什么它不适合我。目前我有这些文件:

package.json:

 {
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "type": "module",
  "scripts": {
    "test": "jest",
    "start": "node src/index.ts",
    "dev": "nodemon src/index.ts",
    "build": "tsc"
  },
  "keywords": [],
  "author": "Gabor Adorjani <[email protected]>",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.21",
    "@types/jest": "^29.5.12",
    "@types/node": "^20.11.30",
    "bcrypt": "^5.1.1",
    "cors": "^2.8.5",
    "crypto": "^1.0.1",
    "express": "^4.19.1",
    "jest": "^29.7.0",
    "jsonwebtoken": "^9.0.2",
    "mongodb": "^6.5.0",
    "mongoose": "^8.2.3",
    "nodemon": "^3.1.0",
    "ts-jest": "^29.1.2",
    "typescript": "^5.4.3",
    "uuid": "^9.0.1"
  },
  "dependencies": {
    "@types/bcryptjs": "^2.4.6",
    "@types/supertest": "^6.0.2",
    "bcryptjs": "^2.4.3",
    "crypto-js": "^4.2.0",
    "dotenv": "^16.4.5",
    "express-rate-limit": "^7.2.0",
    "http": "^0.0.1-security",
    "http-proxy-middleware": "^2.0.6",
    "mongodb-memory-server": "^9.1.8",
    "supertest": "^6.3.4"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "sourceMap": true,
    "resolveJsonModule": true,
    "outDir": "dist",
    "esModuleInterop": true
  },
  "include": ["src/**/*"],
  "files": ["types.d.ts"],
  "ts-node": {
    "esm": true,
    "compiler": "typescript"
  }
}
testing jestjs esmodules
1个回答
0
投票

我最终找到了一个能够运行测试并通过测试的解决方案。我遇到了其他问题,因为

类型错误 [ERR_UNKNOWN_FILE_EXTENSION]

但这需要我下次再解决。

解决方案是使用 babel。

在我的 babelrc.cjs 文件中我得到了这些

module.exports = {
  presets: [
    ["@babel/preset-env", { targets: { node: "current" } }],
    "@babel/preset-react",
    "@babel/preset-typescript",
  ],
  plugins: [
    "@babel/plugin-transform-runtime",
    "@babel/proposal-class-properties",
    "@babel/plugin-transform-template-literals",
    "react-hot-loader/babel",
  ],
};
当前

ts.config:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "sourceMap": true,
    "resolveJsonModule": true,
    "outDir": "dist",
    "esModuleInterop": true,
  },
  "include": ["src/**/*"],
  "files": ["types.d.ts"],
  "ts-node": {
    "esm": true,
    "compiler": "typescript"
  }
}

和 pacakge.json

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "type": "module",
  "scripts": {
    "test": "jest",
    "start": "node src/index.ts",
    "dev": "nodemon src/index.ts",
    "build": "tsc"
  },
  
  "license": "ISC",
  "devDependencies": {
    "@babel/plugin-proposal-class-properties": "^7.18.6",
    "@babel/plugin-transform-runtime": "^7.24.3",
    "@babel/plugin-transform-template-literals": "^7.24.1",
    "@babel/preset-env": "^7.24.3",
    "@babel/preset-react": "^7.24.1",
    "@babel/preset-typescript": "^7.24.1",
    "@types/express": "^4.17.21",
    "@types/express-rate-limit": "^6.0.0",
    "@types/jest": "^29.5.12",
    "@types/node": "^20.11.30",
    "bcrypt": "^5.1.1",
    "cors": "^2.8.5",
    "crypto": "^1.0.1",
    "express": "^4.19.1",
    "jest": "^29.7.0",
    "jsonwebtoken": "^9.0.2",
    "mongodb": "^6.5.0",
    "mongoose": "^8.2.3",
    "nodemon": "^3.1.0",
    "react-hot-loader": "^4.13.1",
    "ts-jest": "^29.1.2",
    "typescript": "^5.4.3",
    "uuid": "^9.0.1"
  },
  "dependencies": {
    "@types/bcryptjs": "^2.4.6",
    "@types/supertest": "^6.0.2",
    "bcryptjs": "^2.4.3",
    "crypto-js": "^4.2.0",
    "dotenv": "^16.4.5",
    "express-rate-limit": "^7.2.0",
    "http": "^0.0.1-security",
    "http-proxy-middleware": "^2.0.6",
    "mongodb-memory-server": "^9.1.8",
    "supertest": "^6.3.4"
  }
}

希望这对某人有帮助

© www.soinside.com 2019 - 2024. All rights reserved.