@forsakringskassan/cypress-config 1.5.1 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -15,9 +15,10 @@ npm rm mocha-multi-reporter
15
15
  ```
16
16
 
17
17
  - [Usage](#usage)
18
- - [Axe](#axe)
19
- - [docs-generator](#docs-generator)
20
- - [HTML-Validate](#html-validate)
18
+ - [Commands](#commands)
19
+ - [Axe plugin](#axe-plugin)
20
+ - [docs-generator plugin](#docs-generator-plugin)
21
+ - [HTML-Validate plugin](#html-validate-plugin)
21
22
 
22
23
  ## Usage
23
24
 
@@ -51,6 +52,7 @@ A configuration function is available for usage in `cypress/support/e2e.ts` and/
51
52
  import { configure } from "@forsakringskassan/cypress-config/support";
52
53
 
53
54
  configure({
55
+ resetEmulatedMedia: true,
54
56
  afterEach: {
55
57
  htmlvalidate: true,
56
58
  },
@@ -62,9 +64,42 @@ configure({
62
64
  > If the call is omitted some functionality will not work as expected.
63
65
  > Calling it multiple times is undefined behaviour.
64
66
 
65
- See the various components for details.
67
+ - `resetEmulatedMedia` - When enabled, any emulated CSS media features will be reset between runs.
68
+ - `afterEach` - Enables features to automatically run after each test. See the various components for details.
66
69
 
67
- ### Axe
70
+ ### Commands
71
+
72
+ This preset adds new custom commands:
73
+
74
+ #### forcedColors
75
+
76
+ - Syntax: `cy.forcedColors(value)`
77
+ - Parameters:
78
+ - `value: "active" | "none"` - `"active"`" enables forced colors, `"none"` disables it.
79
+
80
+ Emulate CSS media feature [`forced-colors`](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@media/forced-colors) for testing High Contrast mode.
81
+
82
+ Unless `resetEmulatedMedia` is enabled, this modifies browser settings that persist until the next `cy.forcedColors()` call.
83
+ It is independent from `cy.prefersColorScheme()`, e.g. if you want to use forced colors with dark mode you need to call both functions.
84
+
85
+ > [!TIP]
86
+ > Enable `resetEmulatedMedia` in `configure()` to automatically reset emulated media features, including forced color mode, between tests.
87
+
88
+ ### prefersColorScheme
89
+
90
+ - Syntax: `cy.prefersColorScheme(value)`
91
+ - Parameters
92
+ - `value: "light" | "dark" | "none"` - Sets the preferred color scheme or `none` to disable it (use system default).
93
+
94
+ Emulate CSS media feature [`prefers-color-scheme`](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@media/prefers-color-scheme) for testing light/dark mode themes.
95
+
96
+ Unless `resetEmulatedMedia` is enabled, this modifies browser settings that persist until the next `cy.prefersColorScheme()` call.
97
+ It is independent from `cy.forcedColors()`, e.g. if you want to use forced colors with dark mode you need to call both functions.
98
+
99
+ > [!TIP]
100
+ > Enable `resetEmulatedMedia` in `configure()` to automatically reset emulated media features, including the color scheme, between tests.
101
+
102
+ ### Axe plugin
68
103
 
69
104
  A preconfigured [Axe](https://github.com/dequelabs/axe-core) plugin ([@forsakringskassan/cypress-axe](https://github.com/Forsakringskassan/cypress-axe/)) is exported as `axePlugin`:
70
105
 
@@ -83,7 +118,7 @@ export default defineConfig(import.meta.dirname, {
83
118
 
84
119
  Automatic validation after each test cannot currently be disabled.
85
120
 
86
- ### docs-generator
121
+ ### docs-generator plugin
87
122
 
88
123
  A plugin to get all pages and examples generated by `@forsakringskassan/docs-generator`.
89
124
  This is typically only useful in E2E testing.
@@ -119,7 +154,7 @@ for (const page of pages) {
119
154
 
120
155
  See [manifest documentation](https://forsakringskassan.github.io/docs-generator/latest/processors/manifest.html) for details about what each page entry contains.
121
156
 
122
- ### HTML-Validate
157
+ ### HTML-Validate plugin
123
158
 
124
159
  A preconfigured [HTML-validate](https://html-validate.org/) plugin ([cypress-html-validate](https://gitlab.com/html-validate/cypress-html-validate/)) is exported as `htmlValidatePlugin`:
125
160
 
@@ -145,6 +180,7 @@ To automatically run validation after each test configure it using the configura
145
180
  import { configure } from "@forsakringskassan/cypress-config/support";
146
181
 
147
182
  configure({
183
+ resetEmulatedMedia: true,
148
184
  afterEach: {
149
185
  htmlvalidate: true,
150
186
  },
package/dist/support.d.ts CHANGED
@@ -8,9 +8,50 @@ import "cypress-html-validate/commands";
8
8
  * @since v1.1.0
9
9
  */
10
10
  export declare function configure(options: {
11
+ /** When enabled, any emulated CSS media features will be reset between runs */
12
+ resetEmulatedMedia: boolean;
11
13
  afterEach: {
12
14
  htmlvalidate: boolean;
13
15
  };
14
16
  }): void;
15
17
 
16
18
  export { }
19
+
20
+
21
+ declare global {
22
+ namespace Cypress {
23
+ interface Chainable {
24
+ /**
25
+ * Emulate CSS media feature `forced-colors`.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * cy.forcedColors("active");
30
+ * ```
31
+ *
32
+ * @since v1.6.0
33
+ * @param mode - "active" to enable, "none" to disable.
34
+ */
35
+ forcedColors(mode: "none" | "active"): Cypress.Chainable<void>;
36
+ }
37
+ }
38
+ }
39
+
40
+ declare global {
41
+ namespace Cypress {
42
+ interface Chainable {
43
+ /**
44
+ * Emulate CSS media feature `prefers-color-scheme`.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * cy.prefersColorScheme("dark");
49
+ * ```
50
+ *
51
+ * @since v1.6.0
52
+ * @param value - "light" or "dark" color mode, "none" will disable.
53
+ */
54
+ prefersColorScheme(value: "none" | "dark" | "light"): Cypress.Chainable<void>;
55
+ }
56
+ }
57
+ }
package/dist/support.mjs CHANGED
@@ -4,13 +4,95 @@ import "@forsakringskassan/cypress-axe/support";
4
4
  // src/support/html-validate.ts
5
5
  import "cypress-html-validate/commands";
6
6
 
7
+ // src/utils/use-emulated-media.ts
8
+ var state = {
9
+ prefersColorScheme: "none",
10
+ forcedColors: "none"
11
+ };
12
+ function* getFeatures() {
13
+ if (state.prefersColorScheme !== "none") {
14
+ yield {
15
+ name: "prefers-color-scheme",
16
+ value: state.prefersColorScheme
17
+ };
18
+ }
19
+ if (state.forcedColors !== "none") {
20
+ yield {
21
+ name: "forced-colors",
22
+ value: state.forcedColors
23
+ };
24
+ }
25
+ }
26
+ function useEmulatedMedia() {
27
+ return {
28
+ forcedColors(value) {
29
+ state.forcedColors = value;
30
+ },
31
+ prefersColorScheme(value) {
32
+ state.prefersColorScheme = value;
33
+ },
34
+ cdp() {
35
+ const features = Array.from(getFeatures());
36
+ return {
37
+ command: "Emulation.setEmulatedMedia",
38
+ params: {
39
+ media: "screen",
40
+ features
41
+ }
42
+ };
43
+ },
44
+ isEnabled() {
45
+ return state.prefersColorScheme !== "none" || state.forcedColors !== "none";
46
+ },
47
+ reset() {
48
+ state.prefersColorScheme = "none";
49
+ state.forcedColors = "none";
50
+ }
51
+ };
52
+ }
53
+
54
+ // src/commands/forced-colors.ts
55
+ var emulatedMedia = useEmulatedMedia();
56
+ Cypress.Commands.add("forcedColors", (mode) => {
57
+ emulatedMedia.forcedColors(mode);
58
+ const event = "remote:debugger:protocol";
59
+ cy.wrap(Cypress.automation(event, emulatedMedia.cdp()), { log: false });
60
+ });
61
+
62
+ // src/commands/prefers-color-scheme.ts
63
+ var emulatedMedia2 = useEmulatedMedia();
64
+ Cypress.Commands.add("prefersColorScheme", (value) => {
65
+ emulatedMedia2.prefersColorScheme(value);
66
+ const event = "remote:debugger:protocol";
67
+ cy.wrap(Cypress.automation(event, emulatedMedia2.cdp()), { log: false });
68
+ });
69
+
7
70
  // src/support/configure.ts
71
+ function resetEmulatedMedia(emulatedMedia3) {
72
+ if (!emulatedMedia3.isEnabled()) {
73
+ return;
74
+ }
75
+ emulatedMedia3.reset();
76
+ const event = "remote:debugger:protocol";
77
+ cy.wrap(Cypress.automation(event, emulatedMedia3.cdp()), {
78
+ log: false
79
+ });
80
+ }
8
81
  function configure(options) {
9
82
  if (options.afterEach.htmlvalidate) {
10
83
  afterEach(() => {
11
84
  cy.htmlvalidate();
12
85
  });
13
86
  }
87
+ if (options.resetEmulatedMedia) {
88
+ const emulatedMedia3 = useEmulatedMedia();
89
+ afterEach(() => {
90
+ resetEmulatedMedia(emulatedMedia3);
91
+ });
92
+ beforeEach(() => {
93
+ resetEmulatedMedia(emulatedMedia3);
94
+ });
95
+ }
14
96
  }
15
97
  export {
16
98
  configure
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forsakringskassan/cypress-config",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "description": "Sharable Cypress configuration building blocks for Försäkringskassan",
5
5
  "keywords": [
6
6
  "cypress"
@@ -17,8 +17,8 @@
17
17
  "author": "Försäkringskassan",
18
18
  "sideEffects": [
19
19
  "./dist/support.mjs",
20
- "./src/support/axe.ts",
21
- "./src/support/html-validate.ts"
20
+ "./src/commands/*",
21
+ "./src/support/*"
22
22
  ],
23
23
  "type": "module",
24
24
  "imports": {