# @cypress/skip-test 中文文档

规范地址：https://npmcn.com/package/%40cypress/skip-test/

版本：2\.6\.1
npm 元数据更新时间：2025-10-27T19:17:49.852Z
中文译文更新时间：2026-11-11T07:20:30.808Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cypress/skip-test
原文对照：https://npmcn.com/package/%40cypress/skip-test/?lang=original
项目主页：https://github.com/cypress-io/cypress-skip-test#readme
源码仓库：https://github.com/cypress-io/cypress-skip-test

弃用说明：Package no longer supported\. Contact Support at https://www\.npmjs\.com/support for more info\.

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：17 kB，6 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2019-11-07T15:11:08.573Z
- 最新版本发布：2021-06-08T11:40:29.536Z
- 近 30 天下载量：475097（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：14（近 12 个月 0 个）

## 中文简介

根据平台、浏览器或 URL 跳过测试的简单命令

## 安装

```sh
npm install @cypress/skip-test
```

也可以使用：`pnpm add @cypress/skip-test`、`yarn add @cypress/skip-test`、`bun add @cypress/skip-test`

## README

# @ cypress/ skip - test [![renovate-app badge](https://img.shields.io/badge/renovate-app-blue.svg)](https://renovateapp.com/) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg) ](https://github.com/semantic-release/semantic-release)[![CircleCI](https://circleci.com/gh/cypress-io/cypress-skip-test/tree/master.svg?style=svg)](https://circleci.com/gh/cypress-io/cypress-skip-test/tree/master)

> 根据平台、浏览器或 URL 跳过测试的简单命令

```js
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](https://npmcn.com/package/%40cypress/skip-test/images/skip.gif)

## 重要

这是一个简单的工具插件, 直到 [Cypress 支持测试过滤](https://github.com/cypress-io/cypress/pull/5346)。 。 。 .

## 安装

```shell
npm install -D @cypress/skip-test
```

## 示例

您可以使用此模块作为自定义 Cypress 命令 `cy.skipOn` 财务报告和审定财务报表 `cy.onlyOn` 或通过导入其功能。 使用自定义命令 `cy.skipOn`,将此模块添加到您的支持文件中 `cypress/support/index.js`

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

### `cy.skipOn`

在 Electron 浏览器中运行时跳过此测试

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

在 Windows 平台运行时跳过此测试

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

### `cy.onlyOn`

仅在Mac上运行时继续测试, 在其它平台上运行时跳过

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

仅在对决时继续此测试 `localhost`启用 `baseUrl` 设置url来比较.

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

### 进口

```js
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')
})
```

### 带有回调的导入

与其在运行时动态跳过测试,不如使用回调格式来隐藏整个测试块.

```js
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', () => {})
})
```

**提示 :** 可以嵌入回调 结合条件

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

当跳过一组测试或使用浏览器名的单一测试时,它会插入一个假人空测试,让您知道发生了什么

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

### 布尔旗

如果您想要计算何时进行测试, 您可以向每个函数或命令传递布尔 .

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

您可以使用旗下回调格式

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

您甚至可以在决定跳过或继续前运行其他 Cypress 命令

```js
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`

您可以对照浏览器名称或环境来检查条件 .

```js
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)
})
```

### 标题

您可以在标题/ 无头环境中跳过或运行测试

```js
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', () => { ... })
})
```

**说明:** 在此情况下跳过测试时, 它会插入一个空占位符测试, 以提供测试为何跳过的信息 .

```text
- Skipping test(s), not on headed
```

### `ENVIRONMENT`

此模块还读取特殊环境变量 `ENVIRONMENT` 在它的检查。 例如,只有树枝网络呼叫 `staging` 环境,执行这样的测试 :

```shell
CYPRESS_ENVIRONMENT=staging npx cypress run
```

您可以在 Spec 文件内写入

```js
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', () => {...})
})
```

测试 `works` 运行时会调用分站网络 `staging`但是会跳过呼叫 `stubServer` 其他环境。 测试 `works on non-staging` 当环境出现时会跳过 `staging`。 。 。 .

### 页:1

你可以把条件连在一起

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

如果测试运行, 它将打印命令日志中的条件

![Skip and only](https://npmcn.com/package/%40cypress/skip-test/images/skip-and-only.png)

## 智能

要获得打字,请参考此模块,例如使用 `reference` 注释

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

![Skip intellisense](https://npmcn.com/package/%40cypress/skip-test/images/skip.png)

将更多细节改为 [Cypress 智能完成指南](https://on.cypress.io/intellisense)

## 3⁄4 ̄ ̧漯B

运行时跳过 Mocha 测试 `afterEach` 钩子. 在这个例子中, `afterEach` 将被跳过 `after` 钩会跑.

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

## 作者数

-   老凯文
-   格列布·巴赫穆托夫

[MIT 许可证](https://npmcn.com/package/%40cypress/skip-test/LICENSE)

## 常见问题

### @cypress/skip-test 是什么？

根据平台、浏览器或 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。作者已将其标记为弃用，新项目建议寻找替代方案。

