Devops CICD运行ReactJS应用程序,同时使用jest+putpeer进行测试



场景

我正在努力让我的端到端测试在GitLab管道中工作。目前所有的测试都在本地运行,但我在使用GitLab的持续集成功能运行它们时遇到了问题。

在本地运行测试时,puppeteer将使用无头版本的chrome进行端到端测试,但ReactJS的CRA服务器必须运行才能运行。为了让CRA服务器在本地运行,我在一个终端中使用npm start,一旦它完全运行,我就可以在另一个终端上运行npm test。在测试过程中,Puppeter使用无头Chromium在现场运行e2e测试。

问题

npm start启动了我的CRA服务器,但这个过程不会停止,因为除非我手动退出,否则我不知道如何停止它。这会挂起docker并阻止我开始测试。

问题

如何使用npm start启动应用程序,并在启动后运行npm run test:e2e?最终,一旦测试完成,我想退出CRA。

当前管道yml

image: node:10.19.0
stages:
- test
- build
- deploy
cache:
# untracked: true
key:
files:
- package-lock.json
paths:
- node_modules
#test_async:
#  script:
#    - npm install
#    - node ./specs/start.js ./specs/async.spec.js
before_script:
- apt-get update && apt-get install -y lftp gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
- if [ ! -d "node_modules" ]; then npm install; fi
test:
stage: test
script:
- npm start
- npm run test:e2e
build:
stage: build
script:
- npm run build
# deploy-to-staging:
#   stage: deploy
#   script:
#     - lftp -e "open ftp.mediajackagency.com; user $MJA_FTP_USER $MJA_FTP_PASS; mirror --reverse --verbose build/ /var/www/domains/projects/my-project/build/; bye"
#   environment:
#     name: staging
#     url: http://dev.mediajackagency.com/projects/my-project/build
#   when: manual
#   only:
#     - dev

我切换到使用生产构建,通过使用本地Web服务器进行测试。我利用了[jest-puppeteer][1]库,它允许您启动带有测试的Web服务器。

小丑木偶师.config.js

/**
* find all flags at the following site @jkr
* https://jestjs.io/docs/en/cli
*/
module.exports = {
launch: {
devtools: true, // allows for use of 'debugger;' in tests
// executablePath: '/usr/bin/chromium-browser',
headless: true,
defaultViewport: {
width: 1024,
height: 768,
//isMobile: true,
//hasTouch: true,
},
ignoreDefaultArgs: ['--disable-extensions'],
args: [
'--enable-font-antialiasing',
'--font-render-hinting=medium',
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox', // GOOD
'--no-zygote',
'--single-process', // GOOD
"--renderer",
"--no-service-autorun",
"--no-experiments",
"--no-default-browser-check",
"--disable-extensions",
]
},
server: { // launches webserver just for tests @jkr
// command: 'node server.js',
command: 'npm run test:webserver'
// port: 4444,
},
browser: 'chromium',
browserContext: 'default'
};

.gitlab ci.yml

image: node:10.19.0 # https://hub.docker.com/_/node/
# image: node:latest
cache:
# untracked: true
key: my-project-name
# key: ${CI_COMMIT_REF_SLUG} # per branch
# key:
#   files:
#     - package-lock.json # only update cache when this file changes (not working) @jkr
paths:
- .npm/
- node_modules
- build
stages:
- prepare # can install ci-deps here ? @jkr
- test # uses test:build specifically @jkr
- build
- deploy
# before_install:
before_script:
- npm ci --cache .npm --prefer-offline #
#   # - apt-get update && apt-get install -y lftp gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
#   - if [ ! -d "node_modules" ]; then set NODE_ENV=dev && npm install; fi # make sure dev dependencies are installed todo: 'if' needs to be verified working @jkr
#   # - npm install
#   # - if [ ! -d "build" ]; then npm run build; fi
prepare: # could this job be in a before_script for test ? @jkr
stage: prepare
script:
- npm install
- npm run build:test
# cache:
#   paths:
#     - node_modules
#   policy: push
test:
stage: test
script:
- npm run test:ci-deps
- npm run test:e2e # runs puppeteer tests @jkr
build:
stage: build
script:
- npm run build
# artifacts:
#   paths:
#     - build
deploy-ftp:
stage: deploy
script:
- lftp -e "open ftp.my.domain.com; user $MJA_FTP_USER $MJA_FTP_PASS; mirror --reverse --verbose build/ /var/www/domains/dev/projects/my-project/build/; bye"
environment:
name: staging
url: http://my.domain.com/my-project/build
when: manual
only:
- dev

最新更新