Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

单元测试集成到Gitlab CI #57

Open
fayeah opened this issue Mar 16, 2022 · 0 comments
Open

单元测试集成到Gitlab CI #57

fayeah opened this issue Mar 16, 2022 · 0 comments

Comments

@fayeah
Copy link
Owner

fayeah commented Mar 16, 2022

一、技术栈

   vue3+jest 

二、结果:

  • 代码库的badge:

image

  • 测试报告

image

image

  • 每个job都生成相应的数据,也可以下载报告

image

三、文件配置:

  • .gitlab-ci.yml
stages:
  - pages


pages:
  stage: pages
  script:
    - npm install
    - npm run test
    - mkdir .public
    - cp -r coverage/* .public
    - rm -rf public
    - mv .public public
  artifacts:
    paths:
      - public
    when: always
    reports:
      junit:
        - junit.xml
      cobertura: coverage/cobertura-coverage.xml
  tags:
    - some-runner

注:

  1. stage名需为pages,否则无法生成page 链接。
  2. npm run test的脚本为: "test": "jest --config ./jest.config.js --collectCoverage --coverageDirectory=\"./coverage\" --ci --reporters=default --reporters=jest-junit --watchAll=false"
  • jest配置文件:
module.exports = {
  moduleFileExtensions: [
    'js',
    'ts',
    'json',
    'vue',
    'tsx'
  ],
  transform: {
    '^.+\\.ts$': 'ts-jest',
    '^.+\\.vue$': 'vue-jest',
    '^.+\\.tsx$': 'vue-jest',
    "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
  },
  modulePaths: ["src"],
  moduleDirectories: ["node_modules"],
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1'
  },
  collectCoverageFrom: [
    "src/**/*.{js,ts,tsx}",
    "!<rootDir>/node_modules/"
  ],
  coverageReporters: ["html", "text", "text-summary", "cobertura"],
}

四、Gitlab设置:

  • 测试报告的正则解析

image

  • badge配置:可以自己定义,例如%{default_branch} 可以改为名为"test"的分支名称,那么指定读取该分支下生成的报告或其他状态。
    测试覆盖率badge

image

  • pipeline状态badge

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant