@cypress/skip-test

中文文档
2.6.1发布于 2021/06/08MITTypeScriptCommonJS零依赖近 30 天下载 47.5万

Simple commands to skip a test based on platform, browser or an url

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

原始使用文档
文档目录15 节
  1. Important
  2. Install
  3. Example
  4. cy.skipOn
  5. cy.onlyOn
  6. imports
  7. imports with callback
  8. boolean flag
  9. isOn
  10. Headed
  11. ENVIRONMENT
  12. Notes
  13. Intellisense
  14. Warning
  15. Authors

@cypress/skip-test renovate-app badge semantic-release CircleCI

Simple commands to skip a test based on platform, browser or an url

it('skips this test when running on Mac', () => {
  cy.log('about to run custom command to skip this test')
    .wait(1000)
    .skipOn('mac')
})

Skip in action

Important

This is a simple utility plugin until Cypress supports filtering of tests.

Install

npm install -D @cypress/skip-test

Example

You can use this module as custom Cypress commands cy.skipOn and cy.onlyOn or by importing its functions. To use custom commands like cy.skipOn, add this module to your support file cypress/support/index.js

require('@cypress/skip-test/support')

cy.skipOn

Skip this test if running in Electron browser

it('only runs on Electron', () => {
  cy.skipOn('electron')
  // the rest of the test
})

Skip this test if running on Windows platform

it('runs on Linux and Mac', () => {
  cy.skipOn('windows')
  // the rest of the test
})

cy.onlyOn

Continues the test only when running on Mac, skips when running on any other platform

it('runs on Mac only', () => {
  cy.onlyOn('mac')
  // the rest of the test
})

Continues this test only when running against localhost. Use baseUrl to set the url to compare.

it('only tests localhost', () => {
  cy.onlyOn('localhost')
  // the rest of the test
})

imports

import { onlyOn, skipOn } from '@cypress/skip-test'

it('runs only on Mac', () => {
  // using the exported function instead of
  // the custom command cy.onlyOn(...)
  onlyOn('mac')
})

it('skips on Mac', () => {
  skipOn('darwin')
})

imports with callback

Instead of dynamically skipping a test at run-time, you can hide entire blocks of tests using the callback format.

import { onlyOn, skipOn } from '@cypress/skip-test'
onlyOn('mac', () => {
  // this callback will only evaluate on Mac
  // thus the tests will be completely hidden from other platforms
  describe('Mac tests', () => {
    it('works', () => {})
  })
})
skipOn('mac', () => {
  // this test will run on every platform but Mac
  it('hides this test on Mac', () => {})
})

Tip: you can nest the callbacks to combine the conditions

// run these group of tests only on Mac and only on Chrome
onlyOn('mac', () => {
  onlyOn('chrome', () => {
    it('works', () => {})
  })
})

When skipping a block of tests or a single test using browser name, it will insert a dummy empty test to let you know what has happened

skipOn('firefox', () => {
  it('works', () => {...})
  it('works too', () => {...})
})
// output in the console
//  Skipping test(s) on firefox

boolean flag

You can pass a boolean to each function or command if you want to calculate when to run the tests yourself.

// run this test if S is "foo"
cy.onlyOn(S === 'foo')

You can use callback form with the flag

onlyOn(S === 'foo', () => {
  describe('foo', () => {
    it('works', () => {...})
  })
})

You can even run other Cypress commands before deciding to skip or continue

it('runs if task returns production', () => {
  cy.task('getDbName').then((name) => cy.onlyOn(name === 'production'))
  // equivalent
  cy.task('getDbName').then((name) => onlyOn(name === 'production'))
  // equivalent
  cy.task('getDbName')
    .then((name) => name === 'production')
    .then(onlyOn)
})

isOn

You can check the condition against a browser name or an environment yourself.

import { isOn } from '@cypress/skip-test'
it('loads users', () => {
  // when running on Windows locally, the backend is not running
  // thus we need to stub XHR requests
  if (isOn('windows') && isOn('localhost')) {
    cy.server()
    cy.route('/users', 'fixture:users')
  }
  cy.visit('/')
  cy.get('.user').should('have.length', 10)
})

Headed

You can skip or run tests in headed / headless environments

import { skipOn, onlyOn } from '@cypress/skip-test'

skipOn('headed', () => {
  it('skips the current test in headed mode', () => {
    cy.wrap(true).should('equal', true)
  })
})

onlyOn('headless', () => {
  it('runs only in headless mode', () => { ... })
})

Note: when skipping tests in this case, it will insert an empty placeholder test to provide information why the tests were skipped.

- Skipping test(s), not on headed

ENVIRONMENT

This module also reads special environment variable ENVIRONMENT inside its checks. For example, to only stub network calls on staging environment, execute the tests like this:

CYPRESS_ENVIRONMENT=staging npx cypress run

Inside the spec file you can write

import {onlyOn, skipOn} from '@cypress/skip-test'
const stubServer = () => {
  cy.server()
  cy.route('/api/me', 'fx:me.json')
  cy.route('/api/permissions', 'fx:permissions.json')
  // Lots of fixtures ...
}
it('works', () => {
  onlyOn('staging', stubServer)
  ...
})
skipOn('staging', () => {
  it('works on non-staging', () => {...})
})

The test works will stub network calls when running on staging, but will skip calling stubServer for other environments. The test works on non-staging will be skipped when the environment is staging.

Notes

You can chain conditions together

it('combination of skip and only', () => {
  cy.skipOn('firefox')
  cy.onlyOn('electron').onlyOn('mac')
  cy.log('running test')
})

If the test runs, it will print the conditions in the command log

Skip and only

Intellisense

To get typings, reference this module, for example by using reference comment

/// <reference types="@cypress/skip-test" />

Skip intellisense

For more details read Cypress Intelligent Completion Guide

Warning

Skipping tests in Mocha at run-time skips the afterEach hooks. In this example, afterEach will be skipped, and after hook will run.

it('example', () => {
  cy.skipOn('mac')
})
afterEach(() => {
  // this will be skipped when a test is skipped
})
after(() => {
  // this will run even after skipping test
})

Authors

  • Kevin Old
  • Gleb Bahmutov

MIT License

版本与发布记录

@cypress/skip-test 共发布 14 个版本,首次发布于 2019/11/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.6.1latest2021/06/085 年前
2.6.02021/01/135 年前
2.5.12020/11/305 年前
2.5.02020/02/056 年前
2.4.12020/02/046 年前
2.4.02020/02/046 年前

关于 @cypress/skip-test 的常见问题

@cypress/skip-test 是什么?

Simple commands to skip a test based on platform, browser or an url注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 @cypress/skip-test?

在项目目录运行 npm install @cypress/skip-test,或使用 pnpm add / yarn add / bun add。

@cypress/skip-test 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cypress/skip-test 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cypress/skip-test 可以商用吗?

@cypress/skip-test 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cypress/skip-test 的下载量有多少?

根据 npm 官方下载统计,@cypress/skip-test 在 2026/10/11 – 2026/11/09 这 30 天内被下载 475,097 次,日均约 15,837 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cypress/skip-test 还在维护吗?

最新版本 2.6.1 发布于 2021/06/08(5 年前);至今共发布 14 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/11/07。作者已将其标记为弃用,新项目建议寻找替代方案。