typescript Gitlab CICD无法检测ts-node,尽管已安装

ny6fqffe  于 2023-06-07  发布在  TypeScript
关注(0)|答案(1)|浏览(209)

我对设置GitLab CI相对较新。我目前正在使用MERN管道(MongoDB,express,react,Nodejs)实现一个小型Web应用程序。我想创建一个非常小的项目,类似于谷歌驱动器,用户可以存储和路由他们的文件在线。以下是我的项目结构:
project structure
我只使用Jest和super test为后端配置了单元测试。我已经安装了所有必需的包,并设置了jest.config.ts来编译typescript测试。然而,我不知道为什么测试文件不能检测到我安装的jest,如果我只在后端文件夹中安装npm所需的包。因此,我在根文件夹和后端文件夹中安装了测试所需的所有软件包。以下是我的包裹:

cloud_todo/package.json:

{
  "dependencies": {
    "supertest": "^6.3.3"
  },
  "devDependencies": {
    "@babel/core": "^7.22.1",
    "@babel/preset-env": "^7.22.4",
    "@types/jest": "^29.5.2",
    "@types/supertest": "^2.0.12",
    "babel-jest": "^29.5.0",
    "jest": "^29.5.0",
    "ts-jest": "^29.1.0"
  }
}

cloud_todo/backend/package.json:

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "dist/server.js",
  "scripts": {
    "start": "nodemon src/server.ts",
    "lint": "eslint . --ext .ts",
    "test": "jest --testTimeout=10000 --forceExit"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.1.0",
    "body-parser": "^1.20.2",
    "connect-mongo": "^4.6.0",
    "cross-env": "^7.0.3",
    "dotenv": "^16.0.3",
    "envalid": "^7.3.1",
    "express": "^4.18.2",
    "express-session": "^1.17.3",
    "http-errors": "^2.0.0",
    "jest": "^29.5.0",
    "leaked-handles": "^5.2.0",
    "mongodb": "^5.5.0",
    "mongodb-memory-server": "^8.12.2",
    "mongoose": "^6.8.1",
    "morgan": "^1.10.0",
    "multer": "^1.4.5-lts.1",
    "multer-gridfs-storage": "^5.0.2",
    "supertest": "^6.3.3",
    "ts-dev": "^2.1.16",
    "ts-jest": "^29.1.0"
  },
  "devDependencies": {
    "@types/bcrypt": "^5.0.0",
    "@types/express": "^4.17.15",
    "@types/express-session": "^1.17.7",
    "@types/http-errors": "^2.0.1",
    "@types/jest": "^29.5.2",
    "@types/morgan": "^1.9.3",
    "@types/multer": "^1.4.7",
    "@types/multer-gridfs-storage": "^4.0.5",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.59.6",
    "@typescript-eslint/parser": "^5.59.6",
    "eslint": "^8.30.0",
    "eslint-plugin-react": "^7.32.2",
    "nodemon": "^2.0.20",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.4"
  }
}

cloud_todo/backend/jest.config.ts:

/*
 * For a detailed explanation regarding each configuration property and type check, visit:
 * https://jestjs.io/docs/configuration
 */

export default {

  coverageProvider: "v8",
  moduleFileExtensions: [
    "js",
    "mjs",
    "cjs",
    "jsx",
    "ts",
    "tsx",
    "json",
    "node"
  ],
  roots: [
    "<rootDir>"
  ],
  testMatch: [
    "**/__tests__/**/*.[jt]s?(x)",
    "**/?(*.)+(spec|test).[tj]s?(x)"
  ],
  transform: {"^.+\\.(ts|tsx)$": "ts-jest"},

};

在设置好所有这些之后,后端单元测试在本地工作得很好。所以我用下面的yml文件进一步设置了gitlab CI/CD管道

cloud_todo/.gitlab-ci.yml

image: node:latest

stages:
  - npm
  - test

npm:
  stage: npm
  script:
    - npm install
    - cd backend
    - npm install --legacy-peer-deps
    - npm i ts-node -D --legacy-peer-deps
    - cd ..
    - cd frontend
    - npm install --legacy-peer-deps
  cache:
    paths:
      - node_modules/
  artifacts:
    expire_in: 1 days
    when: on_success
    paths:
      - node_modules/

test:
  stage: test
  dependencies:
    - npm
  script:
    - cd backend
    - npm test

但我在我的管道中不断得到以下信息:

$ cd backend
$ npm test
> backend@1.0.0 test
> jest --testTimeout=10000 --forceExit
Error: Jest: Failed to parse the TypeScript config file /builds/BWN133/cloud_todo/backend/jest.config.ts
  Error: Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed
Error: Cannot find package 'ts-node' imported from /builds/BWN133/cloud_todo/node_modules/jest-config/build/readConfigFileAndSetRootDir.js
    at readConfigFileAndSetRootDir (/builds/BWN133/cloud_todo/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:116:13)
    at async readInitialOptions (/builds/BWN133/cloud_todo/node_modules/jest-config/build/index.js:400:13)
    at async readConfig (/builds/BWN133/cloud_todo/node_modules/jest-config/build/index.js:147:48)
    at async readConfigs (/builds/BWN133/cloud_todo/node_modules/jest-config/build/index.js:421:26)
    at async runCLI (/builds/BWN133/cloud_todo/node_modules/@jest/core/build/cli/index.js:152:59)
    at async Object.run (/builds/BWN133/cloud_todo/node_modules/jest-cli/build/run.js:124:37)
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1

有谁能给我一些提示吗?非常感谢!
我尝试在yml脚本中安装ts-node并更改不同的docker镜像,但这些都不起作用。
我也尝试了一些随机的东西在互联网上,但这些都没有帮助,我的项目。

nbysray5

nbysray51#

首先,使用工件或缓存在作业之间传递已安装的node_modules。我建议把这个藏起来。因此,您可以删除与npm作业相关的所有工件。
其次,您需要在两个作业中该高速缓存,或者在全局级别上定义缓存,以便将其应用于所有作业。
第三,据我所知,你有三个node_modules文件夹,你的测试可能需要所有的文件夹,具体的错误发生是因为你只在项目根目录中定义了node_modules的路径,而不是在后端文件夹中。
试试这个:

image: node:latest

cache:
    paths:
      - node_modules/
      - backend/node_modules
      - frontend/node_modules    

stages:
  - npm
  - test

npm:
  stage: npm
  script:
    - npm install
    - cd backend
    - npm install --legacy-peer-deps
    - npm i ts-node -D --legacy-peer-deps
    - cd ..
    - cd frontend
    - npm install --legacy-peer-deps

test:
  stage: test
  script:
    - cd backend
    - npm test

相关问题