@forsakringskassan/cypress-config

中文文档
1.11.0发布于 2026/11/03MITTypeScriptES Module构建来源已验证近 30 天下载 2.4万

Sharable Cypress configuration building blocks for Försäkringskassan

原始使用文档
文档目录7 节
  1. Install
  2. Usage
  3. Commands
  4. prefersColorScheme
  5. Axe plugin
  6. docs-generator plugin
  7. HTML-Validate plugin

@forsakringskassan/cypress-config

Sharable Cypress configuration building blocks for Försäkringskassan.

Install

npm install --save-dev @forsakringskassan/cypress-config

If you previously had these packages installed, they can be uninstalled:

npm rm \
  @forsakringskassan/cypress-axe \
  cypress-html-validate \
  mocha-multi-reporters

Usage

In cypress.config.ts, replace the vanilla defineConfig() helper from cypress with @forsakringskassan/cypress-config and pass in import.meta.dirname as the first parameter:

-import { defineConfig } from "cypress";
+import { defineConfig } from "@forsakringskassan/cypress-config";

-export default defineConfig({
+export default defineConfig(import.meta.dirname, {
   /* ... */
 });

It works the same but is preconfigured with new defaults.

[!TIP] The defineConfig() helper does not require the configuration object as the second parameter. Unless you are explicitly overwriting the default configuration it is recommended to call defineConfig() without it.

export default defineConfig(import.meta.dirname);

This ensures maximum compatibility with future releases.

A configuration function is available for usage in cypress/support/e2e.ts and/or cypress/support/component.ts:

import { configure } from "@forsakringskassan/cypress-config/support";

await configure({
    resetEmulatedMedia: true,
    afterEach: {
        htmlvalidate: true,
    },
});

[!NOTE]
The configure() function is expected to be called exactly once. If the call is omitted some functionality will not work as expected. Calling it multiple times is undefined behaviour.

  • resetEmulatedMedia - When enabled, any emulated CSS media features will be reset between runs.
  • afterEach - Enables features to automatically run after each test. See the various components for details.

For component tests a preconfigured mount function is available. It adds the following Vue plugins from @fkui/vue:

Additionally, it also:

  • Treats Vue warnings as errors.

To use, edit cypress/support/component.ts:

-import { mount } from "cypress/mount";
-import { configure } from "@forsakringskassan/cypress-config/support";
+import { configure, mount } from "@forsakringskassan/cypress-config/support";

 Cypress.Commands.add("mount", mount);

If you need further customization, wrap it in your own custom function:

Cypress.Commands.add("mount", (component, options = {}) => {
    /* modify options as needed */
    return mount(component, options);
});

For advanced usage, use the createMount() call to create the mount() function manually:

import { createMount } from "@forsakringskassan/cypress-config/support";

const mount = await createMount({/* options */});

Cypress.Commands.add("mount", mount);

Commands

This preset adds new custom commands:

forcedColors

  • Syntax: cy.forcedColors(value)
  • Parameters:
    • value: "active" | "none" - "active"" enables forced colors, "none" disables it.

Emulate CSS media feature forced-colors for testing High Contrast mode.

Unless resetEmulatedMedia is enabled, this modifies browser settings that persist until the next cy.forcedColors() call. It is independent from cy.prefersColorScheme(), e.g. if you want to use forced colors with dark mode you need to call both functions.

[!TIP] Enable resetEmulatedMedia in configure() to automatically reset emulated media features, including forced color mode, between tests.

prefersColorScheme

  • Syntax: cy.prefersColorScheme(value)
  • Parameters
    • value: "light" | "dark" | "none" - Sets the preferred color scheme or none to disable it (use system default).

Emulate CSS media feature prefers-color-scheme for testing light/dark mode themes.

Unless resetEmulatedMedia is enabled, this modifies browser settings that persist until the next cy.prefersColorScheme() call. It is independent from cy.forcedColors(), e.g. if you want to use forced colors with dark mode you need to call both functions.

[!TIP] Enable resetEmulatedMedia in configure() to automatically reset emulated media features, including the color scheme, between tests.

Axe plugin

A preconfigured Axe plugin (@forsakringskassan/cypress-axe) is exported as axePlugin:

import { defineConfig, axePlugin } from "@forsakringskassan/cypress-config";

export default defineConfig(import.meta.dirname, {
    component: {
        async setupNodeEvents(on, config) {
            config = await axePlugin(on, config);
            return config;
        },
    },
});

Automatic validation after each test cannot currently be disabled.

docs-generator plugin

A plugin to get all pages and examples generated by @forsakringskassan/docs-generator. This is typically only useful in E2E testing.

import {
    defineConfig,
    docsGeneratorPlugin,
} from "@forsakringskassan/cypress-config";
/
const docs = new Generator(import.meta.url, {});
const sourceFiles = [];

export default defineConfig(import.meta.dirname, {
    e2e: {
        async setupNodeEvents(on, config) {
            config = await docsGeneratorPlugin(on, config, docs, sourceFiles);
            return config;
        },
    },
});

The pages are retrieved using Cypress.expose("pages") which contains additional information:

const pages = Cypress.expose("pages") ?? [];
for (const page of pages) {
    const { path, examples } = page;
    /* ... */
}

See manifest documentation for details about what each page entry contains.

HTML-Validate plugin

A preconfigured HTML-validate plugin (cypress-html-validate) is exported as htmlValidatePlugin:

import {
    defineConfig,
    htmlValidatePlugin,
} from "@forsakringskassan/cypress-config";

export default defineConfig(import.meta.dirname, {
    component: {
        async setupNodeEvents(on, config) {
            config = await htmlValidatePlugin(on, config);
            return config;
        },
    },
});

To automatically run validation after each test configure it using the configuration function:

import { configure } from "@forsakringskassan/cypress-config/support";

await configure({
    resetEmulatedMedia: true,
    afterEach: {
        htmlvalidate: true,
    },
});

版本与发布记录

@forsakringskassan/cypress-config 共发布 22 个版本,首次发布于 2026/09/02;过去 12 个月发布了 22 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.11.0latest2026/11/0312 天前
1.10.22026/10/2422 天前
1.10.12026/10/091 个月前
1.10.02026/09/181 个月前
1.9.22026/09/171 个月前
1.9.12026/09/152 个月前

关于 @forsakringskassan/cypress-config 的常见问题

@forsakringskassan/cypress-config 是什么?

Sharable Cypress configuration building blocks for Försäkringskassan

如何安装 @forsakringskassan/cypress-config?

在项目目录运行 npm install @forsakringskassan/cypress-config,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:cypress(^15.17.0 || ^16.0.0)、@fkui/vue(>= 6.0.0)、html-validate(>= 8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@forsakringskassan/cypress-config 支持 TypeScript 吗?

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

@forsakringskassan/cypress-config 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@forsakringskassan/cypress-config 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.16 || >= 24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@forsakringskassan/cypress-config 可以商用吗?

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

@forsakringskassan/cypress-config 的下载量有多少?

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

@forsakringskassan/cypress-config 还在维护吗?

最新版本 1.11.0 发布于 2026/11/03(12 天前);至今共发布 22 个版本,其中过去 12 个月发布了 22 个;首次发布于 2026/09/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。