<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ cypress - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ freeCodeCamp 是一个免费学习编程的开发者社区，涵盖 Python、HTML、CSS、React、Vue、BootStrap、JSON 教程等，还有活跃的技术论坛和丰富的社区活动，在你学习编程和找工作时为你提供建议和帮助。 ]]>
        </description>
        <link>https://www.freecodecamp.org/chinese/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ cypress - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/chinese/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 04 Jun 2026 20:19:26 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/chinese/news/tag/cypress/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ 使用 Cypress 创建测试镜像并完成 E2E 测试 ]]>
                </title>
                <description>
                    <![CDATA[ 缘由 最近在做一个 Buildkite 的 Dashboard 的项目 Powerboard [https://github.com/guzhongren/Powerboard]，项目是托管在 GitHub 的 Git Pages 上的; 项目只是一个纯前端项目，且 E2E 测试是用 Cypress [https://www.cypress.io/]构建的；如果要进行 E2E 测试一般情况都是对着部署在 Git Pages 上的网站直接测试，而且也是这么做的😄。 痛点 测试滞后 这么做肯定是有问题的，产品都上线了才做测试，肯定已经迟了；如果程序有问题，那么就会影响所有用户。这种情况应该算是 P1 级别的产品事故，对用户来说简直就是灾难。应该在部署之前就应该完成 E2E 测试，如果测试通过不了，就不应该部署代码。所以测试应该前移。 解决方案 由于我们的测试需要自动化，需要在 Pipeline 上执行，所以必须是一个可以独立运行的程序和 Cypress 程序同时运行，并最终返回测试结果，由 Pipeline 来决定是否终止 Pipeline 运行。 在 GitHub Actions ]]>
                </description>
                <link>https://www.freecodecamp.org/chinese/news/do-e2e-test-with-cypress-image/</link>
                <guid isPermaLink="false">61e413486161280665ed8273</guid>
                
                    <category>
                        <![CDATA[ cypress ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Docker ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ 谷中仁 ]]>
                </dc:creator>
                <pubDate>Mon, 17 Jan 2022 07:00:00 +0000</pubDate>
                <media:content url="https://chinese.freecodecamp.org/news/content/images/2022/01/pexels-photo-5667741-1.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <h2 id="-">缘由</h2><p>最近在做一个 Buildkite 的 Dashboard 的项目 <a href="https://github.com/guzhongren/Powerboard" rel="noopener noreffer">Powerboard</a>，项目是托管在 GitHub 的 Git Pages 上的; 项目只是一个纯前端项目，且 E2E 测试是用 <a href="https://www.cypress.io/" rel="noopener noreffer">Cypress</a>构建的；如果要进行 E2E 测试一般情况都是对着部署在 Git Pages 上的网站直接测试，而且也是这么做的😄。</p><h2 id="--1">痛点</h2><h3 id="--2">测试滞后</h3><p>这么做肯定是有问题的，产品都上线了才做测试，肯定已经迟了；如果程序有问题，那么就会影响所有用户。这种情况应该算是 P1 级别的产品事故，对用户来说简直就是灾难。应该在部署之前就应该完成 E2E 测试，如果测试通过不了，就不应该部署代码。所以测试应该前移。</p><h2 id="--3">解决方案</h2><p>由于我们的测试需要自动化，需要在 Pipeline 上执行，所以必须是一个可以独立运行的程序和 Cypress 程序同时运行，并最终返回测试结果，由 Pipeline 来决定是否终止 Pipeline 运行。</p><p>在 GitHub Actions 的 Pipeline 上同时运行程序只能依靠 <code>docker-compose</code>, 在这我们可以使用 Cypress 官方出品的 <a href="https://hub.docker.com/r/cypress/included" rel="noopener noreffer">cypress/included</a>, 通过编排程序来进行测试。</p><h3 id="cypress-included">cypress/included</h3><p>cypress/included 可以让我们挂载 cypress 的测试脚本，然后自动执行，并在最终返回 Linux 命令状态值，如 0 ， 非 0 值。</p><h3 id="docker-compose">Docker-compose</h3><p><a href="https://docs.docker.com/compose/" rel="noopener noreffer">Docker-compose</a> 是一套容器编排工具，可以很轻松的管理容器的启动顺序等。在本地项目搭建中非常有用，比如构建数据库，执行 shell/yaml lint 等。</p><h2 id="--4">执行方案</h2><h3 id="--5">构建应用镜像</h3><p>在测试之前需要将应用构建好并部署好，我们可以用 Node 镜像打包应用，并利用容器的多阶段构建(<a href="https://docs.docker.com/develop/develop-images/multistage-build/" rel="noopener noreffer">multi-stage builds</a>) 完成应用轻量化构建，并部署在 <a href="https://hub.docker.com/_/nginx" rel="noopener noreffer">Nginx</a> 中。</p><pre><code class="language-yaml">FROM node:17-alpine as distPackage
COPY ./ /app
WORKDIR /app
RUN yarn
RUN yarn build

FROM nginx:latest
COPY --from=distPackage /app/dist /usr/share/nginx/html
</code></pre><h3 id="-service">编排 service</h3><p>因为我们的程序需要在测试的时候就要部署好，所以我们可以利用 Docker-compose 的 <a href="https://docs.docker.com/compose/compose-file/compose-file-v3/#build" rel="noopener noreffer">build</a> 参数，在容器启动时构建应用并部署。并在 cypress/included 启动是执行测试命令 <code>npx cy:docker</code>, 具体就是<code>cross-env ENV=docker cypress run --spec 'cypress/integration/dashboard.spec.js</code>。</p><pre><code class="language-yaml">version: '3'
services:
  web:
    build:
      context: ./
      dockerfile: ./Dockerfile
    container_name: web
    restart: always
    ports:
      - '80:80'

  e2e:
    image: cypress/included:9.2.1
    container_name: cypress
    depends_on:
      - web
    environment:
      - CYPRESS_baseUrl=http://web
      - ENV=docker
    command: npx cy:docker
    working_dir: /e2e
    volumes:
      - ./:/e2e

</code></pre><p>这样我们就可以独立的运行起真实程序和正式的测试程序了，具体的 Pipeline 可以参考 Powerboard 的 <a href="https://github.com/guzhongren/Powerboard/blob/main/.github/workflows/main.yml" rel="noopener noreffer">Workflow</a>。</p><pre><code class="language-yml">      - name: E2E
        run: |
          docker-compose up --build e2e

</code></pre><h2 id="--6">总结</h2><p><code>Docker-compopse</code> 有很好的应用编排能力，可以很轻松的构建多服务程序；并在构建应用的时候可以使用多阶段构建来优化镜像大小。使用 <code>Cypress</code> 可以提高开发效率并可在 <code>Pipeline</code> 上保证程序的正确性。</p><h2 id="refs">Refs</h2><ul><li><a href="https://guzhongren.github.io/" rel="noopener noreffer">博客:https://guzhongren.github.io/</a></li><li><a href="https://www.cypress.io/" rel="noopener noreffer">Cypress: https://www.cypress.io/</a></li><li><a href="https://hub.docker.com/r/cypress/included" rel="noopener noreffer">cypress/included: https://hub.docker.com/r/cypress/included</a></li><li><a href="https://docs.github.com/en/actions" rel="noopener noreffer">GitHub Actions: https://docs.github.com/en/actions</a></li><li><a href="https://github.com/guzhongren/Powerboard" rel="noopener noreffer">Powerboard: https://github.com/guzhongren/Powerboard</a></li></ul><p>欢迎阅读我的<a href=" https://guzhongren.github.io/2022/01/%E4%BD%BF%E7%94%A8cypress%E5%88%9B%E5%BB%BA%E6%B5%8B%E8%AF%95%E9%95%9C%E5%83%8F%E5%B9%B6%E5%AE%8C%E6%88%90e2e%E6%B5%8B%E8%AF%95/">更多文章</a>。</p><!--kg-card-begin: markdown--><p><img src="https://cdn.jsdelivr.net/gh/guzhongren/data-hosting@master/20210819/%E6%89%AB%E7%A0%81_%E6%90%9C%E7%B4%A2%E8%81%94%E5%90%88%E4%BC%A0%E6%92%AD%E6%A0%B7%E5%BC%8F-%E7%99%BD%E8%89%B2%E7%89%88.ae9zxgscqcg.png" alt="谷哥说-微信公众号" width="600" height="400" loading="lazy"></p>
<!--kg-card-end: markdown--><p></p> ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
