Just a quick update to the article I wrote some time ago that could be considered as part one on this topic. The problem outlined in the article is basic. When developing Cypress tests, it is helpful to use the .only(), a Cypress modifier to exclude other tests to see results for the single test being developed for the quick iterations. But accidentally pushing it to the repository creates many unwanted problems for anyone involved.

The solution from that article I was using for some time is very basic, yet probably not too portable. It was working for me, but sadly, recently I did not have too much luck trying to make it work with the recently released Husky 7.0. I have switched to the npm package called stop-only since and cannot complain.

Using stop-only with Husky 7.0

Setup Husky automatically:

npx husky-init && npm install

The above does multiple changes to your git repository:

  • Installs Husky into dev dependencies.
  • Enables git hooks.
  • Adds a prepare script into package.json.
  • Bootstraps the .husky/ folder where hooks reside.

Remove the bootstrapped example pre-commit file:

rm .husky/pre-commit

Generate the pre-push Husky hook via command:

npx husky add .husky/pre-push "npx stop-only --folder cypress/integration"

This will create the .husky/pre-push hook file with the following contents:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx stop-only --folder cypress/integration

Don't forget to start tracking the file:

git add .husky/pre-push

Next, install stop-only and Cypress itself, both as dev dependencies:

npm install --save-dev stop-only cypress

And create an example test containing .only(), for instance in the base Cypress test path of cypress/integration/spec.js:

/// <reference types="cypress" />
describe("Simplest test should", () => {
  it.only("visit base URL", () => {
    cy.visit("/")
  })
})

Pushing to the remote repository is now prevented in an early and spectacular fashion:

$ git push
Found .only here 👎
cypress/integration/spec.js:3:  it.only("visit base URL", () => {
husky - pre-push hook exited with code 1 (error)

The full example is available in the repository. Happy testing!