vue.js - 将 Cypress 与 NuxtJS 一起使用,并在开始 e2e 测试之前等待服务器启动

标签 vue.js cypress e2e-testing package.json nuxtjs

我用 Cypress 建立了一个 NuxtJS 项目。 在我的测试中,我转到我的应用程序的页面之一,例如主页 (/)

describe('Index page', function () {
  it('should show index page of app', function () {
    cy.visit('/')
    cy.get('h1.title').contains('frontend')
  })
})

所以我需要启动我的开发服务器才能构建我的 Nuxt 应用程序 (Vue),然后启动我的 e2e 测试。

问题是当我启动它时(相当长,至少 15 秒),它没有给我控制权,进程保持事件状态所以我无法启动我的 yarn 命令来运行测试。

enter image description here

"scripts": {
    "dev": "nuxt-ts",
    "build": "nuxt-ts build",
    "start": "nuxt-ts start",
    "generate": "nuxt-ts generate",
    "lint:js": "eslint --ext .js,.vue --ignore-path .gitignore .",
    "lint:style": "stylelint **/*.{vue,css} --ignore-path .gitignore",
    "lint": "yarn lint:js && yarn lint:style",
    "test": "jest",
    "e2e": "cypress open",
    "e2e:slient": "yarn run dev & cypress run"
  },

因此,一旦服务器正确启动,我真的不知道如何启动我的测试。

谢谢。

最佳答案

因为 NUXT 在准备测试之前生成应用程序需要时间。

所以您不应该使用“yarn run dev & cypress run”

相反,您可以通过 start-server-and-test 来尝试,这是 cypress 文档推荐的: https://docs.cypress.io/guides/continuous-integration/introduction#Boot-your-server

"scripts": {
   "dev": "nuxt-ts",
   "build": "nuxt-ts build",
   "start": "nuxt-ts start",
   "generate": "nuxt-ts generate",
   "lint:js": "eslint --ext .js,.vue --ignore-path .gitignore .",
   "lint:style": "stylelint **/*.{vue,css} --ignore-path .gitignore",
   "lint": "yarn lint:js && yarn lint:style",
   "test": "jest",
   "cypress:open": "cypress open",
   "cypress:run": "cypress run",
   "e2e": "start-server-and-test dev http://localhost:3000 cypress:open",
   "pree2e:slient": "npm run build",
   "e2e:silent": "start-server-and-test start http://localhost:3000 cypress:run"
 },
 "devDependencies": {
    .
    .
    .
    "start-server-and-test": "^1.11.5",
  }

添加“pree2e:slient”脚本的原因是强制 NUXT 在以静默模式开始 cypress 测试之前构建应用程序(您可以在 CI 管道上运行此脚本)

关于vue.js - 将 Cypress 与 NuxtJS 一起使用,并在开始 e2e 测试之前等待服务器启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65017205/

相关文章:

css - 为什么我的 css 代码在 vuejs 中不起作用?

javascript - Vue 将数据表中的值相乘

javascript - 无法在 Gmail 中找到元素

protractor - 我如何知道 Protractor 中运行测试中使用的基本 url?

javascript - vue.js方法返回不断更新

javascript - Vue.js:Nuxt 错误处理

cypress - 如何在 Cypress 中使用 fakerjs

javascript - cy.readFile 和 cy.fixture 之间有什么区别?

cypress - 使用 Cypress 运行测试时,在 Auth0 中禁用验证码的正确方法是什么

e2e-testing - cy.getBy*** 不是函数