Existuje novejšia metóda, ktorú teraz používam, opísaná v časti 2.

Tu je spôsob, ktorý používam na zabránenie pushovania zmien, keď sú niektoré Cypress testy preskočené.

  • Nainštalujte potrebné balíky
npm i -D cypress husky start-server-and-test
  • Definujte skript dev v package.json, ak to vaše lešenie ešte neurobilo
...
"scripts": {
+  "dev": "<start your server>"
}

Otestujte spustením a uistite sa, že server naštartoval

npm run dev
  • Vytvorte súbor cypress.json, upravte port podľa potreby
{
  "baseUrl": "http://localhost:3000/"
}
  • Bežný skript na spustenie Cypress
...
"scripts": {
  "dev": "<start your server>",
+ "cy:run": "cypress run"
}

Spustenie Cypressu prvýkrát ho inicializuje

$ npm run cy:run
Can't run because no spec files were found.
We searched for any files inside of this folder:
/project-path/cypress/integration
...
"scripts": {
  "dev": "<start your server>",
  "cy:run": "cypress run",
+ "test": "start-server-and-test dev http://localhost:3000 cy:run"
}

start-test je kratší alias a stačí zadať port

...
"scripts": {
  "dev": "<start your server>",
  "cy:run": "cypress run",
- "test": "start-server-and-test dev http://localhost:3000 cy:run"
+ "test": "start-test dev 3000 cy:run"
}
  • Vytvorte najjednoduchší test v cypress/integration/spec.js
/// <reference types="cypress" />
describe("Simplest test should", () => {
  it("visit base URL", () => {
    cy.visit("/")
  })
})

Zatiaľ dobre, len rýchla kontrola

npm run test
"scripts": {
  "dev": "<start your server>",
  "cy:run": "cypress run",
  "test": "start-test dev 3000 cy:run"
},
...
+"husky": {
+  "hooks": {
+    "pre-push": "npm run test"
+  }
}
/// <reference types="cypress" />
describe("Simplest test should", () => {
  it.only("visit base URL", () => {
    cy.visit("/")
  })
})
...
"scripts": {
  "dev": "<start your server>",
  "cy:run": "cypress run",
  "test": "start-test dev 3000 cy:run"
},
"husky": {
  "hooks": {
-   "pre-push": "npm run test"
+   "pre-push": "grep -Rvzq -e '.skip' -e '.only' cypress/integration && npm run test"
  }
}

Hotovo!

Zdrojové kódy sú dostupné v repozitári

Odkazy #